static bool getDateTimePatterns(JNIEnv* env, jobject localeData, const char* locale_name) { UErrorCode status = U_ZERO_ERROR; ScopedResourceBundle root(ures_open(NULL, locale_name, &status)); if (U_FAILURE(status)) { return false; } ScopedResourceBundle calendar(ures_getByKey(root.get(), "calendar", NULL, &status)); if (U_FAILURE(status)) { return false; } ScopedResourceBundle gregorian(ures_getByKey(calendar.get(), "gregorian", NULL, &status)); if (U_FAILURE(status)) { return false; } ScopedResourceBundle dateTimePatterns(ures_getByKey(gregorian.get(), "DateTimePatterns", NULL, &status)); if (U_FAILURE(status)) { return false; } setStringField(env, localeData, "fullTimeFormat", dateTimePatterns.get(), 0); setStringField(env, localeData, "longTimeFormat", dateTimePatterns.get(), 1); setStringField(env, localeData, "mediumTimeFormat", dateTimePatterns.get(), 2); setStringField(env, localeData, "shortTimeFormat", dateTimePatterns.get(), 3); setStringField(env, localeData, "fullDateFormat", dateTimePatterns.get(), 4); setStringField(env, localeData, "longDateFormat", dateTimePatterns.get(), 5); setStringField(env, localeData, "mediumDateFormat", dateTimePatterns.get(), 6); setStringField(env, localeData, "shortDateFormat", dateTimePatterns.get(), 7); return true; }
static bool getYesterdayTodayAndTomorrow(JNIEnv* env, jobject localeData, const char* locale_name) { UErrorCode status = U_ZERO_ERROR; ScopedResourceBundle root(ures_open(NULL, locale_name, &status)); if (U_FAILURE(status)) { return false; } ScopedResourceBundle calendar(ures_getByKey(root.get(), "calendar", NULL, &status)); if (U_FAILURE(status)) { return false; } ScopedResourceBundle gregorian(ures_getByKey(calendar.get(), "gregorian", NULL, &status)); if (U_FAILURE(status)) { return false; } ScopedResourceBundle fields(ures_getByKey(gregorian.get(), "fields", NULL, &status)); if (U_FAILURE(status)) { return false; } ScopedResourceBundle day(ures_getByKey(fields.get(), "day", NULL, &status)); if (U_FAILURE(status)) { return false; } ScopedResourceBundle relative(ures_getByKey(day.get(), "relative", NULL, &status)); if (U_FAILURE(status)) { return false; } // bn_BD only has a "-2" entry. if (relative.hasKey("-1") && relative.hasKey("0") && relative.hasKey("1")) { setStringField(env, localeData, "yesterday", relative.get(), "-1"); setStringField(env, localeData, "today", relative.get(), "0"); setStringField(env, localeData, "tomorrow", relative.get(), "1"); return true; } return false; }
static bool getAmPmMarkersNarrow(JNIEnv* env, jobject localeData, const char* locale_name) { UErrorCode status = U_ZERO_ERROR; ScopedResourceBundle root(ures_open(NULL, locale_name, &status)); if (U_FAILURE(status)) { return false; } ScopedResourceBundle calendar(ures_getByKey(root.get(), "calendar", NULL, &status)); if (U_FAILURE(status)) { return false; } ScopedResourceBundle gregorian(ures_getByKey(calendar.get(), "gregorian", NULL, &status)); if (U_FAILURE(status)) { return false; } ScopedResourceBundle amPmMarkersNarrow(ures_getByKey(gregorian.get(), "AmPmMarkersNarrow", NULL, &status)); if (U_FAILURE(status)) { return false; } setStringField(env, localeData, "narrowAm", amPmMarkersNarrow.get(), 0); setStringField(env, localeData, "narrowPm", amPmMarkersNarrow.get(), 1); return true; }
static jboolean initLocaleDataImpl(JNIEnv* env, jclass clazz, jstring locale, jobject localeData) { const char* loc = env->GetStringUTFChars(locale, NULL); UErrorCode status = U_ZERO_ERROR; ScopedResourceBundle root(ures_openU(NULL, loc, &status)); env->ReleaseStringUTFChars(locale, loc); if (U_FAILURE(status)) { LOGE("Error getting ICU resource bundle: %s", u_errorName(status)); status = U_ZERO_ERROR; return JNI_FALSE; } ScopedResourceBundle calendar(ures_getByKey(root.get(), "calendar", NULL, &status)); if (U_FAILURE(status)) { LOGE("Error getting ICU calendar resource bundle: %s", u_errorName(status)); return JNI_FALSE; } ScopedResourceBundle gregorian(ures_getByKey(calendar.get(), "gregorian", NULL, &status)); if (U_FAILURE(status)) { LOGE("Error getting ICU gregorian resource bundle: %s", u_errorName(status)); return JNI_FALSE; } int firstDayVals[2]; if (getDayIntVector(env, gregorian.get(), firstDayVals)) { setIntegerField(env, localeData, "firstDayOfWeek", firstDayVals[0]); setIntegerField(env, localeData, "minimalDaysInFirstWeek", firstDayVals[1]); } setStringArrayField(env, localeData, "amPm", getAmPmMarkers(env, gregorian.get())); setStringArrayField(env, localeData, "eras", getEras(env, gregorian.get())); setStringArrayField(env, localeData, "longMonthNames", getLongMonthNames(env, gregorian.get())); setStringArrayField(env, localeData, "shortMonthNames", getShortMonthNames(env, gregorian.get())); setStringArrayField(env, localeData, "longWeekdayNames", getLongWeekdayNames(env, gregorian.get())); setStringArrayField(env, localeData, "shortWeekdayNames", getShortWeekdayNames(env, gregorian.get())); ScopedResourceBundle gregorianElems(ures_getByKey(gregorian.get(), "DateTimePatterns", NULL, &status)); if (U_SUCCESS(status)) { setStringField(env, localeData, "fullTimeFormat", gregorianElems.get(), 0); setStringField(env, localeData, "longTimeFormat", gregorianElems.get(), 1); setStringField(env, localeData, "mediumTimeFormat", gregorianElems.get(), 2); setStringField(env, localeData, "shortTimeFormat", gregorianElems.get(), 3); setStringField(env, localeData, "fullDateFormat", gregorianElems.get(), 4); setStringField(env, localeData, "longDateFormat", gregorianElems.get(), 5); setStringField(env, localeData, "mediumDateFormat", gregorianElems.get(), 6); setStringField(env, localeData, "shortDateFormat", gregorianElems.get(), 7); } status = U_ZERO_ERROR; ScopedResourceBundle numberElements(ures_getByKey(root.get(), "NumberElements", NULL, &status)); if (U_SUCCESS(status) && ures_getSize(numberElements.get()) >= 11) { setStringField(env, localeData, "decimalPatternChars", getDecimalPatternChars(env, numberElements.get())); setStringField(env, localeData, "infinity", numberElements.get(), 9); setStringField(env, localeData, "NaN", numberElements.get(), 10); } status = U_ZERO_ERROR; jstring internationalCurrencySymbol = getIntCurrencyCode(env, locale); jstring currencySymbol = NULL; if (internationalCurrencySymbol != NULL) { currencySymbol = getCurrencySymbolNative(env, clazz, locale, internationalCurrencySymbol); } else { internationalCurrencySymbol = env->NewStringUTF("XXX"); } if (currencySymbol == NULL) { // This is the UTF-8 encoding of U+00A4 (CURRENCY SIGN). currencySymbol = env->NewStringUTF("\xc2\xa4"); } setStringField(env, localeData, "currencySymbol", currencySymbol); setStringField(env, localeData, "internationalCurrencySymbol", internationalCurrencySymbol); ScopedResourceBundle numberPatterns(ures_getByKey(root.get(), "NumberPatterns", NULL, &status)); if (U_SUCCESS(status) && ures_getSize(numberPatterns.get()) >= 3) { setStringField(env, localeData, "numberPattern", numberPatterns.get(), 0); setStringField(env, localeData, "currencyPattern", numberPatterns.get(), 1); setStringField(env, localeData, "percentPattern", numberPatterns.get(), 2); } return JNI_TRUE; }
static jboolean ICU_initLocaleDataImpl(JNIEnv* env, jclass, jstring locale, jobject localeData) { ScopedUtfChars localeName(env, locale); if (localeName.c_str() == NULL) { return JNI_FALSE; } // Get DateTimePatterns UErrorCode status; char currentLocale[ULOC_FULLNAME_CAPACITY]; int32_t localeNameLen = 0; if (localeName.size() >= ULOC_FULLNAME_CAPACITY) { return JNI_FALSE; // Exceed ICU defined limit of the whole locale ID. } strcpy(currentLocale, localeName.c_str()); do { status = U_ZERO_ERROR; ScopedResourceBundle root(ures_open(NULL, currentLocale, &status)); if (U_FAILURE(status)) { if (localeNameLen == 0) { break; // No parent locale, report this error outside the loop. } else { status = U_ZERO_ERROR; continue; // get parent locale. } } ScopedResourceBundle calendar(ures_getByKey(root.get(), "calendar", NULL, &status)); if (U_FAILURE(status)) { status = U_ZERO_ERROR; continue; // get parent locale. } ScopedResourceBundle gregorian(ures_getByKey(calendar.get(), "gregorian", NULL, &status)); if (U_FAILURE(status)) { status = U_ZERO_ERROR; continue; // get parent locale. } ScopedResourceBundle dateTimePatterns(ures_getByKey(gregorian.get(), "DateTimePatterns", NULL, &status)); if (U_SUCCESS(status)) { setStringField(env, localeData, "fullTimeFormat", dateTimePatterns.get(), 0); setStringField(env, localeData, "longTimeFormat", dateTimePatterns.get(), 1); setStringField(env, localeData, "mediumTimeFormat", dateTimePatterns.get(), 2); setStringField(env, localeData, "shortTimeFormat", dateTimePatterns.get(), 3); setStringField(env, localeData, "fullDateFormat", dateTimePatterns.get(), 4); setStringField(env, localeData, "longDateFormat", dateTimePatterns.get(), 5); setStringField(env, localeData, "mediumDateFormat", dateTimePatterns.get(), 6); setStringField(env, localeData, "shortDateFormat", dateTimePatterns.get(), 7); break; } else { status = U_ZERO_ERROR; // get parent locale. } } while((localeNameLen = uloc_getParent(currentLocale, currentLocale, sizeof(currentLocale), &status)) >= 0); if (U_FAILURE(status)) { ALOGE("Error getting ICU resource bundle: %s", u_errorName(status)); return JNI_FALSE; } status = U_ZERO_ERROR; Locale localeObj = getLocale(env, locale); UniquePtr<Calendar> cal(Calendar::createInstance(localeObj, status)); if (U_FAILURE(status)) { return JNI_FALSE; } setIntegerField(env, localeData, "firstDayOfWeek", cal->getFirstDayOfWeek()); setIntegerField(env, localeData, "minimalDaysInFirstWeek", cal->getMinimalDaysInFirstWeek()); // Get DateFormatSymbols status = U_ZERO_ERROR; DateFormatSymbols dateFormatSym(localeObj, status); if (U_FAILURE(status)) { return JNI_FALSE; } int32_t count = 0; // Get AM/PM marker const UnicodeString* amPmStrs = dateFormatSym.getAmPmStrings(count); setStringArrayField(env, localeData, "amPm", amPmStrs, count); const UnicodeString* erasStrs = dateFormatSym.getEras(count); setStringArrayField(env, localeData, "eras", erasStrs, count); const UnicodeString* longMonthNames = dateFormatSym.getMonths(count, DateFormatSymbols::FORMAT, DateFormatSymbols::WIDE); setStringArrayField(env, localeData, "longMonthNames", longMonthNames, count); const UnicodeString* shortMonthNames = dateFormatSym.getMonths(count, DateFormatSymbols::FORMAT, DateFormatSymbols::ABBREVIATED); setStringArrayField(env, localeData, "shortMonthNames", shortMonthNames, count); const UnicodeString* longWeekdayNames = dateFormatSym.getWeekdays(count, DateFormatSymbols::FORMAT, DateFormatSymbols::WIDE); setStringArrayField(env, localeData, "longWeekdayNames", longWeekdayNames, count); const UnicodeString* shortWeekdayNames = dateFormatSym.getWeekdays(count, DateFormatSymbols::FORMAT, DateFormatSymbols::ABBREVIATED); setStringArrayField(env, localeData, "shortWeekdayNames", shortWeekdayNames, count); const UnicodeString* longStandAloneMonthNames = dateFormatSym.getMonths(count, DateFormatSymbols::STANDALONE, DateFormatSymbols::WIDE); setStringArrayField(env, localeData, "longStandAloneMonthNames", longStandAloneMonthNames, count); const UnicodeString* shortStandAloneMonthNames = dateFormatSym.getMonths(count, DateFormatSymbols::STANDALONE, DateFormatSymbols::ABBREVIATED); setStringArrayField(env, localeData, "shortStandAloneMonthNames", shortStandAloneMonthNames, count); const UnicodeString* longStandAloneWeekdayNames = dateFormatSym.getWeekdays(count, DateFormatSymbols::STANDALONE, DateFormatSymbols::WIDE); setStringArrayField(env, localeData, "longStandAloneWeekdayNames", longStandAloneWeekdayNames, count); const UnicodeString* shortStandAloneWeekdayNames = dateFormatSym.getWeekdays(count, DateFormatSymbols::STANDALONE, DateFormatSymbols::ABBREVIATED); setStringArrayField(env, localeData, "shortStandAloneWeekdayNames", shortStandAloneWeekdayNames, count); status = U_ZERO_ERROR; // For numberPatterns and symbols. setNumberPatterns(env, localeData, locale); setDecimalFormatSymbolsData(env, localeData, locale); jstring countryCode = env->NewStringUTF(Locale::createFromName(localeName.c_str()).getCountry()); jstring internationalCurrencySymbol = ICU_getCurrencyCode(env, NULL, countryCode); env->DeleteLocalRef(countryCode); countryCode = NULL; jstring currencySymbol = NULL; if (internationalCurrencySymbol != NULL) { currencySymbol = ICU_getCurrencySymbol(env, NULL, locale, internationalCurrencySymbol); } else { internationalCurrencySymbol = env->NewStringUTF("XXX"); } if (currencySymbol == NULL) { // This is the UTF-8 encoding of U+00A4 (CURRENCY SIGN). currencySymbol = env->NewStringUTF("\xc2\xa4"); } setStringField(env, localeData, "currencySymbol", currencySymbol); setStringField(env, localeData, "internationalCurrencySymbol", internationalCurrencySymbol); return JNI_TRUE; }
static jboolean ICU_initLocaleDataImpl(JNIEnv* env, jclass, jstring locale, jobject localeData) { ScopedUtfChars localeName(env, locale); UErrorCode status = U_ZERO_ERROR; ScopedResourceBundle root(ures_open(NULL, localeName.c_str(), &status)); if (U_FAILURE(status)) { LOGE("Error getting ICU resource bundle: %s", u_errorName(status)); status = U_ZERO_ERROR; return JNI_FALSE; } ScopedResourceBundle calendar(ures_getByKey(root.get(), "calendar", NULL, &status)); if (U_FAILURE(status)) { LOGE("Error getting ICU calendar resource bundle: %s", u_errorName(status)); return JNI_FALSE; } ScopedResourceBundle gregorian(ures_getByKey(calendar.get(), "gregorian", NULL, &status)); if (U_FAILURE(status)) { LOGE("Error getting ICU gregorian resource bundle: %s", u_errorName(status)); return JNI_FALSE; } int firstDayVals[] = { 0, 0 }; if (getDayIntVector(env, gregorian.get(), firstDayVals)) { setIntegerField(env, localeData, "firstDayOfWeek", firstDayVals[0]); setIntegerField(env, localeData, "minimalDaysInFirstWeek", firstDayVals[1]); } setStringArrayField(env, localeData, "amPm", getAmPmMarkers(env, gregorian.get())); setStringArrayField(env, localeData, "eras", getEras(env, gregorian.get())); ScopedResourceBundle dayNames(ures_getByKey(gregorian.get(), "dayNames", NULL, &status)); ScopedResourceBundle monthNames(ures_getByKey(gregorian.get(), "monthNames", NULL, &status)); // Get the regular month and weekday names. jobjectArray longMonthNames = getNames(env, monthNames.get(), true, REGULAR, LONG); jobjectArray shortMonthNames = getNames(env, monthNames.get(), true, REGULAR, SHORT); jobjectArray longWeekdayNames = getNames(env, dayNames.get(), false, REGULAR, LONG); jobjectArray shortWeekdayNames = getNames(env, dayNames.get(), false, REGULAR, SHORT); setStringArrayField(env, localeData, "longMonthNames", longMonthNames); setStringArrayField(env, localeData, "shortMonthNames", shortMonthNames); setStringArrayField(env, localeData, "longWeekdayNames", longWeekdayNames); setStringArrayField(env, localeData, "shortWeekdayNames", shortWeekdayNames); // Get the stand-alone month and weekday names. If they're not available (as they aren't for // English), we reuse the regular names. If we returned null to Java, the usual fallback // mechanisms would come into play and we'd end up with the bogus stand-alone names from the // root locale ("1" for January, and so on). jobjectArray longStandAloneMonthNames = getNames(env, monthNames.get(), true, STAND_ALONE, LONG); if (longStandAloneMonthNames == NULL) { longStandAloneMonthNames = longMonthNames; } jobjectArray shortStandAloneMonthNames = getNames(env, monthNames.get(), true, STAND_ALONE, SHORT); if (shortStandAloneMonthNames == NULL) { shortStandAloneMonthNames = shortMonthNames; } jobjectArray longStandAloneWeekdayNames = getNames(env, dayNames.get(), false, STAND_ALONE, LONG); if (longStandAloneWeekdayNames == NULL) { longStandAloneWeekdayNames = longWeekdayNames; } jobjectArray shortStandAloneWeekdayNames = getNames(env, dayNames.get(), false, STAND_ALONE, SHORT); if (shortStandAloneWeekdayNames == NULL) { shortStandAloneWeekdayNames = shortWeekdayNames; } setStringArrayField(env, localeData, "longStandAloneMonthNames", longStandAloneMonthNames); setStringArrayField(env, localeData, "shortStandAloneMonthNames", shortStandAloneMonthNames); setStringArrayField(env, localeData, "longStandAloneWeekdayNames", longStandAloneWeekdayNames); setStringArrayField(env, localeData, "shortStandAloneWeekdayNames", shortStandAloneWeekdayNames); ScopedResourceBundle dateTimePatterns(ures_getByKey(gregorian.get(), "DateTimePatterns", NULL, &status)); if (U_SUCCESS(status)) { setStringField(env, localeData, "fullTimeFormat", dateTimePatterns.get(), 0); setStringField(env, localeData, "longTimeFormat", dateTimePatterns.get(), 1); setStringField(env, localeData, "mediumTimeFormat", dateTimePatterns.get(), 2); setStringField(env, localeData, "shortTimeFormat", dateTimePatterns.get(), 3); setStringField(env, localeData, "fullDateFormat", dateTimePatterns.get(), 4); setStringField(env, localeData, "longDateFormat", dateTimePatterns.get(), 5); setStringField(env, localeData, "mediumDateFormat", dateTimePatterns.get(), 6); setStringField(env, localeData, "shortDateFormat", dateTimePatterns.get(), 7); } status = U_ZERO_ERROR; ScopedResourceBundle numberElements(ures_getByKey(root.get(), "NumberElements", NULL, &status)); if (U_SUCCESS(status) && ures_getSize(numberElements.get()) >= 11) { setCharField(env, localeData, "zeroDigit", numberElements.get(), 4); setCharField(env, localeData, "digit", numberElements.get(), 5); setCharField(env, localeData, "decimalSeparator", numberElements.get(), 0); setCharField(env, localeData, "groupingSeparator", numberElements.get(), 1); setCharField(env, localeData, "patternSeparator", numberElements.get(), 2); setCharField(env, localeData, "percent", numberElements.get(), 3); setCharField(env, localeData, "perMill", numberElements.get(), 8); setCharField(env, localeData, "monetarySeparator", numberElements.get(), 0); setCharField(env, localeData, "minusSign", numberElements.get(), 6); setStringField(env, localeData, "exponentSeparator", numberElements.get(), 7); setStringField(env, localeData, "infinity", numberElements.get(), 9); setStringField(env, localeData, "NaN", numberElements.get(), 10); } status = U_ZERO_ERROR; jstring internationalCurrencySymbol = getIntCurrencyCode(env, locale); jstring currencySymbol = NULL; if (internationalCurrencySymbol != NULL) { currencySymbol = ICU_getCurrencySymbolNative(env, NULL, locale, internationalCurrencySymbol); } else { internationalCurrencySymbol = env->NewStringUTF("XXX"); } if (currencySymbol == NULL) { // This is the UTF-8 encoding of U+00A4 (CURRENCY SIGN). currencySymbol = env->NewStringUTF("\xc2\xa4"); } setStringField(env, localeData, "currencySymbol", currencySymbol); setStringField(env, localeData, "internationalCurrencySymbol", internationalCurrencySymbol); ScopedResourceBundle numberPatterns(ures_getByKey(root.get(), "NumberPatterns", NULL, &status)); if (U_SUCCESS(status) && ures_getSize(numberPatterns.get()) >= 3) { setStringField(env, localeData, "numberPattern", numberPatterns.get(), 0); setStringField(env, localeData, "currencyPattern", numberPatterns.get(), 1); setStringField(env, localeData, "percentPattern", numberPatterns.get(), 2); } return JNI_TRUE; }