Home | History | Annotate | Download | only in i18n
      1 /*
      2 *******************************************************************************
      3 * Copyright (C) 1997-2011, International Business Machines Corporation and    *
      4 * others. All Rights Reserved.                                                *
      5 *******************************************************************************
      6 *
      7 * File DCFMTSYM.CPP
      8 *
      9 * Modification History:
     10 *
     11 *   Date        Name        Description
     12 *   02/19/97    aliu        Converted from java.
     13 *   03/18/97    clhuang     Implemented with C++ APIs.
     14 *   03/27/97    helena      Updated to pass the simple test after code review.
     15 *   08/26/97    aliu        Added currency/intl currency symbol support.
     16 *   07/20/98    stephen     Slightly modified initialization of monetarySeparator
     17 ********************************************************************************
     18 */
     19 
     20 #include "unicode/utypes.h"
     21 
     22 #if !UCONFIG_NO_FORMATTING
     23 
     24 #include "unicode/dcfmtsym.h"
     25 #include "unicode/ures.h"
     26 #include "unicode/decimfmt.h"
     27 #include "unicode/ucurr.h"
     28 #include "unicode/choicfmt.h"
     29 #include "unicode/unistr.h"
     30 #include "unicode/numsys.h"
     31 #include "unicode/unum.h"
     32 #include "ucurrimp.h"
     33 #include "cstring.h"
     34 #include "locbased.h"
     35 #include "uresimp.h"
     36 #include "ureslocs.h"
     37 
     38 // *****************************************************************************
     39 // class DecimalFormatSymbols
     40 // *****************************************************************************
     41 
     42 U_NAMESPACE_BEGIN
     43 
     44 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(DecimalFormatSymbols)
     45 
     46 static const char gNumberElements[] = "NumberElements";
     47 static const char gCurrencySpacingTag[] = "currencySpacing";
     48 static const char gBeforeCurrencyTag[] = "beforeCurrency";
     49 static const char gAfterCurrencyTag[] = "afterCurrency";
     50 static const char gCurrencyMatchTag[] = "currencyMatch";
     51 static const char gCurrencySudMatchTag[] = "surroundingMatch";
     52 static const char gCurrencyInsertBtnTag[] = "insertBetween";
     53 
     54 
     55 static const UChar INTL_CURRENCY_SYMBOL_STR[] = {0xa4, 0xa4, 0};
     56 
     57 // -------------------------------------
     58 // Initializes this with the decimal format symbols in the default locale.
     59 
     60 DecimalFormatSymbols::DecimalFormatSymbols(UErrorCode& status)
     61     : UObject(),
     62     locale()
     63 {
     64     initialize(locale, status, TRUE);
     65 }
     66 
     67 // -------------------------------------
     68 // Initializes this with the decimal format symbols in the desired locale.
     69 
     70 DecimalFormatSymbols::DecimalFormatSymbols(const Locale& loc, UErrorCode& status)
     71     : UObject(),
     72     locale(loc)
     73 {
     74     initialize(locale, status);
     75 }
     76 
     77 // BEGIN android-added: we need a default constructor for performance (http://bugs.icu-project.org/trac/ticket/7392).
     78 // -------------------------------------
     79 
     80 DecimalFormatSymbols::DecimalFormatSymbols()
     81 {
     82     *validLocale = *actualLocale = 0;
     83     currPattern = NULL;
     84     initialize();
     85 }
     86 // END android-added
     87 
     88 // -------------------------------------
     89 
     90 DecimalFormatSymbols::~DecimalFormatSymbols()
     91 {
     92 }
     93 
     94 // -------------------------------------
     95 // copy constructor
     96 
     97 DecimalFormatSymbols::DecimalFormatSymbols(const DecimalFormatSymbols &source)
     98     : UObject(source)
     99 {
    100     *this = source;
    101 }
    102 
    103 // -------------------------------------
    104 // assignment operator
    105 
    106 DecimalFormatSymbols&
    107 DecimalFormatSymbols::operator=(const DecimalFormatSymbols& rhs)
    108 {
    109     if (this != &rhs) {
    110         for(int32_t i = 0; i < (int32_t)kFormatSymbolCount; ++i) {
    111             // fastCopyFrom is safe, see docs on fSymbols
    112             fSymbols[(ENumberFormatSymbol)i].fastCopyFrom(rhs.fSymbols[(ENumberFormatSymbol)i]);
    113         }
    114         for(int32_t i = 0; i < (int32_t)UNUM_CURRENCY_SPACING_COUNT; ++i) {
    115             currencySpcBeforeSym[i].fastCopyFrom(rhs.currencySpcBeforeSym[i]);
    116             currencySpcAfterSym[i].fastCopyFrom(rhs.currencySpcAfterSym[i]);
    117         }
    118         locale = rhs.locale;
    119         uprv_strcpy(validLocale, rhs.validLocale);
    120         uprv_strcpy(actualLocale, rhs.actualLocale);
    121     }
    122     return *this;
    123 }
    124 
    125 // -------------------------------------
    126 
    127 UBool
    128 DecimalFormatSymbols::operator==(const DecimalFormatSymbols& that) const
    129 {
    130     if (this == &that) {
    131         return TRUE;
    132     }
    133     for(int32_t i = 0; i < (int32_t)kFormatSymbolCount; ++i) {
    134         if(fSymbols[(ENumberFormatSymbol)i] != that.fSymbols[(ENumberFormatSymbol)i]) {
    135             return FALSE;
    136         }
    137     }
    138     for(int32_t i = 0; i < (int32_t)UNUM_CURRENCY_SPACING_COUNT; ++i) {
    139         if(currencySpcBeforeSym[i] != that.currencySpcBeforeSym[i]) {
    140             return FALSE;
    141         }
    142         if(currencySpcAfterSym[i] != that.currencySpcAfterSym[i]) {
    143             return FALSE;
    144         }
    145     }
    146     return locale == that.locale &&
    147         uprv_strcmp(validLocale, that.validLocale) == 0 &&
    148         uprv_strcmp(actualLocale, that.actualLocale) == 0;
    149 }
    150 
    151 // -------------------------------------
    152 
    153 void
    154 DecimalFormatSymbols::initialize(const Locale& loc, UErrorCode& status, UBool useLastResortData)
    155 {
    156     static const char *gNumberElementKeys[kFormatSymbolCount] = {
    157         "decimal",
    158         "group",
    159         "list",
    160         "percentSign",
    161         NULL, /* Native zero digit is deprecated from CLDR - get it from the numbering system */
    162         NULL, /* Pattern digit character is deprecated from CLDR - use # by default always */
    163         "minusSign",
    164         "plusSign",
    165         NULL, /* currency symbol - We don't really try to load this directly from CLDR until we know the currency */
    166         NULL, /* intl currency symbol - We don't really try to load this directly from CLDR until we know the currency */
    167         "currencyDecimal",
    168         "exponential",
    169         "perMille",
    170         NULL, /* Escape padding character - not in CLDR */
    171         "infinity",
    172         "nan",
    173         NULL, /* Significant digit symbol - not in CLDR */
    174         "currencyGroup",
    175         NULL, /* one digit - get it from the numbering system */
    176         NULL, /* two digit - get it from the numbering system */
    177         NULL, /* three digit - get it from the numbering system */
    178         NULL, /* four digit - get it from the numbering system */
    179         NULL, /* five digit - get it from the numbering system */
    180         NULL, /* six digit - get it from the numbering system */
    181         NULL, /* seven digit - get it from the numbering system */
    182         NULL, /* eight digit - get it from the numbering system */
    183         NULL, /* nine digit - get it from the numbering system */
    184     };
    185 
    186     static const char *gLatn =  "latn";
    187     static const char *gSymbols = "symbols";
    188     const char *nsName;
    189     const UChar *sym = NULL;
    190     int32_t len = 0;
    191 
    192     *validLocale = *actualLocale = 0;
    193     currPattern = NULL;
    194     if (U_FAILURE(status))
    195         return;
    196 
    197     const char* locStr = loc.getName();
    198     UResourceBundle *resource = ures_open((char *)0, locStr, &status);
    199     UResourceBundle *numberElementsRes = ures_getByKeyWithFallback(resource, gNumberElements, NULL, &status);
    200 
    201     if (U_FAILURE(status)) {
    202         if ( useLastResortData ) {
    203             status = U_USING_DEFAULT_WARNING;
    204             initialize();
    205         }
    206         return;
    207     } else {
    208 
    209         // First initialize all the symbols to the fallbacks for anything we can't find
    210         initialize();
    211 
    212         //
    213         // Next get the numbering system for this locale and set zero digit
    214         // and the digit string based on the numbering system for the locale
    215         //
    216 
    217         NumberingSystem* ns = NumberingSystem::createInstance(loc,status);
    218         if (U_SUCCESS(status) && ns->getRadix() == 10 && !ns->isAlgorithmic()) {
    219             nsName = ns->getName();
    220             UnicodeString digitString(ns->getDescription());
    221             setSymbol(kZeroDigitSymbol,  digitString.tempSubString(0, 1), FALSE);
    222             setSymbol(kOneDigitSymbol,   digitString.tempSubString(1, 1), FALSE);
    223             setSymbol(kTwoDigitSymbol,   digitString.tempSubString(2, 1), FALSE);
    224             setSymbol(kThreeDigitSymbol, digitString.tempSubString(3, 1), FALSE);
    225             setSymbol(kFourDigitSymbol,  digitString.tempSubString(4, 1), FALSE);
    226             setSymbol(kFiveDigitSymbol,  digitString.tempSubString(5, 1), FALSE);
    227             setSymbol(kSixDigitSymbol,   digitString.tempSubString(6, 1), FALSE);
    228             setSymbol(kSevenDigitSymbol, digitString.tempSubString(7, 1), FALSE);
    229             setSymbol(kEightDigitSymbol, digitString.tempSubString(8, 1), FALSE);
    230             setSymbol(kNineDigitSymbol,  digitString.tempSubString(9, 1), FALSE);
    231         } else {
    232             nsName = gLatn;
    233         }
    234 
    235         UBool isLatn = !uprv_strcmp(nsName,gLatn);
    236 
    237         UErrorCode nlStatus = U_ZERO_ERROR;
    238         UResourceBundle *nonLatnSymbols = NULL;
    239         if ( !isLatn ) {
    240             nonLatnSymbols = ures_getByKeyWithFallback(numberElementsRes, nsName, NULL, &nlStatus);
    241             nonLatnSymbols = ures_getByKeyWithFallback(nonLatnSymbols, gSymbols, nonLatnSymbols, &nlStatus);
    242         }
    243 
    244         UResourceBundle *latnSymbols = ures_getByKeyWithFallback(numberElementsRes, gLatn, NULL, &status);
    245         latnSymbols = ures_getByKeyWithFallback(latnSymbols, gSymbols, latnSymbols, &status);
    246 
    247         UBool kMonetaryDecimalSet = FALSE;
    248         UBool kMonetaryGroupingSet = FALSE;
    249         for(int32_t i = 0; i<kFormatSymbolCount; i++) {
    250             if ( gNumberElementKeys[i] != NULL ) {
    251                 UErrorCode localStatus = U_ZERO_ERROR;
    252                 if ( !isLatn ) {
    253                     sym = ures_getStringByKeyWithFallback(nonLatnSymbols,gNumberElementKeys[i],&len,&localStatus);
    254                     // If we can't find the symbol in the numbering system specific resources,
    255                     // use the "latn" numbering system as the fallback.
    256                     if ( U_FAILURE(localStatus) ) {
    257                         localStatus = U_ZERO_ERROR;
    258                         sym = ures_getStringByKeyWithFallback(latnSymbols,gNumberElementKeys[i],&len,&localStatus);
    259                     }
    260                 } else {
    261                         sym = ures_getStringByKeyWithFallback(latnSymbols,gNumberElementKeys[i],&len,&localStatus);
    262                 }
    263 
    264                 if ( U_SUCCESS(localStatus) ) {
    265                     setSymbol((ENumberFormatSymbol)i, UnicodeString(TRUE, sym, len));
    266                     if ( i == kMonetarySeparatorSymbol ) {
    267                         kMonetaryDecimalSet = TRUE;
    268                     } else if ( i == kMonetaryGroupingSeparatorSymbol ) {
    269                         kMonetaryGroupingSet = TRUE;
    270                     }
    271                 }
    272             }
    273         }
    274 
    275         ures_close(latnSymbols);
    276         if ( !isLatn ) {
    277             ures_close(nonLatnSymbols);
    278         }
    279 
    280         // If monetary decimal or grouping were not explicitly set, then set them to be the
    281         // same as their non-monetary counterparts.
    282 
    283         if ( !kMonetaryDecimalSet ) {
    284             setSymbol(kMonetarySeparatorSymbol,fSymbols[kDecimalSeparatorSymbol]);
    285         }
    286         if ( !kMonetaryGroupingSet ) {
    287             setSymbol(kMonetaryGroupingSeparatorSymbol,fSymbols[kGroupingSeparatorSymbol]);
    288         }
    289 
    290         if (ns) {
    291             delete ns;
    292         }
    293 
    294         // Obtain currency data from the currency API.  This is strictly
    295         // for backward compatibility; we don't use DecimalFormatSymbols
    296         // for currency data anymore.
    297         UErrorCode internalStatus = U_ZERO_ERROR; // don't propagate failures out
    298         UChar curriso[4];
    299         UnicodeString tempStr;
    300         ucurr_forLocale(locStr, curriso, 4, &internalStatus);
    301 
    302         // Reuse numberElements[0] as a temporary buffer
    303         uprv_getStaticCurrencyName(curriso, locStr, tempStr, internalStatus);
    304         if (U_SUCCESS(internalStatus)) {
    305             fSymbols[kIntlCurrencySymbol].setTo(curriso, -1);
    306             fSymbols[kCurrencySymbol] = tempStr;
    307         }
    308         /* else use the default values. */
    309 
    310         U_LOCALE_BASED(locBased, *this);
    311         locBased.setLocaleIDs(ures_getLocaleByType(numberElementsRes,
    312                               ULOC_VALID_LOCALE, &status),
    313                               ures_getLocaleByType(numberElementsRes,
    314                               ULOC_ACTUAL_LOCALE, &status));
    315 
    316         //load the currency data
    317         UChar ucc[4]={0}; //Currency Codes are always 3 chars long
    318         int32_t uccLen = 4;
    319         const char* locName = loc.getName();
    320         UErrorCode localStatus = U_ZERO_ERROR;
    321         uccLen = ucurr_forLocale(locName, ucc, uccLen, &localStatus);
    322 
    323         if(U_SUCCESS(localStatus) && uccLen > 0) {
    324             char cc[4]={0};
    325             u_UCharsToChars(ucc, cc, uccLen);
    326             /* An explicit currency was requested */
    327             UResourceBundle *currencyResource = ures_open(U_ICUDATA_CURR, locStr, &localStatus);
    328             UResourceBundle *currency = ures_getByKeyWithFallback(currencyResource, "Currencies", NULL, &localStatus);
    329             currency = ures_getByKeyWithFallback(currency, cc, currency, &localStatus);
    330             if(U_SUCCESS(localStatus) && ures_getSize(currency)>2) { // the length is 3 if more data is present
    331                 currency = ures_getByIndex(currency, 2, currency, &localStatus);
    332                 int32_t currPatternLen = 0;
    333                 currPattern = ures_getStringByIndex(currency, (int32_t)0, &currPatternLen, &localStatus);
    334                 UnicodeString decimalSep = ures_getUnicodeStringByIndex(currency, (int32_t)1, &localStatus);
    335                 UnicodeString groupingSep = ures_getUnicodeStringByIndex(currency, (int32_t)2, &localStatus);
    336                 if(U_SUCCESS(localStatus)){
    337                     fSymbols[kMonetaryGroupingSeparatorSymbol] = groupingSep;
    338                     fSymbols[kMonetarySeparatorSymbol] = decimalSep;
    339                     //pattern.setTo(TRUE, currPattern, currPatternLen);
    340                     status = localStatus;
    341                 }
    342             }
    343             ures_close(currency);
    344             ures_close(currencyResource);
    345             /* else An explicit currency was requested and is unknown or locale data is malformed. */
    346             /* ucurr_* API will get the correct value later on. */
    347         }
    348             // else ignore the error if no currency
    349 
    350         // Currency Spacing.
    351         localStatus = U_ZERO_ERROR;
    352         UResourceBundle *currencyResource = ures_open(U_ICUDATA_CURR, locStr, &localStatus);
    353         UResourceBundle *currencySpcRes = ures_getByKeyWithFallback(currencyResource,
    354                                            gCurrencySpacingTag, NULL, &localStatus);
    355 
    356         if (localStatus == U_USING_FALLBACK_WARNING || U_SUCCESS(localStatus)) {
    357             const char* keywords[UNUM_CURRENCY_SPACING_COUNT] = {
    358                 gCurrencyMatchTag, gCurrencySudMatchTag, gCurrencyInsertBtnTag
    359             };
    360             localStatus = U_ZERO_ERROR;
    361             UResourceBundle *dataRes = ures_getByKeyWithFallback(currencySpcRes,
    362                                        gBeforeCurrencyTag, NULL, &localStatus);
    363             if (localStatus == U_USING_FALLBACK_WARNING || U_SUCCESS(localStatus)) {
    364                 localStatus = U_ZERO_ERROR;
    365                 for (int32_t i = 0; i < UNUM_CURRENCY_SPACING_COUNT; i++) {
    366                   currencySpcBeforeSym[i] = ures_getUnicodeStringByKey(dataRes, keywords[i], &localStatus);
    367                 }
    368                 ures_close(dataRes);
    369             }
    370             dataRes = ures_getByKeyWithFallback(currencySpcRes,
    371                                       gAfterCurrencyTag, NULL, &localStatus);
    372             if (localStatus == U_USING_FALLBACK_WARNING || U_SUCCESS(localStatus)) {
    373                 localStatus = U_ZERO_ERROR;
    374                 for (int32_t i = 0; i < UNUM_CURRENCY_SPACING_COUNT; i++) {
    375                   currencySpcAfterSym[i] = ures_getUnicodeStringByKey(dataRes, keywords[i], &localStatus);
    376                 }
    377                 ures_close(dataRes);
    378             }
    379             ures_close(currencySpcRes);
    380             ures_close(currencyResource);
    381         }
    382     }
    383     ures_close(resource);
    384     ures_close(numberElementsRes);
    385 
    386 }
    387 
    388 void
    389 DecimalFormatSymbols::initialize() {
    390     /*
    391      * These strings used to be in static arrays, but the HP/UX aCC compiler
    392      * cannot initialize a static array with class constructors.
    393      *  markus 2000may25
    394      */
    395     fSymbols[kDecimalSeparatorSymbol] = (UChar)0x2e;    // '.' decimal separator
    396     fSymbols[kGroupingSeparatorSymbol].remove();        //     group (thousands) separator
    397     fSymbols[kPatternSeparatorSymbol] = (UChar)0x3b;    // ';' pattern separator
    398     fSymbols[kPercentSymbol] = (UChar)0x25;             // '%' percent sign
    399     fSymbols[kZeroDigitSymbol] = (UChar)0x30;           // '0' native 0 digit
    400     fSymbols[kOneDigitSymbol] = (UChar)0x31;            // '1' native 1 digit
    401     fSymbols[kTwoDigitSymbol] = (UChar)0x32;            // '2' native 2 digit
    402     fSymbols[kThreeDigitSymbol] = (UChar)0x33;          // '3' native 3 digit
    403     fSymbols[kFourDigitSymbol] = (UChar)0x34;           // '4' native 4 digit
    404     fSymbols[kFiveDigitSymbol] = (UChar)0x35;           // '5' native 5 digit
    405     fSymbols[kSixDigitSymbol] = (UChar)0x36;            // '6' native 6 digit
    406     fSymbols[kSevenDigitSymbol] = (UChar)0x37;          // '7' native 7 digit
    407     fSymbols[kEightDigitSymbol] = (UChar)0x38;          // '8' native 8 digit
    408     fSymbols[kNineDigitSymbol] = (UChar)0x39;           // '9' native 9 digit
    409     fSymbols[kDigitSymbol] = (UChar)0x23;               // '#' pattern digit
    410     fSymbols[kPlusSignSymbol] = (UChar)0x002b;          // '+' plus sign
    411     fSymbols[kMinusSignSymbol] = (UChar)0x2d;           // '-' minus sign
    412     fSymbols[kCurrencySymbol] = (UChar)0xa4;            // 'OX' currency symbol
    413     fSymbols[kIntlCurrencySymbol].setTo(TRUE, INTL_CURRENCY_SYMBOL_STR, 2);
    414     fSymbols[kMonetarySeparatorSymbol] = (UChar)0x2e;   // '.' monetary decimal separator
    415     fSymbols[kExponentialSymbol] = (UChar)0x45;         // 'E' exponential
    416     fSymbols[kPerMillSymbol] = (UChar)0x2030;           // '%o' per mill
    417     fSymbols[kPadEscapeSymbol] = (UChar)0x2a;           // '*' pad escape symbol
    418     fSymbols[kInfinitySymbol] = (UChar)0x221e;          // 'oo' infinite
    419     fSymbols[kNaNSymbol] = (UChar)0xfffd;               // SUB NaN
    420     fSymbols[kSignificantDigitSymbol] = (UChar)0x0040;  // '@' significant digit
    421     fSymbols[kMonetaryGroupingSeparatorSymbol].remove(); //
    422 }
    423 
    424 Locale
    425 DecimalFormatSymbols::getLocale(ULocDataLocaleType type, UErrorCode& status) const {
    426     U_LOCALE_BASED(locBased, *this);
    427     return locBased.getLocale(type, status);
    428 }
    429 
    430 const UnicodeString&
    431 DecimalFormatSymbols::getPatternForCurrencySpacing(UCurrencySpacing type,
    432                                                  UBool beforeCurrency,
    433                                                  UErrorCode& status) const {
    434     if (U_FAILURE(status)) {
    435       return fNoSymbol;  // always empty.
    436     }
    437     if (beforeCurrency) {
    438       return currencySpcBeforeSym[(int32_t)type];
    439     } else {
    440       return currencySpcAfterSym[(int32_t)type];
    441     }
    442 }
    443 
    444 void
    445 DecimalFormatSymbols::setPatternForCurrencySpacing(UCurrencySpacing type,
    446                                                    UBool beforeCurrency,
    447                                              const UnicodeString& pattern) {
    448   if (beforeCurrency) {
    449     currencySpcBeforeSym[(int32_t)type] = pattern;
    450   } else {
    451     currencySpcAfterSym[(int32_t)type] =  pattern;
    452   }
    453 }
    454 U_NAMESPACE_END
    455 
    456 #endif /* #if !UCONFIG_NO_FORMATTING */
    457 
    458 //eof
    459