|
|
@@ -3,6 +3,15 @@
|
|
|
Internationalization is always tricky as it involves several layers,
|
|
|
from routing to controller logic and view outputs to URL building and redirecting.
|
|
|
|
|
|
+## Basics
|
|
|
+Set up a default language in your configs:
|
|
|
+```php
|
|
|
+ 'Config' => [
|
|
|
+ 'language' => 'de',
|
|
|
+ ],
|
|
|
+```
|
|
|
+Any `Configure::read('Config.language')` or `I18n::locale()` call should return that default language.
|
|
|
+
|
|
|
## Session based language switch
|
|
|
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()`:
|
|
|
@@ -54,16 +63,22 @@ if (!$language) {
|
|
|
```
|
|
|
|
|
|
And make sure your routes are all adjusted to accept and parse the language param:
|
|
|
-```php
|
|
|
+```php
|
|
|
Router::scope('/', function (RouteBuilder $routes) {
|
|
|
$routes->connect('/', ['controller' => 'Pages', 'action' => 'display', 'home']);
|
|
|
|
|
|
$routes->connect(
|
|
|
'/:language/:controller/:action/*',
|
|
|
[],
|
|
|
- ['language' => 'en']
|
|
|
+ ['language' => 'de']
|
|
|
);
|
|
|
|
|
|
...
|
|
|
}
|
|
|
```
|
|
|
+
|
|
|
+In this case only `/de/...` is a language param, since `/...` (without any) would continue to use the default language (English).
|
|
|
+
|
|
|
+You can of course also make the regexp here `'language' => 'de|en'` for all languages to be a param in the URL.
|
|
|
+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.
|