9

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.

Alessandro Resta
  • 1,102
  • 1
  • 19
  • 26
  • 1
    Great post. I just wanted to point out one thing. `= ` could be risky. Not all setups have short tags enabled. might sound silly, but it can take down your whole system when you migrate to new server. Why not use ` – Gogol May 18 '15 at 17:07
  • Not sure if this is it, But I've found that if the target and source destinations are the same, you'll need to force translations to get it to respect the file instead of the param input from `Yii::t`. http://www.yiiframework.com/doc-2.0/yii-i18n-messagesource.html#$forceTranslation-detail – M Sost May 18 '15 at 20:04

1 Answers1

4

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',
MEM
  • 30,529
  • 42
  • 121
  • 191