So first and foremost excuse me if this has been asked before, but there are so many questions regarding not working js translations that I didnt find any matching mine.

I had an issue where I wanted to dynamically translate the result of an API undercertain circumstances in JS like this:

value['availability'] = $.mage.__(value['availability']);

But no matter what I did the value wouldnt be translated, while it would if I used the static text instead of variable aka:

value['availability'] = $.mage.__('Ask Us');

After a bit of debugging I came to this situation:

// $.mage.__('Ask Us');
value['availability'] = $.mage.__(value['availability']);

Which then magically worked, so I came to the following conclusion, something in the generation of js-translation.json seems to check wether or not the csv translations actually appear in js files.

After a bit more grumbling I came to the thought that possibly this is the case to prevent a huge file beeing loaded client side everytime a user opens the page, so actually expected behaviour.

But now is the issue if thats expected behaviour, whats the expected solution for dynamic translations, because I dont think its to rare that someone wants to translate the returns of an AJAX call, for example translating error msgs.

But putting every possibilty of a dynamic translation in a comment above is probably not the intended way to go, so I was wondering what is?

  • Have you able to translate dynamic value coming from ajax call ? Aug 1, 2023 at 5:28
  • @クジェー if u are calling something with AJAX within magento context you would probably be fine to use the normal __() within PHP. If its outside of it, and the result is within a predictable range of options u can use the hack i described above. Where u put the possible strings in a comment so they get added into the translation js
    – astragt
    Aug 3, 2023 at 14:43
  • I already found the caused. It seems it does not translate text coming from an API result. I think it only translate text that already exist/hard coded on the file. Aug 4, 2023 at 1:06


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.