C
C
Carlos Rodriguez2015-01-15 17:44:39
Yii
Carlos Rodriguez, 2015-01-15 17:44:39

Yii2. How to use Yii::t?

/common/config/main.php
'

components' => [
        ...
        'i18n' => [
            'translations' => [
                '*' => [
                    'class' => 'yii\i18n\PhpMessageSource',
                    'basePath' => '@common/messages',
                    'fileMap' => [
                        'app' => 'app.php',
                        'app/auth' => 'auth.php'
                    ]
                ],
            ],
        ],

/common/messages/ru-RU/app.php
<?php
return [
    'City ID' => 'Город',
];

/common/config/params.php
<?php
return [
    'language' => 'ru-RU',
    'sourceLanguage' => 'ru-RU',

index.php
echo \Yii::t('app','City ID');
displays City ID
Question: what did I miss?

Answer the question

In order to leave comments, you need to log in

5 answer(s)
C
Carlos Rodriguez, 2015-01-16
@asf

Thanks to all. You were right, my sourceLanguage is not correctly specified.
And a couple more points for the translations to work:
1) moved

<?php
return [
    'language' => 'ru-RU',
    'sourceLanguage' => 'ru-RU',

from /common/config/params.php to /common/config/main.php
2) changed * to app* in component settings
'i18n' => [
            'translations' => [
                'app*'=> [
                    'class' => 'yii\i18n\PhpMessageSource',

as a result, everything worked.
ps if someone gets into such a problematic situation as I do, first of all, look at what they output
echo \Yii::$app->language;
var_dump(\Yii::$app->i18n);

A
Alexander Zelenin, 2015-01-16
@zelenin

the source language (the one you use inside Yii::t) is Russian. application language (the one you want to display) - Russian. what to translate?
Yii understands this in Russian. Change sourceLanguage

A
Alexander Gerasimov, 2015-01-15
@agerasimov

I can assume the following:
When language and sourceLanguage match, then Yii will not even go for the translation, but will take what is indicated. You have this City ID.
PS Try changing the sourceLanguage since you are using Yii::t('app','City ID') English here.

S
savelyev_andrey, 2016-01-05
@savelyev_andrey

Under tell me why messages like Update {modelClass} are not translated?
Code in app.php file

<?php
return [
    'Update' => 'Изменить',
  'Brandnames' => 'Брэнд',
  'Update {modelClass}'=>'Изменить {modelClass}',
];
?>

The rest of the lines are translated normally.

A
at0m1x, 2016-08-14
@at0m1x

Details on how to set up multilingualism in Yii2 step by step .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question