Best strategy for javascript i18n in Yii2

Dear all,

What is the best or recommended strategy to get i18n strings (from yii2) in javascript?

I’d like to develop a Yii2 reusable component, but it has substential feedback strings that need js representation.

So what is the best practice inside yii2?

Shall I create a view that creates a js object with all strings for a language with Yii::t()?

Shall I create a set of js files, one per language, very much like Yii2 message files?

Something else?

I’d prefer to avoid one of those js i18n package if possible. (I think that you don’t actually need one.)

Most Yii2 extensions that have similar needs often rely on the i18n coming with the js/jquery extension they expose in yii2. (For example, look at the numerous datepicker extensions.)

Thanks for your advices.


It depends on what do you want to translate. If it’s a couple of strings, data-attributes plus their generation on PHP side would do. If it’s complex calendar or dates or numbers, you need JavaScript implementation of ICU.