Class SessionLocaleResolver

java.lang.Object
com.oorian.i18n.SessionLocaleResolver
All Implemented Interfaces:
LocaleResolver

public class SessionLocaleResolver extends Object implements LocaleResolver
Resolves the locale from the user's session, allowing per-user locale overrides.

When a locale has been explicitly stored in the session via setLocale(Locale), this resolver returns that locale. Otherwise it returns null, allowing a CompositeLocaleResolver to fall through to the next resolver (typically AcceptLanguageLocaleResolver).

This resolver enables the common pattern where users select a preferred language in their profile or settings, which then takes precedence over the browser's Accept-Language header for subsequent requests.

Usage:


 // Set the user's preferred locale (e.g., from a language selector)
 SessionLocaleResolver.setLocale(Locale.GERMANY);

 // Retrieve the session locale (null if not explicitly set)
 Locale sessionLocale = SessionLocaleResolver.getLocale();

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

    • SessionLocaleResolver

      public SessionLocaleResolver()
      Creates a new session-based locale resolver.
  • Method Details

    • resolveLocale

      public Locale resolveLocale(OorianHttpRequest request)
      Resolves the locale from the current session.

      Returns the locale stored in the session attribute, or null if no locale has been explicitly set via setLocale(Locale).

      Specified by:
      resolveLocale in interface LocaleResolver
      Parameters:
      request - the current HTTP request
      Returns:
      the session locale, or null if none is stored
    • setLocale

      public static void setLocale(Locale locale)
      Stores a locale override in the current session.

      Once set, the SessionLocaleResolver will return this locale for all subsequent requests in the same session, until it is cleared via clearLocale().

      Parameters:
      locale - the locale to store in the session
    • getLocale

      public static Locale getLocale()
      Returns the locale stored in the current session.
      Returns:
      the session locale, or null if none has been set
    • clearLocale

      public static void clearLocale()
      Removes the locale override from the current session.

      After clearing, locale resolution will fall through to the next resolver in the chain (typically Accept-Language detection).