P
P
ProgramSM2017-08-23 11:35:51
Yii
ProgramSM, 2017-08-23 11:35:51

How to configure Yii2 i18n to translate from ru-RU (source language) to ru-RU (target language)?

The situation is the following. The project is implemented in Russian, but in some cases messages in English may fly by. Previously, the default value was en-US, but at the moment internationalization is being implemented and there is a need to translate the application interface into English.
There is a configuration

return [
    ...,
    'language' => 'ru-RU',
    'sourceLanguage' => 'ru-RU',
    ...,
]

and
return [
    ...,
    'components' => [
        'i18n' => [
            'translations' => [
                'app*' => [
                    'class' => 'yii\i18n\PhpMessageSource',
                    'basePath' => '@frontend/messages',
                    'fileMap' => [
                        'app' => 'app.php',
                        'app/error' => 'error.php',
                    ],
                ],
            ],
        ],
    ],
    ...,
];

The problem is that if the source language and the target language are the same, then Yii2 does not translate, which is logical.
I found three solutions to this problem, but all of them are crutches:
  1. Make a directory in @frontend/messages', for example, ru-RU2 and make the target language ru-RU2.
  2. Make 'sourceLanguage' => null,
  3. Make 'sourceLanguage' => 'ru',

Is there a better way to solve this problem through the configuration?
UPDATE.
Solution:
Add to configuration 'forceTranslation' => true,
return [
    ...,
    'components' => [
        'i18n' => [
            'translations' => [
                'app*' => [
                    'class' => 'yii\i18n\PhpMessageSource',
                    'basePath' => '@frontend/messages',
                    'fileMap' => [
                        'app' => 'app.php',
                        'app/error' => 'error.php',
                    ],
                    'forceTranslation' => true,
                ],
            ],
        ],
    ],
    ...,
];

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Timofeev, 2017-08-23
@ProgramSM

www.yiiframework.com/doc-2.0/yii-i18n-phpmessageso...
forceTranslation - Whether to force message translation when the source and target languages ​​are the same.
Apparently this is what you need.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question