List of usage examples for java.util Locale lookup
public static Locale lookup(List<LanguageRange> priorityList, Collection<Locale> locales)
From source file:org.kitodo.dataeditor.ruleset.Labeled.java
/** * Lists the entries alphabetically by the translated label, taking into * account the language preferred by the user for alphabetical sorting. In * the auxiliary map, the keys are sorted by the sequence translated label + * separator + key, to account for the case where multiple keys are * (inadvertently) translated equally.// w w w.ja v a2s. c o m * * @param ruleset * The ruleset. From the ruleset, we learn which language is a * label that does not have a language attribute, that is what is * the default language of the rule set. * @param elements * The items to sort. The function is so generic that you can * sort divisions, keys, and options with it. * @param keyGetter * A function to extract from the element the value used in the * result map as key. * @param labelGetter * A function that allows you to extract the list of labels from * the element and then select the best translated one. * @param priorityList * The list of languages spoken by the user human * @return a map in the order of the best-fitting translated label */ public static <T> LinkedHashMap<String, String> listByTranslatedLabel(Ruleset ruleset, Collection<T> elements, Function<T, String> keyGetter, Function<T, Collection<Label>> labelGetter, List<LanguageRange> priorityList) { Locale sortLocale = Locale.lookup(priorityList, Arrays.asList(Collator.getAvailableLocales())); TreeMap<String, Pair<String, String>> byLabelSorter = sortLocale != null ? new TreeMap<>(Collator.getInstance(sortLocale)) : new TreeMap<>(); for (T element : elements) { String key = keyGetter.apply(element); Labeled labeled = new Labeled(ruleset, key, labelGetter.apply(element)); String label = labeled.getLabel(priorityList); byLabelSorter.put(label + '\037' + key, Pair.of(key, label)); } LinkedHashMap<String, String> result = new LinkedHashMap<>((int) Math.ceil(elements.size() / 0.75)); for (Pair<String, String> entry : byLabelSorter.values()) { result.put(entry.getKey(), entry.getValue()); } return result; }
From source file:org.kitodo.dataeditor.ruleset.Labeled.java
/** * Returns the etiquette in the favorite language. * * @param priorityList/*from w ww . ja v a2s . c o m*/ * wish list for favorite language * @return the etiquette in the favorite language */ String getLabel(List<LanguageRange> priorityList) { Map<Locale, String> labelMap = new HashMap<>(); for (Label label : labels) { Optional<Locale> langAttribute = label.getLanguage(); if (langAttribute.isPresent()) { labelMap.put(langAttribute.get(), label.getValue()); } else { labelMap.put(new Locale(ruleset.getDefaultLang()), label.getValue()); labelMap.put(UNDEFINED_LOCALE, label.getValue()); } } Locale bestMatching = Locale.lookup(priorityList, labelMap.keySet()); if (bestMatching == null) { bestMatching = Locale.lookup(UNDEFINED_LANGUAGE_RANGE, labelMap.keySet()); } return Optional.ofNullable(labelMap.get(bestMatching)).orElse(id); }