/** * Create a new language matcher from a String form. The highest-weighted * language is the default. * * @param languagePriorityListString String form of LanguagePriorityList * @stable ICU 4.4 */ public LocaleMatcher(String languagePriorityListString) { this(LocalePriorityList.add(languagePriorityListString).build()); }
return new LocalePriorityList(Collections.unmodifiableMap(temp));
/** * Internal testing function; may expose API later. * @param languagePriorityList LocalePriorityList to match * @param matcherData Internal matching data * @internal * @deprecated This API is ICU internal only. */ @Deprecated public LocaleMatcher(LocalePriorityList languagePriorityList, LanguageMatcherData matcherData, double threshold) { this.matcherData = matcherData == null ? defaultWritten : matcherData.freeze(); for (final ULocale language : languagePriorityList) { add(language, languagePriorityList.getWeight(language)); } processMapping(); Iterator<ULocale> it = languagePriorityList.iterator(); defaultLanguage = it.hasNext() ? it.next() : null; this.threshold = threshold; }
/** * Get the best match for a LanguagePriorityList * * @param languageList list to match * @return best matching language code * @stable ICU 4.4 */ public ULocale getBestMatch(LocalePriorityList languageList) { double bestWeight = 0; ULocale bestTableMatch = null; double penalty = 0; OutputDouble matchWeight = new OutputDouble(); for (final ULocale language : languageList) { final ULocale matchLocale = getBestMatchInternal(language, matchWeight); final double weight = matchWeight.value * languageList.getWeight(language) - penalty; if (weight > bestWeight) { bestWeight = weight; bestTableMatch = matchLocale; } penalty += 0.07000001; } if (bestWeight < threshold) { bestTableMatch = defaultLanguage; } return bestTableMatch; }
/** * @internal * @deprecated This API is ICU internal only. */ @Deprecated public ULocale getBestMatch(ULocale... ulocales) { return getBestMatch(LocalePriorityList.add(ulocales).build()); }
/** * Convenience method: Get the best match for a LanguagePriorityList * * @param languageList String form of language priority list * @return best matching language code * @stable ICU 4.4 */ public ULocale getBestMatch(String languageList) { return getBestMatch(LocalePriorityList.add(languageList).build()); }
private static LocaleMatcher CreateDefaultMatcher() { LocalePriorityList priorityList = LocalePriorityList.add(ULocale.ROOT).build(); return new LocaleMatcher(priorityList); }
@Inject public I18nConfigurationHandler(Application application, LocaleService localeService) { I18nConfig i18nConfig = application.getConfiguration().get(I18nConfig.class); List<String> additionalLocales = i18nConfig.getAdditionalLocales(); supportedLocales = sortLanguages( W20_BUILTIN_LOCALES, additionalLocales.toArray(new String[additionalLocales.size()]) ); localeMatcher = new LocaleMatcher(LocalePriorityList.add(Joiner.on(',').join(supportedLocales)).build()); this.localeService = localeService; }
private ULocale getClosestULocale(String locale) { if (isAvailable(locale)) { return new ULocale(locale); } String defaultLocale = getDefaultLocale(); LocalePriorityList.Builder builder = null; if (defaultLocale != null) { builder = LocalePriorityList.add(defaultLocale); } for (String availableLocale : getAvailableLocales()) { if (defaultLocale == null || !defaultLocale.equals(availableLocale)) { if (builder == null) { builder = LocalePriorityList.add(availableLocale); } builder.add(availableLocale); } } if (builder != null) { LocaleMatcher localeMatcher = new LocaleMatcher(builder.build()); return localeMatcher.getBestMatch(locale); } else { return null; } } }
ULocale getClosestULocale(String locale) { String defaultLocale = getDefaultLocale(); if (isAvailable(locale)) { return new ULocale(locale); } LocalePriorityList.Builder builder = null; if (defaultLocale != null) { builder = LocalePriorityList.add(defaultLocale); } for (String availableLocale : getAvailableLocales()) { if (defaultLocale == null || !defaultLocale.equals(availableLocale)) { if (builder == null) { builder = LocalePriorityList.add(availableLocale); } builder.add(availableLocale); } } if (builder != null) { LocaleMatcher localeMatcher = new LocaleMatcher(builder.build()); return localeMatcher.getBestMatch(locale); } else { return null; } } }