H
H
HaruAtari2015-09-17 18:25:55
Yii
HaruAtari, 2015-09-17 18:25:55

I18n in Yii2 using special keys instead of original language phrase?

Good afternoon.
Yii for localization suggests specifying text in one language in the program code, and pulling up translations for other languages. This is how we write in the code:
<?= \Yii::t('app', 'Hello {0}', $name); ?>
And this is how it is in the dictionary:

retunr [
  'Hello {0}' => 'Привет {0}',
];

It is inconvenient that in the case of, for example, correcting the original phrase, this key will also have to be edited in all dictionaries. Or sometimes it turns out that in the original language two similar phrases sound identical, but in translation they are all different. There is already a problem of duplication of keys on the face.
As a solution to this problem, I want to use generic keys in code. Something like this:
<?= \Yii::t('app', 'user.greeting', $name); ?>
And like this in the dictionary:
retunr [
  'user.greeting' => 'Привет {0}',
];

Has anyone used this approach? Should we expect trouble because of this?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander Makarov, 2015-09-20
@HaruAtari

Using keys is the normal approach. Specify `sourceLanguage` as `key` and go.

M
matperez, 2015-09-17
@matperez

Try it. If you don't like it, go back to the original version.
The problem of duplicate keys is solved by dividing strings into different categories. The first argument in Yii::t is just a category.
The fact that you have to edit the dictionary after changing the phrase is normal. Yii is able to extract phrases for the dictionary itself, you will only need to find the old translation in the file and add it to the new line after a little correction.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question