2
votes

I'm setting the localization on a website, but I've some trouble.

I've found a tutorial, which does exactly what I need: http://nuts-and-bolts-of-cakephp.com/2008/11/28/cakephp-url-based-language-switching-for-i18n-and-l10n-internationalization-and-localization/ (having directly the language after the hostname).

So I've done everything which is written on this website:

  1. I've generated a pot file, with this pot file, I create three po files(eng, ger and fre) with PoEdit, I translated two tests fields.
  2. I've put those files into app/locale/eng|fre|ger
  3. I've added the route Router::connect('/:language/:controller/:action/*', array(),array('language' => '[a-z]{3}')); in the correct file
  4. I've set the default language: Configure::write('Config.language', 'fre');
  5. I've created three links to change the language: link('Français', array('language'=>'fre')); ?>
  6. I've set the _setLanguage method in my appController(and calling it in the beforeFilter())
  7. I've created an appHelper to don't have to specify everytime the current language

My links are well generated, I don't have any exception/errors, my locals files are readable, but when I go on the template where I try to use variable( ) I just see "testTranslation" as text, no translated text.

Why this isn't working? Looks like it can't find my files but I don't know why :(

Any help would be really appreciated!

Edit: I did some debugging with cakePhp, and it appears that when the method translate of the i18n.php file is called, I receive a $domain = default_ger var. My translations are all in default.po files, and I suppose that he is searching into default_ger.po? why this? how can i force it to read in the default.po file?

Edit: I found something: I had a problem with my controller, I forgot to declare the parent::beforeFilter() in my children controller, then the method in the controller wasn't called and the language was always the german one. After I fixed this, the french and the english are working fine, but the german is still only displaying key and not values that I've in the translation file. This is really weird, I've deleted all files in the cache, deleted my /app/locale/ger folder and copied my /app/locale/fre to /app/locale/ger(without changing anything in files) and this still doesn't work. What can be different?

3
Did you put the .po files in the LC_MESSAGES directory (/app/locale/eng/LC_MESSAGES/)?JJJ
Yes I've: /app/locale/eng/LC_MESSAGES/default.po, /app/locale/fre/LC_MESSAGES/default.po and /app/locale/ger/LC_MESSAGES/default.poJ4N

3 Answers

1
votes

Try this file layout:

***\app\locale>tree /F
├───eng
│   └───LC_MESSAGES
│           default.po
│
└───spa
    └───LC_MESSAGES
            default.po
1
votes

I finally found the problem:

My App controller wasn't called, so the German language was always used(because it worked once when changing the language to german, and register it through cookies).

All other languages than german were displaying correctly my values.

So I searched about this problem, and I tried to use "deu" instead of "ger", and it worked!

The locale folder has to be "deu", the language value can be deu or ger.

This is very strange, because for the french, I've a "fre"(FREnch) and not a "fra"(FRAnçais), and this is working.

Anyway, this work with this change

0
votes

**

  • CakePHP 3.x Localization

**

use App\Middleware\TranslationMiddleware;

 $routes->registerMiddleware('translation', new TranslationMiddleware());
 $routes->applyMiddleware('translation');

//Change language route

  $routes->connect('/swtich-language/:lang', ['controller' => 'Translations', 'action' => 'switchLanguage', ['pass' => ['lang'], '_name' => 'switch-language']);

Runtime language change for action

use Cake\I18n\I18n;
use Cake\Cache\Cache;

 /**
     * Change language run time
     * @param type $lang local
     */
    public function switchLanguage($lang = null)
    {
        if (array_key_exists($lang, Configure::read('Translations'))) {
            Cache::write('Config.language', $lang);
            $this->redirect($this->referer());
        } else {
            Cache::write('Config.language', I18n::getLocale());
            $this->redirect($this->referer());
        }
    }


namespace App\Middleware;

use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Cake\I18n\I18n;
use Cake\Cache\Cache;
class TranslationMiddleware
{

      public function __invoke(ServerRequestInterface $request, ResponseInterface $response, $next)
    {
        if (Cache::read('Config.language')) {
            I18n::setLocale(Cache::read('Config.language'));
        } else {
            I18n::setLocale(I18n::getLocale());
        }
        return $next($request, $response);
    }
}

Create .po file

/src/locale/fr/default.po

msgid "Dashboard"
msgstr "Tableau de bord"

msgid "Companies"
msgstr "Entreprises"

msgid "Log Out"
msgstr "Connectez - Out"

Use of msgid key

Ex.

<?= __(‘Log Out);?>