List<Locale.LanguageRange> list1 = new ArrayList<>(); list1.add(new Locale.LanguageRange("*-*")); // Locales List<Locale> list3 = Locale.filter(list1,Arrays.asList(Locale.getAvailableLocales())); list3.forEach(System.out::println);
Collection<Locale> locales = Arrays.asList(Locale.forLanguageTag("en"), Locale.forLanguageTag("en-GB"), Locale.forLanguageTag("en-US")); List<Locale> filtered = Locale.filter( Locale.LanguageRange.parse("en-US;q=1.0,en-GB;q=1.0"), locales); System.out.println("filtered: "+filtered);
List<Locale> filtered = Locale.filter( Locale.LanguageRange.parse("en-US;q=1.0,en-GB;q=1.0"), locales); System.out.println("filtered: "+filtered);
/** * Returns the best matching locale chosen from a set of available locales for the given language ranges. * * @param languageRanges a list of comma-separated language ranges or a list of language ranges in the form of the * "Accept-Language" header defined in * <a href="https://tools.ietf.org/html/rfc2616#section-14.4">RFC * 2616</a> * @param availableLocales available locales to choose from * @return Locale the best matching locale, or {@code null} if nothing matches */ public static Locale getMatchingLocale(String languageRanges, Set<Locale> availableLocales) { if ((languageRanges == null) || languageRanges.isEmpty()) { return null; } List<Locale> matchingLocales; try { matchingLocales = Locale.filter(Locale.LanguageRange.parse(languageRanges), availableLocales); } catch (IllegalArgumentException e) { // languageRanges is ill formed return null; } return matchingLocales.isEmpty() ? null : matchingLocales.get(0); } }
/** * Returns the best matching locale chosen from a set of available locales for the given language ranges. * * @param languageRanges a list of comma-separated language ranges or a list of language ranges in the form of the * "Accept-Language" header defined in * <a href="https://tools.ietf.org/html/rfc2616#section-14.4">RFC * 2616</a> * @param availableLocales available locales to choose from * @return Locale the best matching locale, or {@code null} if nothing matches */ public static Locale getMatchingLocale(String languageRanges, Set<Locale> availableLocales) { if ((languageRanges == null) || languageRanges.isEmpty()) { return null; } List<Locale> matchingLocales; try { matchingLocales = Locale.filter(Locale.LanguageRange.parse(languageRanges), availableLocales); } catch (IllegalArgumentException e) { // languageRanges is ill formed return null; } return matchingLocales.isEmpty() ? null : matchingLocales.get(0); } }