+ // 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, Boolean.FALSE);
+
+ // Initial formatting, may change when user chooses other locale
+ NUMBER_FORMAT = NumberFormat.getNumberInstance(this.locale);
+ }
+
+ /**
+ * 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
+ * @param notifyBeans Whether to notify other beans
+ */
+ private void changeLocale (final Locale locale, final Boolean notifyBeans) {
+ // Is the locale language_country at least?
+ if (null == locale) {
+ // Throw NPE
+ throw new NullPointerException("locale is null"); //NOI18N
+ } else if (!locale.toString().contains("_")) { //NOI18N
+ // Throw IAE
+ throw new IllegalArgumentException(MessageFormat.format("locale={0} does not have country information set.", locale)); //NOI18N
+ }
+
+ // Set locale + code here
+ this.setLocale(locale);
+ this.setLocaleCode(locale.toString());
+
+ // Inform faces context
+ FacesContext.getCurrentInstance().getViewRoot().setLocale(locale);
+
+ // Notify other beans?
+ if (notifyBeans) {
+ // Fire event
+ this.localeChangeEvent.fire(new LocaleChangeEvent(locale));
+ }
+ }
+
+ /**
+ * Clears this bean
+ */
+ private void clear () {
+ // Clear all fields
+ this.setLocaleCode(null);
+ this.setLocale(null);