lajax / yii2-translate-manager

Translation Manager
MIT License
227 stars 90 forks source link

Yii::t dynamic translations #69

Closed StalkAlex closed 8 years ago

StalkAlex commented 8 years ago

I have this lines:

$role = key(Yii::$app->authManager->getRolesByUser(Yii::$app->user->identity->id));
echo '<span>'.Yii::t('app',$role).'</span>';

Of course scan will not help me to define such messages and I am not able to translate them, there aren't in table. What's correct way should be? I think adding them manually is not good solution.

moltam commented 8 years ago

This won't work because $role is not a concrete string, but a variable with arbitrary value.

You should wrap your role names in Yii::t() (wherever you define them), and they will be scanned. This way the getRolesByUser() will return the translated string.

StalkAlex commented 8 years ago

Ok, also thought about it. But what about such case like view consist of custom strings taken from db. I see one way - add them manually to source table before render. Is there more good way to solve it?

moltam commented 8 years ago

The extension can scan db values. You have to configure which columns need to be translated. See in readme:

'tables' => [                   // Properties of individual tables
    [
        'connection' => 'db',   // connection identifier
        'table' => '{{%language}}',         // table name
        'columns' => ['name', 'name_ascii'],// names of multilingual fields
        'category' => 'database-table-name',// the category is the database table name
    ]
]
StalkAlex commented 8 years ago

OH, that's right. Thanks again.