@Override public Locale getDefaultLocale() { if (this.defaultLocale == null) { return getFallbackLocale(); } return this.defaultLocale; }
/** * 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(); }
/** * Returns the closest locale for which {@link #isLocaleSupported(Locale)} is true. * <ul> * <li>If the locale has a country specified (fr_CH) the locale without country (fr) will be returned.</li> * <li>If the locale has no country specified (fr) the first locale with the same language but specific country (fr_CH) will be returned.</li> * <li>If this fails the fall-back locale is returned</li> * </ul> * Warning: if you have configured both (fr and fr_CH) this method will jiter between this two values. */ protected Locale getNextLocale(Locale locale) { // if this locale defines a country if (StringUtils.isNotEmpty(locale.getCountry())) { // try to use the language only Locale langOnlyLocale = new Locale(locale.getLanguage()); if (isLocaleSupported(langOnlyLocale)) { return langOnlyLocale; } } // try to find a locale with the same language (ignore the country) for (Iterator<Locale> iter = getLocales().iterator(); iter.hasNext(); ) { Locale otherCountryLocale = iter.next(); // same lang, but not the same country as well or we end up in the loop if (locale.getLanguage().equals(otherCountryLocale.getLanguage()) && !locale.equals(otherCountryLocale)) { return otherCountryLocale; } } return getFallbackLocale(); }
@Test public void testGetLocaleWorksWhenNotInWebContext() { // GIVEN Context ctx = mock(Context.class); MgnlContext.setInstance(ctx); // WHEN Locale result = i18nContentSupport.getLocale(); // THEN assertEquals(i18nContentSupport.getFallbackLocale(), result); }