class DetectLocaleMiddleware implements HTTPMiddleware (View source)

DetectLocaleMiddleware will detect if a locale has been requested (or is default) and is not the current locale, and will redirect the user to that locale if needed.

Will cascade through different checks in order, see "configuration" docs for details. Additionally, detected locales will be set in session and cookies.

Traits

Provides extensions to this object to integrate it with standard config API methods.

Config options

persist_ids string[]

IDs to persist the locale in cookies / session in the front end, CMS, etc

Properties

protected bool $cookiesPersisted

Whether cookies have already been set during setPersistLocale()

Methods

public static 
config()

Get a configuration accessor for this class. Short hand for Config::inst()->get($this->class, .....).

public
mixed
stat(string $name) deprecated

Get inherited config value

public
mixed
uninherited(string $name)

Gets the uninherited value for the given config option

public
$this
set_stat(string $name, mixed $value) deprecated

Update the config value for a given property

public
process(HTTPRequest $request, callable $delegate)

Sets the current locale to the FluentState, provided no previous middleware has set it first

protected
string
getLocale(HTTPRequest $request)

Get the current locale from routing parameters, persistence, browser locale, etc

protected
null|string
getPersistLocale(HTTPRequest $request)

Gets the locale currently set within either the session or cookie.

protected
$this
setPersistLocale(HTTPRequest $request, string $locale)

Specify the locale to persist between sessions, or to use for the locale outside of locale-routed pages (such as in unit tests, custom controllers, etc).

protected
string
getPersistKey()

Get the Fluent locale persistence key. See the "persist_ids" config static.

protected
mixed
getParamLocale(HTTPRequest $request)

Get locale from the query_param

protected
string
getDomainLocale()

Get locale from the domain, if the current domain has exactly one locale

protected
string
getDetectedLocale(HTTPRequest $request)

Use the configured LocaleDetector to guess the locale

protected
string
getDefaultLocale()

Get default locale

Details

static Config_ForClass config()

Get a configuration accessor for this class. Short hand for Config::inst()->get($this->class, .....).

Return Value

Config_ForClass

mixed stat(string $name) deprecated

deprecated 5.0 Use ->config()->get() instead

Get inherited config value

Parameters

string $name

Return Value

mixed

mixed uninherited(string $name)

Gets the uninherited value for the given config option

Parameters

string $name

Return Value

mixed

$this set_stat(string $name, mixed $value) deprecated

deprecated 5.0 Use ->config()->set() instead

Update the config value for a given property

Parameters

string $name
mixed $value

Return Value

$this

HTTPResponse process(HTTPRequest $request, callable $delegate)

Sets the current locale to the FluentState, provided no previous middleware has set it first

{@inheritDoc}

Parameters

HTTPRequest $request
callable $delegate

Return Value

HTTPResponse

protected string getLocale(HTTPRequest $request)

Get the current locale from routing parameters, persistence, browser locale, etc

Parameters

HTTPRequest $request

Return Value

string

protected null|string getPersistLocale(HTTPRequest $request)

Gets the locale currently set within either the session or cookie.

Parameters

HTTPRequest $request

Return Value

null|string

The locale, if available

protected $this setPersistLocale(HTTPRequest $request, string $locale)

Specify the locale to persist between sessions, or to use for the locale outside of locale-routed pages (such as in unit tests, custom controllers, etc).

Not to be confused with the temporary locale assigned with withLocale(). @todo implement this.

Parameters

HTTPRequest $request
string $locale

Locale to assign

Return Value

$this

protected string getPersistKey()

Get the Fluent locale persistence key. See the "persist_ids" config static.

Return Value

string

protected mixed getParamLocale(HTTPRequest $request)

Get locale from the query_param

Parameters

HTTPRequest $request

Return Value

mixed

protected string getDomainLocale()

Get locale from the domain, if the current domain has exactly one locale

Return Value

string

protected string getDetectedLocale(HTTPRequest $request)

Use the configured LocaleDetector to guess the locale

Parameters

HTTPRequest $request

Return Value

string

protected string getDefaultLocale()

Get default locale

Return Value

string