Class LanguageSwitcher

java.lang.Object
com.oorian.i18n.LanguageSwitcher

public final class LanguageSwitcher extends Object
Utility for dynamically switching the user's locale at runtime.

LanguageSwitcher coordinates three actions when changing the locale:

  1. Updates the thread-local locale via OorianLocale.set(Locale)
  2. Persists the locale to the session via SessionLocaleResolver.setLocale(Locale)
  3. Fires a LocaleChangeEvent to notify registered listeners

Pages implementing LocaleChangeListener can register for locale change events and re-render their content accordingly.

Usage:


 // Switch to German
 LanguageSwitcher.switchLocale(Locale.GERMANY);

 // Switch to Arabic (triggers RTL layout update)
 LanguageSwitcher.switchLocale(new Locale("ar"));

 // Clear override (revert to browser Accept-Language detection)
 LanguageSwitcher.clearLocale();
 
Since:
2.1
Version:
1.0
Author:
Marvin P. Warble Jr.
See Also:
  • Method Details

    • switchLocale

      public static void switchLocale(Locale newLocale)
      Switches the current user's locale and notifies all registered listeners.

      This method updates the thread-local locale, persists the preference to the session, and fires a LocaleChangeEvent. All pages implementing LocaleChangeListener that are registered on the current session will be notified.

      Parameters:
      newLocale - the locale to switch to
    • clearLocale

      public static void clearLocale()
      Clears the locale override and reverts to the default resolution strategy.

      After clearing, subsequent requests will resolve the locale from the Accept-Language header or other configured resolvers. A LocaleChangeEvent is fired with the reverted locale.