Skip to content
juriansluiman edited this page Sep 15, 2012 · 4 revisions

This page is an "RFC" for all the strategies from SlmLocale. The goal from SlmLocale is to detect a user's locale automatically with various means. Every mean is called a strategy and works through events. A "detect" event is triggered and strategies are attached as listeners. They can attach at specific priorities, to make sure one strategy has preference over another one.

If a strategy returns a locale, that first one will be picked. A strategy can listen to the "detect" event, but if it doesn't return anything, the next strategy is triggered. In Zend Framework 2 terms, short circuiting of events is used to let the first detection mechanism win.

SlmLocale provides the following strategies:

  1. HTTP Accept-Language header
  2. Cookie
  3. Sub domain
  4. Top level domain
  5. Uri path segment

HTTP Accept-Language header

The strategy uses the Accept-Language header to pick a locale.

Checks if the HTTP header is available and a list of supported routes is given.

Works with all listed locales prioritized from the header, looping through them. The first element in the list which matches a supported locale, will be returned.

Cookie

The strategy uses a cookie object to store the locale. When a user returns, the locale from a previous session can be selected.

Checks if an HTTP request is made and the Cookie header is available.

Works with the value from the cookie. If the value matches a supported locale, it will be returned.

Stores the found locale in the cookie when a locale is detected (so also when the cookie strategy hasn't found a locale, but another strategy has, that found one is put into the cookie).

Sub domain

Uses the host name from the server and matches the locale to a part of that name.

Checks if an HTTP request is made and the host name is available.

Works with a pattern supplied (for example, :locale.example.com), the :locale part is replaced with a regex part and the complete host name is put into the expression. If a match is found, the match is validated against a list of supported locales and a list of aliases. The aliases are a key/value list of possible matches and locales. This makes it possible to have domain names like nld.example.com and deutsch.example.com where nld is an alias for nl-NL and deutsch an alias for de-DE.

Redirects to the correct sub domain. The redirect is optionally, which means you might hit nld.example.com and the cookie returns early the found locale de-DE. When turned off, the strategy will do nothing. When turned on, the strategy will redirect the user to deutsch.example.com. A second flag is available for redirection of default locale. When the default locale is nl-NL and example.com is used as host name, the strategy can redirect to nld.example.com when redirecting for default locales is turned on and it will do nothing of the flag is turned off.

Top level domain

Uses the host name from the server and matches the locale to a part of that name.

Checks if an HTTP request is made and the host name is available.

Works with a pattern supplied (for example, example.:locale), the :locale part is replaced with a regex part and the complete host name is put into the expression. If a match is found, the match is validated against a list of aliases. The aliases are a key/value list of possible matches and locales. This makes it possible to have domain names like example.nl and example.de where nl is an alias for nl-NL and de an alias for de-DE.

Redirects to the correct top level domain. The redirect is optionally, which means you might hit example.nl and the cookie returns early the found locale de-DE. When turned off, the strategy will do nothing. When turned on, the strategy will redirect the user to example.de. A second flag is available for redirection of default locale. When the default locale is nl-NL and example.com is used as host name, the strategy can redirect to example.nl when redirecting for default locales is turned on and it will do nothing of the flag is turned off.

Uri path segment

Uses the uri from the HTTP request and matches the locale to a segment of the path.

Checks if an HTTP request is made and the uri is available.

Works with a check for the first segment of the path (so nl in example.com/nl/foo/bar?baz). The part is compared against a list of supported locales and a list of aliases. The aliases are a key/value list of possible matches and locales. This makes it possible to have domain names like example.com/nederland and example.com/deutschland where nederland is an alias for nl-NL and deutschland an alias for de-DE.

Redirects to the correct path segment. The redirect is optionally, which means you might hit example.com/nl and the cookie returns early the found locale de-DE. When turned off, the strategy will do nothing. When turned on, the strategy will redirect the user to example.com/de. A second flag is available for redirection of default locale. When the default locale is nl-NL and example.com is used as host name, the strategy can redirect to example.com/nl when redirecting for default locales is turned on and it will do nothing of the flag is turned off.

Clone this wiki locally