|
|
@@ -12,10 +12,21 @@ Set up a default language in your configs:
|
|
|
```
|
|
|
Any `Configure::read('Config.language')` or `I18n::locale()` call should return that default language.
|
|
|
|
|
|
-## Session based language switch
|
|
|
+
|
|
|
+## Detecting the user language
|
|
|
To detect and switch based on language, you can leverage the Language class.
|
|
|
Either you manually use `findMatches()` to sort through, or you use the convenience method `findFirstMatch()`:
|
|
|
```php
|
|
|
+use Tools\Utility\Language;
|
|
|
+
|
|
|
+$language = Language::findFirstMatch(['de', 'en']);
|
|
|
+```
|
|
|
+It will use the language with the highest weight supported by the user agent data provided.
|
|
|
+
|
|
|
+
|
|
|
+## Session based language switch
|
|
|
+In your AppController you can now do:
|
|
|
+```php
|
|
|
/**
|
|
|
* @return void
|
|
|
*/
|
|
|
@@ -26,7 +37,7 @@ Either you manually use `findMatches()` to sort through, or you use the convenie
|
|
|
$language = $this->request->session()->read('Config.language');
|
|
|
// Then check the browser preference for the whitelisted languages
|
|
|
if (!$language) {
|
|
|
- $language = Language::findFirstMatch(['de', 'en']);
|
|
|
+ $language = Language::findFirstMatch(Configure::read('Config.supportedLanguages'));
|
|
|
}
|
|
|
// Overwrite the system default
|
|
|
if ($language) {
|
|
|
@@ -59,9 +70,7 @@ new `language` param.
|
|
|
|
|
|
Switch out the above session check then with
|
|
|
```php
|
|
|
-if (!$language) {
|
|
|
- $language = $this->request->getParam('language');
|
|
|
-}
|
|
|
+$language = $this->request->getParam('language');
|
|
|
```
|
|
|
(before CakePHP 3.4+ it would be `param()`)
|
|
|
|
|
|
@@ -86,6 +95,8 @@ You can of course also make the regexp here `'language' => 'de|en'` for all lang
|
|
|
Only the root `/` would then be allowed to fallback to the default one.
|
|
|
In this case you should have a canonical (pointed to the root) on that action to avoid it being treated as duplicate content.
|
|
|
|
|
|
+Now you just have to overwrite `Controller::redirect()` for the controller redirects and `HtmlHelper::link()`/`UrlHelper::build()` etc for the view level URLs generated.
|
|
|
+
|
|
|
|
|
|
## Language vs Locale
|
|
|
A locale is a combination of language and region (usually a country).
|