S
S
Snorway2016-06-22 03:02:34
Yii
Snorway, 2016-06-22 03:02:34

How to properly organize multilingualism in Yii2?

After reading the documentation and a couple of articles, I understood the following:
1. It is necessary to assign the main language (which is used in the source code of the dictionary files as an array key) and the target language (to which the translation will be made, specified as an array value). For example:
web.php file

<?php
return [
    'sourceLanguage' => 'ru-RU',
    'language' => 'en-US'
];

2. It is necessary to create a dictionary file that will contain translation from the source language to the target:
Messages/en-US/app.php file
<?php 
return [
    'Текст' => 'Text'
];

3. All site texts (in models and views) are indicated through: 4. At an arbitrary point in the application, change the language (\Yii::$app->language) from source to target and get pages translated from Russian into English. This scheme works. But, suppose that there are 10 target languages. and then, suddenly, there was a need to slightly change the text of the source language. Then it turns out that you need to change the text in the views of the application itself and in all dictionaries, and these are 10 files. Is that how it should be or am I doing something wrong? How right?
\Yii::t('app', 'Текст');

Answer the question

In order to leave comments, you need to log in

3 answer(s)
T
ThunderCat, 2016-06-22
@ThunderCat

In our bike, we use aliases so that in the first place we don’t write something like:

\Yii::t('app', 'Разлюбезная моя Елизавета Петровна! С этим письмом прилагаю свою любовь бла бла');
// instead use alias
\Yii::t('app', 'suhov_mail');

and each language has its own file. Including main.

P
Paulus, 2016-06-22
@ppokrovsky

Correctly create an abstraction from the language that will not change. English source as an option.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question