+ // Get default locale
+ Locale defaultLocale = FacesContext.getCurrentInstance().getApplication().getDefaultLocale();
+ System.out.println("defaultLocale=" + defaultLocale);
+
+ // Add it to list
+ this.getSupportedLocales().put(defaultLocale.toString(), defaultLocale);
+
+ // Get iterator from faces context
+ Iterator<Locale> iterator = FacesContext.getCurrentInstance().getApplication().getSupportedLocales();
+
+ // Add all locales
+ while (iterator.hasNext()) {
+ // Get next locale
+ Locale supportedLocale = iterator.next();
+ System.out.println("supportedLocale=" + supportedLocale);
+
+ // Add it
+ this.getSupportedLocales().put(supportedLocale.toString(), supportedLocale);
+ }
+
+ // Get locale instance from request (e.g. browser)
+ Locale requestLocale = FacesContext.getCurrentInstance().getExternalContext().getRequestLocale();
+
+ // Is no country code found?
+ if (requestLocale.getCountry().isEmpty()) {
+ // Then try to find one
+ System.out.println("Request locale is without country information, looking up in map ...");
+
+ // Get language from it
+ String language = requestLocale.getLanguage();
+ Boolean found = Boolean.FALSE;
+
+ // Iterate over whole map
+ for (Map.Entry<String, Locale> entry : this.getSupportedLocales().entrySet()) {
+ String languageCode = entry.getKey();
+ Locale foundLocale = entry.getValue();
+ System.out.println(MessageFormat.format("languageCode={0},language={1}", languageCode, language));
+
+ // Does the language match?
+ if (languageCode.startsWith(language)) {
+ // Is found, take it as request locale
+ requestLocale = foundLocale;
+ found = Boolean.TRUE;
+ break;
+ }
+ }
+
+ // Nothing found?
+ if (!found) {
+ // Then set default locale
+ requestLocale = defaultLocale;
+ }
+ }
+
+ // Change locale, may set same back in faces context, but triggers event
+ this.changeLocale(requestLocale);
+ }
+
+ /**
+ * Changes current locale in this controller and faces context to given.
+ * This method makes sure that locales with at least language and country
+ * information are being changed to.
+ * <p>
+ * @param locale New locale to set
+ */
+ private void changeLocale (final Locale locale) {
+ // Is the locale language_country at least?
+ if (null == locale) {
+ // Throw NPE
+ throw new NullPointerException("locale is null");
+ } else if (!locale.toString().contains("_")) {
+ // Throw IAE
+ throw new IllegalArgumentException(MessageFormat.format("locale={0} does not have country information set.", locale));
+ }
+
+ System.out.println("Changing locale to " + locale + " ...");
+
+ // Set locale + code here
+ this.setLocale(locale);
+ this.setLocaleCode(locale.toString());
+
+ // Inform faces context
+ FacesContext.getCurrentInstance().getViewRoot().setLocale(locale);