Yii2 weird i18n message/extract behavior

Viewed 21

I just created a new Yii2 advanced project with the suggested command:

composer create-project --prefer-dist yiisoft/yii2-app-advanced sdcac

(sdcac is the project name)

Following https://www.yiiframework.com/doc/guide/2.0/en/tutorial-i18n I tried to "extract" all i18n messages from my app.

From root folder I executed this command to create config file:

./yii message/config --languages=en,es --messagePath=messages console/config/i18n.php

It generated this:

return [
    'color' => null,
    'interactive' => true,
    'help' => false,
    'silentExitOnException' => null,
    'sourcePath' => '@yii',
    'messagePath' => 'messages',
    'languages' => [
        'en',
        'es',
    ],
    'translator' => [
        'Yii::t',
        '\\Yii::t',
    ],
    'sort' => false,
    'overwrite' => true,
    'removeUnused' => false,
    'markUnused' => true,
    'except' => [
        '.*',
        '/.*',
        '/messages',
        '/tests',
        '/runtime',
        '/vendor',
        '/BaseYii.php',
    ],
    'only' => [
        '*.php',
    ],
    'format' => 'php',
    'db' => 'db',
    'sourceMessageTable' => '{{%source_message}}',
    'messageTable' => '{{%message}}',
    'catalog' => 'messages',
    'ignoreCategories' => [],
    'phpFileHeader' => '',
    'phpDocBlock' => null,
];

Then, I tried to "extract" all the messages from my project running:

./yii message/extract console/config/i18n.php

But it only finds Yii texts on "vendor" folder, although it's listed on "except" param:

Yii2 console extract command result

For example, it misses this message on "views/site/index.php" file:

\Yii::t('app', 'Give it a try!')

I can't understand why. What's wrong? Please, help!

1 Answers

The reason why it's looking inside of vendor folder is this part of config:

'sourcePath' => '@yii',

This is telling the extractor that it should look where @yii alias is pointing. Which is the directory where the BaseYii.php of framework is located.

That's fine if you want to translate strings in framework. But, if you want to translate strings in your own code you have to change sourcePath to point to root directory of your project.

For example if your config is located in console/config folder:

'sourcePath' => dirname(__DIR__, 2),
Related