Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Yii2 - Multi language

I'm trying to set up the website's frontend translation using the i18l thing. Here is my i18l.php file placed on frontend/config

    <?php
return [
    'sourcePath' => 'frontend',
    'languages' => ['en-US', 'pt-BR'] , //Add languages to the array for the language files to be generated.
    'translator' => 'Yii::t',
    'sort' => false,
    'removeUnused' => false,
    'only' => ['*.php'],
    'except' => [
        '.svn',
        '.git',
        '.gitignore',
        '.gitkeep',
        '.hgignore',
        '.hgkeep',
        '/messages',
        '/vendor',
    ],
    'format' => 'php',
    'messagePath' => 'frontend' . DIRECTORY_SEPARATOR . 'translations',
    'overwrite' => true,
];

and here my main.php also on frontend

(...)
'language' => 'en-US',
'components' => [
        'i18n' => [
          'translations' => [
            'app*' => [
              'class' => 'yii\i18n\PhpMessageSource',
              'basePath' => 'frontend/translations',

              'fileMap' => [
                  'app' => 'app.php',
                  'app/error' => 'error.php',
              ],
          ],
        ],
]

I'm using the <?= Yii::t('app', 'some string') ?> on the sites and layouts and when I run the command ./yii message/extract @frontend/config/i18n.php it creates to me a folder called 'translations' contain other two folders 'en-US' and 'pt-BR' both with app.php which i already had filled with some translations. But still, no translation happens when i change the language on the main.php as it should be (i think). I would appreciate if someone could give me a hand on that.

Thanks.

like image 550
Alessandro Resta Avatar asked May 07 '15 15:05

Alessandro Resta


1 Answers

Great post, with all the needed details.

I was struggling with the same things, but you did it quite well.

So, if you can run the command and it generates the file, then the sourcePath is correct. If it doesn't display the translation messages at runtime, despite your setting changes, then, I presume the issue could be on your basePath:

Try using, on your basePath configuration, the following:

'basePath' => '@frontend/translations',
like image 115
MEM Avatar answered Sep 20 '22 06:09

MEM