import com.ibm.icu.text.DateFormat; import com.ibm.icu.util.Calendar; import com.ibm.icu.util.ULocale; ... ULocale locale = new ULocale("fa_IR@calendar=persian"); Calendar calendar = Calendar.getInstance(locale); DateFormat df = DateFormat.getDateInstance(DateFormat.FULL, locale); System.out.println(df.format(calendar));
ULocale uLocale = ULocale.forLanguageTag("pl-PL"); ResourceBundle resources = ResourceBundle.getBundle( "path.to.messages", uLocale.toLocale()); PluralRules pluralRules = PluralRules.forLocale(uLocale); double[] numbers = { 0, 1, 1.5, 2, 2.5, 3, 4, 5, 5.5, 11, 12, 23 }; for (double number : numbers) { String resourceKey = "some.message.plural_form." + pluralRules.select(number); String message = "!" + resourceKey + "!"; try { message = resources.getString(resourceKey); System.out.println(format(message, uLocale, number)); } catch (MissingResourceException e) { // Log this } }
private static ULocale addLikelySubtagsWithDefaults(ULocale locale) { ULocale maximized = ULocale.addLikelySubtags(locale); if (maximized == locale) { // If already in maximal form or no data available for maximization, make sure // language, script and region are not undefined (ICU4J expects all are defined). String language = locale.getLanguage(); String script = locale.getScript(); String region = locale.getCountry(); if (language.isEmpty() || script.isEmpty() || region.isEmpty()) { return new ULocale(toLocaleId(language, script, region)); } } return maximized; }
private static Set<String> addDerivedLanguages(List<String> available) { HashSet<String> availableSet = new HashSet<>(available); HashSet<String> derivedSet = new HashSet<>(available); for (ULocale locale : ULocale.getAvailableLocales()) { String languageTag = locale.toLanguageTag(); if (derivedSet.contains(languageTag)) { continue; if (!(locale.getVariant().isEmpty() && locale.getUnicodeLocaleKeys().isEmpty())) { String language = locale.getLanguage(); if (availableSet.contains(language)) { derivedSet.add(languageTag); continue; String script = locale.getScript(); if (!script.isEmpty()) { String languageScript = language + "-" + script; String country = locale.getCountry(); if (!country.isEmpty()) { String languageCountry = language + "-" + country;
/** * For subclass use. Subclasses will generally not * work correctly unless they manipulate the delegate. */ protected DateFormat() { this.dateFormat = java.text.DateFormat.getDateTimeInstance( java.text.DateFormat.SHORT, java.text.DateFormat.SHORT, ULocale.getDefault(Category.FORMAT).toLocale()); }
private void initializeNLExtensions() { IPreferenceStore store = WorkbenchPlugin.getDefault().getPreferenceStore(); if (!store.isDefault(IPreferenceConstants.NL_EXTENSIONS)) { String nlExtensions = store.getString(IPreferenceConstants.NL_EXTENSIONS); ULocale.setDefault(Category.FORMAT, new ULocale(ULocale.getDefault(Category.FORMAT) .getBaseName() + nlExtensions)); } }
private BaseLocale base() { if (baseLocale == null) { String language = getLanguage(); if (equals(ULocale.ROOT)) { language = ""; } baseLocale = BaseLocale.getInstance(language, getScript(), getCountry(), getVariant()); } return baseLocale; }
/** Returns whether a locale, given as a string in the ICU syntax, is RTL. */ public static boolean isRtlLanguage(String locale) { try { return UScript.isRightToLeft( UCharacter.getPropertyValueEnum( UProperty.SCRIPT, ULocale.addLikelySubtags(new ULocale(locale)).getScript())); } catch (IllegalArgumentException e) { return false; } }
/** * Returns the locale ID localized for display in the provided locale. * This is a cover for the ICU4C API. * @param localeID the locale whose name is to be displayed. * @param displayLocale the locale in which to display the locale name. * @return the localized locale name. * @stable ICU 3.0 */ public static String getDisplayName(String localeID, ULocale displayLocale) { return getDisplayNameInternal(new ULocale(localeID), displayLocale); }
@Override public Locale create(String localeId) { ULocale uLocale = new ULocale(localeId); return create(uLocale.getBaseName(), uLocale.getDisplayNameWithDialect(uLocale), uLocale.getDisplayNameWithDialect(ULocale.ENGLISH)); }
/** * Creates a new cardinal-number <code>PluralFormat</code> for a given * {@link java.util.Locale}. * @param locale the <code>PluralFormat</code> will be configured with * rules for this locale. This locale will also be used for standard * number formatting. * @stable ICU 54 */ public PluralFormat(Locale locale) { this(ULocale.forLocale(locale)); }
/** * Validates a given language tag. Empty and null values are considered false. * @param tag * @return true if a valid language tag, otherwise false */ public static boolean isValidLanguageTag(String tag){ try { if (tag.equals("x-w3c-test")) return true; // hack for testing :( ULocale locale = ULocale.forLanguageTag(tag); if (locale.toLanguageTag() == null) return false; // We don't accept "x" extensions (private use tags) if(locale.getExtension("x".charAt(0))!=null) return false; if (!locale.toLanguageTag().equalsIgnoreCase(tag)) return false; return true; } catch (Exception e) { return false; } }
/** * Returns a locale's language localized for display in the provided locale. * This is a cover for the ICU4C API. * @param localeID the id of the locale whose language will be displayed * @param displayLocaleID the id of the locale in which to display the name. * @return the localized language name. * @stable ICU 3.0 */ public static String getDisplayLanguage(String localeID, String displayLocaleID) { return getDisplayLanguageInternal(new ULocale(localeID), new ULocale(displayLocaleID), false); }
/** * {@icu} Returns the locale ID localized for display in the provided locale. * If a dialect name is present in the locale data, then it is returned. * This is a cover for the ICU4C API. * @param localeID the locale whose name is to be displayed. * @param displayLocale the locale in which to display the locale name. * @return the localized locale name. * @stable ICU 4.4 */ public static String getDisplayNameWithDialect(String localeID, ULocale displayLocale) { return getDisplayNameWithDialectInternal(new ULocale(localeID), displayLocale); }
/** * Gets the language code for this LocaleId. * @return the language code. */ public String getLanguage() { return uLocale.getLanguage(); }
boolean matches(ULocale ulocale) { if (lang != null && !lang.equals(ulocale.getLanguage())) { return false; } if (script != null && !script.equals(ulocale.getScript())) { return false; } if (region != null && !region.equals(ulocale.getCountry())) { return false; } return true; }
/** * Gets the BCP-47 language tag for this LocaleId. * @return the BCP-47 language tag for the given LocaleId. */ public String toBCP47() { if (uLocale == ICU_ALL) { return LOCALE_ID_ALL; } return uLocale.toLanguageTag(); }
private String getClosestW20Locale(String locale) { if (supportedLocales.contains(locale)) { return locale; } else { return localeMatcher.getBestMatch(ULocale.forLanguageTag(locale)).toLanguageTag(); } }
private PluralRules createPluralRules() { ULocale locale = ULocale.forLanguageTag(this.locale); PluralType pluralType = "cardinal".equals(type) ? PluralType.CARDINAL : PluralType.ORDINAL; return PluralRules.forLocale(locale, pluralType); }
/** * Gets an array of the LocaleId objects for all the Java locales installed on the system. * @return an array of the LocaleId objects for all the available Java locales. */ static public LocaleId[] getAvailableLocales() { ULocale[] jlocales = ULocale.getAvailableLocales(); LocaleId[] locIds = new LocaleId[jlocales.length]; for (int i = 0; i < jlocales.length; i++) { locIds[i] = LocaleId.fromString(jlocales[i].toLanguageTag()); } return locIds; }