Strategies are the base of SlmLocale. They all identify the locale in a unique way. You can combine strategies to have a cascade of search mechanisms to find the appropriate locale for your application. Via strategies it is also possible to create your custom locale loader, for example based on a user's profile or other detection mechanism.
The AcceptLanguageStrategy
is a class which checks for the presence of the HTTP header Accept-Language
in the request. The strategy checks this header and if present, matches the values against the provided list of supported locales. If there is a match, the best matching locale will be returned.
This strategy will not perform a redirect when a locale is found, not has any effect on the URI that will be build with the view helpers. This is an excellent strategy to use as a fallback.
The CookieStrategy
checks the value of a cookie to select the locale. This is very useful when visitors come back and you want them to present the site in the same locale as when they left it the previous time.
Mind you need to have the Query strategy selected with a higher priority than the Cookie strategy to modify the contents of the cookie!
The query strategy is a very simple strategy to use in links to change the locale. It checks for a query parameter (default lang
) to change the locale. After this locale is set, a redirect will happen if you have the uri path or host strategy enabled, to redirect the client to the appropriate url.
The query is quite simple to construct and therefore allows you to enforce a locale in any of your own urls. Just place ?lang=my-LOCALE
behind it to change the application's locale.
The path segment uses the first part of the path which does not belong to the base path. This means if your site runs under http://example.com, then /en-US/
selects the locale en-US. If your site runs under http://example.com/my-app/, then /my-app/en-US/
selects the locale.
Furthermore, this strategy updates the router's base path with the locale appended. This means all links built with Laminas\Navigation or with the url()
view helper, will have the locale prepended automatically.
The strategy supports aliases, so you can use en
as an alias for en-US and nl
for nl-NL. However, a few bugs are still present at this moment so it is not advised to use these aliases together with this strategy.
The host name checks a part of the host name. This can be either a subdomain or the TLD. It does support aliases so you can map .com
to en-US and .co.uk
to en-GB.
This strategy always needs the option "domain" where you provide a key :locale
to be substituted with the found locale.
'strategies' => [
[
'name' => 'host',
'options' => ['domain' => 'www.example.:locale'], // For a TLD
// Or:
'options' => ['domain' => ':locale.example.com'], // For a subdomain
],
],
To map TLDs to locales, use the aliases array.
'strategies' => [
[
'name' => 'host',
'options' => [
'domain' => 'www.example.:locale'
'aliases' => ['com' => 'en-US', 'co.uk' => 'en-GB'],
],
],
],
If you use an Asset Manager (f. e. https://github.com/RWOverdijk/AssetManager) you may face the problem of URIs of your assets getting rewritten to a locale-dependent URI (f. e. http://example.com/de/css/style.css
). Depending how your webserver is configured, this leads to 404 errors.
To cope with such problems use AssetStrategy:
'strategies' => [
[
'name' => SlmLocale\Strategy\AssetStrategy::class,
'options' => [
'file_extensions' => [
'css', 'js'
]
]
],
'query',
[
'name' => \SlmLocale\Strategy\UriPathStrategy::class,
'options' => [
'redirect_when_found' => true,
'aliases' => array('de' => 'de_DE', 'en' => 'en_GB'),
]
],
'cookie',
'acceptlanguage'
],
The file_extensions
array should contain all file endings you use for your assets. URIs of files which have one of the specified extensions will not be rewritten (the example above excludes css
and js
files).
Important: Make sure to add AssetStrategy first / have AssetStrategy added with highest priority in your config. Otherwise some other strategies may rewrite the URI of an asset.