How to use the function yii::t(); in js files yii::t(); ? For example, how to translate the "cancel" in this piece of js code:

 "btn-primary cancel-comment'>Отмена</button><button type='button' class='btn " 

The problem is only with the translation of js files, in php files - everything is ok.

Update

The trouble is that in this case there are no views, there are only js files for modal windows with "close", "save" buttons and so on ... I am very weak in js , therefore, if it does not complicate you, whether it is possible in more detail regarding the "similar" function on js - an example of a thread or something.

  • Do you know how to work with yii::t() ? Customized? In php files used? Is the problem only with js scripts or is the translation not configured at all? - Alexey Shimansky
  • If we talk specifically about individual FILES, then nothing. Same PHP function .... Only to do similar function on JS. If we talk about the javascript code in the views, then do the same as Usually <?= Yii::t('translateCatalog', 'translateTag'); ?> <?= Yii::t('translateCatalog', 'translateTag'); ?> - Alexey Shimansky

1 answer 1

IMHO it is necessary to do it with your hands, in Yii, it seems there was nothing like that of a box about dictionaries in js. If this is needed only sporadically, then you can even just through a variable like this in the view:

 $this->registerJs("var cancelText = '". Yii::t('app', 'Отмена') . "';", \yii\web\View::POS_HEAD); 

and correspondingly, in js, substitute the text from the variable cancelText ...

If you think a little more globally, then it probably makes sense to immediately lead / generate a duplicate dictionary in js with a small wrapper similar in functionality to Yii :: t () and connect this dictionary along with the rest of js-code, throwing the required locale from the backend.