mae
mae

Reputation: 15666

Yii2: Translate Application Name

How to properly translate the name of the application in Yii2?

We can easily set the application name in main-local.php (or config/main.php) like so:

$config = [
    'name'  => 'My Application Name',
    // ...
];

But how would we translate it?

Using something like \Yii::t('app.name', 'My Application Name') does not work because the configuration file is parsed before the application language is even determined or set.

Upvotes: 1

Views: 194

Answers (1)

rob006
rob006

Reputation: 22174

The easiest way is to do translation on actual usage of application name:

<?= \Yii::t('app.name', Yii::$app->name) ?>

For messages extraction you may use fake translation in comment. Not sure about Poedit, but built-in Yii extractor support this some time ago:

$config = [
    // \Yii::t('app.name', 'My Application Name')
    'name'  => 'My Application Name',
    // ...
];

In worst case you may create separate file for such fake translations only for messages extraction, and don't include it on actual execution.

Upvotes: 2

Related Questions