Logo Search packages:      
Sourcecode: icu version File versions

U_STABLE const UChar* U_EXPORT2 ucurr_getName ( const UChar *  currency,
const char *  locale,
UCurrNameStyle  nameStyle,
UBool isChoiceFormat,
int32_t *  len,
UErrorCode ec 
)

Returns the display name for the given currency in the given locale. For example, the display name for the USD currency object in the en_US locale is "$".

Parameters:
currency null-terminated 3-letter ISO 4217 code
locale locale in which to display currency
nameStyle selector for which kind of name to return
isChoiceFormat fill-in set to TRUE if the returned value is a ChoiceFormat pattern; otherwise it is a static string
len fill-in parameter to receive length of result
ec error code
Returns:
pointer to display string of 'len' UChars. If the resource data contains no entry for 'currency', then 'currency' itself is returned. If *isChoiceFormat is TRUE, then the result is a ChoiceFormat pattern. Otherwise it is a static string. ICU 2.6

Definition at line 475 of file ucurr.cpp.

References FALSE, NULL, U_FAILURE, U_ILLEGAL_ARGUMENT_ERROR, U_STRING_NOT_TERMINATED_WARNING, u_strlen(), U_SUCCESS, U_USING_DEFAULT_WARNING, U_USING_FALLBACK_WARNING, U_ZERO_ERROR, and ULOC_FULLNAME_CAPACITY.

                              {

    // Look up the Currencies resource for the given locale.  The
    // Currencies locale data looks like this:
    //|en {
    //|  Currencies {
    //|    USD { "US$", "US Dollar" }
    //|    CHF { "Sw F", "Swiss Franc" }
    //|    INR { "=0#Rs|1#Re|1<Rs", "=0#Rupees|1#Rupee|1<Rupees" }
    //|    //...
    //|  }
    //|}

    if (U_FAILURE(*ec)) {
        return 0;
    }

    int32_t choice = (int32_t) nameStyle;
    if (choice < 0 || choice > 1) {
        *ec = U_ILLEGAL_ARGUMENT_ERROR;
        return 0;
    }

    // In the future, resource bundles may implement multi-level
    // fallback.  That is, if a currency is not found in the en_US
    // Currencies data, then the en Currencies data will be searched.
    // Currently, if a Currencies datum exists in en_US and en, the
    // en_US entry hides that in en.

    // We want multi-level fallback for this resource, so we implement
    // it manually.

    // Use a separate UErrorCode here that does not propagate out of
    // this function.
    UErrorCode ec2 = U_ZERO_ERROR;

    char loc[ULOC_FULLNAME_CAPACITY];
    uloc_getName(locale, loc, sizeof(loc), &ec2);
    if (U_FAILURE(ec2) || ec2 == U_STRING_NOT_TERMINATED_WARNING) {
        *ec = U_ILLEGAL_ARGUMENT_ERROR;
        return 0;
    }

    char buf[ISO_COUNTRY_CODE_LENGTH+1];
    myUCharsToChars(buf, currency);

    const UChar* s = NULL;
    ec2 = U_ZERO_ERROR;
    UResourceBundle* rb = ures_open(NULL, loc, &ec2);

    rb = ures_getByKey(rb, CURRENCIES, rb, &ec2);

    // Fetch resource with multi-level resource inheritance fallback
    rb = ures_getByKeyWithFallback(rb, buf, rb, &ec2);

    s = ures_getStringByIndex(rb, choice, len, &ec2);
    ures_close(rb);

    // If we've succeeded we're done.  Otherwise, try to fallback.
    // If that fails (because we are already at root) then exit.
    if (U_SUCCESS(ec2)) {
        if (ec2 == U_USING_DEFAULT_WARNING
            || (ec2 == U_USING_FALLBACK_WARNING && *ec != U_USING_DEFAULT_WARNING)) {
            *ec = ec2;
        }
    }

    // Determine if this is a ChoiceFormat pattern.  One leading mark
    // indicates a ChoiceFormat.  Two indicates a static string that
    // starts with a mark.  In either case, the first mark is ignored,
    // if present.  Marks in the rest of the string have no special
    // meaning.
    *isChoiceFormat = FALSE;
    if (U_SUCCESS(ec2)) {
        U_ASSERT(s != NULL);
        int32_t i=0;
        while (i < *len && s[i] == CHOICE_FORMAT_MARK && i < 2) {
            ++i;
        }
        *isChoiceFormat = (i == 1);
        if (i != 0) ++s; // Skip over first mark
        return s;
    }

    // If we fail to find a match, use the ISO 4217 code
    *len = u_strlen(currency); // Should == ISO_COUNTRY_CODE_LENGTH, but maybe not...?
    *ec = U_USING_DEFAULT_WARNING;
    return currency;
}


Generated by  Doxygen 1.6.0   Back to index