Class LanguageSelector
- All Implemented Interfaces:
Iterable<ComponentType>
,Channel
,ComponentType
,Eligible
,Manager
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.
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic final class
Represents a locale selection.Nested classes/interfaces inherited from interface org.jgrapes.core.Channel
Channel.Default
-
Field Summary
-
Constructor Summary
ConstructorDescriptionCreates a new language selector component with its channel set to itself and the scope set to “/”.LanguageSelector
(String scope) Creates a new language selector component with its channel set to itself and the scope set to the given value.LanguageSelector
(Channel componentChannel) Creates a new language selector component with its channel set to the given channel and the scope to “/”.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.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. -
Method Summary
Modifier and TypeMethodDescriptionstatic Locale
associatedLocale
(Associator assoc) Convenience method to retrieve a locale from an associator.Returns the max age of the cookie used to store the preferences.Returns the cookie name.void
onProtocolSwitchAccepted
(ProtocolSwitchAccepted event, IOSubchannel channel) Handles a procotol switch by associating the language selection with the channel.void
onRequest
(Request.In event) Associates the event with aLanguageSelector.Selection
object usingSelection.class
as association identifier.Returns the value of the same site attribute.setCookieMaxAge
(Duration maxAge) Sets the max age of the cookie used to store the preferences.setCookieName
(String cookieName) Sets the name of the cookie used to store the locale.setSameSiteAttribute
(Converters.SameSiteAttribute attribute) Sets the same site attribute for the locale cookie defaults toLax
.Methods inherited from class org.jgrapes.core.Component
channel, component, defaultCriterion, isEligibleFor, setName
Methods inherited from class org.jgrapes.core.internal.ComponentVertex
activeEventPipeline, addHandler, attach, channelReplacements, children, componentPath, componentVertex, detach, fire, initComponentsHandlers, iterator, name, newEventPipeline, newEventPipeline, parent, registerAsGenerator, root, toString, unregisterAsGenerator
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
Methods inherited from interface java.lang.Iterable
forEach, spliterator
-
Constructor Details
-
LanguageSelector
public 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
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
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
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 channelscope
- the scope
-
LanguageSelector
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 channelscope
- the scopepriority
- the priority
-
-
Method Details
-
setCookieName
Sets the name of the cookie used to store the locale.- Parameters:
cookieName
- the cookie name to use- Returns:
- the locale selector for easy chaining
-
cookieName
Returns the cookie name.Defaults to the class name.
- Returns:
- the cookie name
-
setCookieMaxAge
Sets the max age of the cookie used to store the preferences.Defaults to one year.
- Parameters:
maxAge
- the max age- Returns:
- the language selector
- See Also:
-
cookieMaxAge
Returns the max age of the cookie used to store the preferences.- Returns:
- the duration
-
setSameSiteAttribute
Sets the same site attribute for the locale cookie defaults toLax
.- Parameters:
attribute
- the attribute- Returns:
- the language selector
-
sameSiteAttribute
Returns the value of the same site attribute.- Returns:
- the same site attribute
-
onRequest
Associates the event with aLanguageSelector.Selection
object usingSelection.class
as association identifier.Does nothing if the request has already been fulfilled.
- Parameters:
event
- the event
-
onProtocolSwitchAccepted
@Handler(priority=1000) public void onProtocolSwitchAccepted(ProtocolSwitchAccepted event, IOSubchannel channel) Handles a procotol switch by associating the language selection with the channel.- Parameters:
event
- the eventchannel
- the channel
-
associatedLocale
Convenience method to retrieve a locale from an associator.- Parameters:
assoc
- the associator- Returns:
- the locale
-