Plug that discovers and sets the locale for the current process.
The locale can be derived from the accept-language header, a query parameter, a URL parameter, a body parameter, a cookie, the route, the session, or the hostname TLD. Sources are checked in the order specified by the :from option, and the first successful match wins.
When a locale is found, Localize.put_locale/1 is always called to set the process locale. If Gettext backends are configured, the locale is also set on each backend.
If a locale is found then conn.private[:localize_locale] is also set. It can be retrieved with Localize.Plug.PutLocale.get_locale/1.
Options
:fromis a list specifying where in the request to look for the locale. The default is[:session, :accept_language, :query, :path, :route]. The valid options are::accept_languagewill parse theaccept-languageheader and find the best matched configured locale.:pathwill look for a locale by examiningconn.path_params.:querywill look for a locale by examiningconn.query_params.:bodywill look for a locale by examiningconn.body_params.:cookiewill look for a locale in the request cookie(s).:sessionwill look for a locale in the session.:routewill look for a locale in the route that was matched under the keyprivate.:localize_locale.:hostwill attempt to resolve a locale from the hostname top-level domain.{Module, function, args}in which case the indicated function will be called. If it returns{:ok, locale}then the locale is set.localemust be aLocalize.LanguageTag.t/0.{Module, function}in which case the function is called withconnandoptionsas its two arguments.
:defaultis the default locale to set if no locale is found by other configured methods. It can be a string like"en"or aLocalize.LanguageTagstruct. It may also be:noneto indicate that no locale is to be set by default. Lastly, it may also be a{Module, function, args}or{Module, function}tuple. The default isLocalize.default_locale/0.:gettextis a Gettext backend module or a list of Gettext backend modules on which the locale will be set. The default is[](no Gettext backends).:paramis the name of the parameter to look for in the query, path, body, or cookie. The default is"locale".
Examples
plug Localize.Plug.PutLocale,
from: [:query, :path, :body, :cookie, :accept_language],
param: "locale",
gettext: MyApp.Gettext
plug Localize.Plug.PutLocale,
from: [:route, :session, :accept_language],
gettext: [MyApp.Gettext, MyOtherApp.Gettext]
Summary
Functions
Returns the locale set by Localize.Plug.PutLocale.
Attempts to resolve a locale from a hostname's top-level domain.
Returns the name of the session key used to store the locale.
Functions
Returns the locale set by Localize.Plug.PutLocale.
Arguments
connis aPlug.Conn.t/0.
Returns
- A
Localize.LanguageTag.t/0ornil.
Attempts to resolve a locale from a hostname's top-level domain.
Extracts the TLD, validates it as a territory code, and returns the best matching locale for that territory.
Arguments
hostis a hostname string (e.g., "example.co.uk").
Returns
{:ok, Localize.LanguageTag.t()}or{:error, reason}
Returns the name of the session key used to store the locale.
Examples
iex> Localize.Plug.PutLocale.session_key()
"localize_locale"