Class LanguageSelector

All Implemented Interfaces:
Iterable<ComponentType>, Channel, ComponentType, Eligible, Manager

public class LanguageSelector extends Component
A component that attempts to derive information about language preferences from requests in the specified scope (usually “/”) and make them available as a LanguageSelector.Selection object associated with the request event using Selection.class as association identifier.

The component first checks if the event has an associated Session and whether that session has a value with key Selection.class. If such an entry exists, its value is assumed to be a LanguageSelector.Selection object which is (re-)used for all subsequent operations. Else, a new LanguageSelector.Selection object is created (and associated with the session, if a session exists).

If the LanguageSelector.Selection represents explicitly set values, it is used as result (i.e. as object associated with the event by Selection.class).

Else, the selector tries to derive the language preferences from the request. It first checks for a cookie with the specified name (see cookieName()). If a cookie is found, its value is used to set the preferred locales. If no cookie is found, the values derived from the Accept-Language header are set as fall-backs.

Whenever the language preferences change (see LanguageSelector.Selection.prefer(Locale)), a cookie with the specified name and a path value set to the scope is created and added to the request’s response. This new cookie is then sent with the response to the browser.

Note that this scheme does not work in a SPA where browser and server only communicate over a WebSocket.

  • Constructor Details

    • LanguageSelector

      Creates a new language selector component with its channel set to itself and the scope set to “/”.

      The handler’s priority is set to 990.

    • LanguageSelector

      public LanguageSelector(String scope)
      Creates a new language selector component with its channel set to itself and the scope set to the given value.

      The handler’s priority is set to 990.

      Parameters:
      scope - the scope
    • LanguageSelector

      public LanguageSelector(Channel componentChannel)
      Creates a new language selector component with its channel set to the given channel and the scope to “/”.

      The handler’s priority is set to 990.

      Parameters:
      componentChannel - the component channel
    • LanguageSelector

      public LanguageSelector(Channel componentChannel, String scope)
      Creates a new language selector component with its channel set to the given channel and the scope to the given scope.

      The handler’s priority is set to 990.

      Parameters:
      componentChannel - the component channel
      scope - the scope
    • LanguageSelector

      public LanguageSelector(Channel componentChannel, String scope, int priority)
      Creates a new language selector component with its channel set to the given channel and the scope to the given scope.

      The handler’s priority is set to the given value.

      Parameters:
      componentChannel - the component channel
      scope - the scope
      priority - the priority
  • Method Details