/** * Uses {@link #getNextLocale(Locale)} to find the next locale. If the returned locale is in the * checkedLocales set it falls back to the fall-back locale. If the fall-back locale itself is * passed to the method, the method returns null to signal the end of the chain. */ protected Locale getNextContentLocale(Locale locale, Set<Locale> checkedLocales) { if (locale.equals(getFallbackLocale())) { return null; } Locale candidate = getNextLocale(locale); if (!checkedLocales.contains(candidate)) { return candidate; } return getFallbackLocale(); }
/** * Extracts the language from the uri. */ @Override public Locale determineLocale() { Locale locale; locale = onDetermineLocale(); // depending on the implementation the returned local can be null (not defined) if (locale == null) { locale = getDefaultLocale(); } // if we have a locale but it is not supported we try to get the closest locale if (!isLocaleSupported(locale)) { locale = getNextLocale(locale); } return locale; }
@Test public void getNextLocaleFromLanguageLocale() { // GIVEN Map<String, Locale> locales = new HashMap<String, Locale>(); locales.put("fr_CA", Locale.CANADA_FRENCH); i18nContentSupport.setLocales(locales); // WHEN Locale nextLocale = i18nContentSupport.getNextLocale(Locale.FRENCH); // THEN assertEquals(Locale.CANADA_FRENCH, nextLocale); }
@Test public void getNextLocaleFromCountrySpecificLocale() { // GIVEN Map<String, Locale> locales = new HashMap<String, Locale>(); locales.put("fr", Locale.FRENCH); i18nContentSupport.setLocales(locales); // WHEN Locale nextLocale = i18nContentSupport.getNextLocale(Locale.CANADA_FRENCH); // THEN assertEquals(Locale.FRENCH, nextLocale); }
@Test public void getNextLocaleWithFallbackLocale() { // GIVEN i18nContentSupport.setFallbackLocale(Locale.ENGLISH); Map<String, Locale> locales = new HashMap<String, Locale>(); locales.put("fr", Locale.FRENCH); i18nContentSupport.setLocales(locales); // WHEN Locale nextLocale = i18nContentSupport.getNextLocale(Locale.JAPANESE); // THEN assertEquals(Locale.ENGLISH, nextLocale); }