I am using CakePHP 3.5. When My browser is in english, enverything is fine. The default locale is en_US, as I set it, and I can display content in french if I set the locale to fr_CA (I18n::setLocale('fr_CA'))
But when I change my browser's language to fr_CA, it somehow changes the default locale as well to fr_CA. So the website displays in french, but the content still displays in english since it is now the default locale
Setting the default locale in config\app.php
'App' => [
'namespace' => 'App',
'encoding' => env('APP_ENCODING', 'UTF-8'),
'defaultLocale' => env('APP_DEFAULT_LOCALE', 'en_US'),
.....
],
Adding the valid locales in src\Application.php
$middlewareQueue->add(new LocaleSelectorMiddleware(['en_US', 'fr_CA']));
Adding the Translate behavior in ArticlesTable.php
$this->addBehavior('Translate', [
'fields' => ['name', 'slug'],
'allowEmptyTranslations' => false,
]);
Fetching the content in ArticlesController.php
$query = $this->Articles->find('all')
->where(['Articles.name !=' => ''])
->contain(['Media' => function ($q) {
return $q->find('medium');
}]);
When my browser is in english (en_US) and I echo I18n::getDefaultLocale()
'en_US'
When it's in french (fr_CA) and I echo I18n::getDefaultLocale();
'fr_CA'
Note that I updated to CakePHP 3.5 recently and followed the guide to add the Middleware: Adding the new HTTP Stack to an Existing Application