3

Is there a way to translate phrases defined in JS templates in Magento 2? I have checked official translation structure overview and searched the net but can't seem to find proper solution.

For example: I wish to translate Qty string within minicart element. I have added the following translation to i18n/xx_XX.csv:

"Qty","Translation"

but doesn't take effect after refreshing. Other strings within the csv that are server side generated are translated fine.

I also have tried with deleting all cache files within /var and refreshing/flushing cache from admin.

Thanks for any suggestions.

  • Translations work for JS as well as for PHP. Try to add translation to app/code/Magento/Sales/i18n/en_US.csv and you will see translated Qty in minicart in default locale. Most likely you added custom csv to the wrong location, check once again according to the official guide. – Alex Paliarush Dec 23 '15 at 10:21
  • As i have checked for each custom templete there should be i18n folder and all translation overrides should go there. Funny that most of the strings get translated, only from the js isn't. – g5wx Dec 23 '15 at 10:24
  • We are having similar problems but not specific for JS. In our case the whole mini cart stays untranslated while most of the rest of the theme (including the full shopping cart page) gets translated correctly. We are using a custom language pack as well as a theme i18n folder. CSVs are definitely in the right location as in most parts of the theme translation is working. – Jey DWork Dec 24 '15 at 2:50
  • This seems related also it's only about the js-translation.js and we weren't able to solve our problems with this file with the answer there: magento.stackexchange.com/questions/89135/… – Jey DWork Dec 24 '15 at 4:49
  • Yep, that didn't work for us either. – g5wx Dec 24 '15 at 8:15
5

Ok, so finally got this resolved by creating a new language pack, modified certain template files and cleared all caches:

  1. Created a new language pack:
    • created new folder inside app/i18n/< vendor >/xx_xx/
    • created 3 necessary files: language.xml, registration.php, xx_XX.csv according to official guidelines (mind lower and uppercases of folders and files)
    • translated the store front within the csv
    • cleared all the caches within administration
  2. As almost all strings were translated except the minicart & checkout items my next steps were:
    • copied each corresponding html file for specific element to my theme_folder/module which started to pull the new translations from my language pack. For example on how i translated mini cart string "Recently added item(s)": i copied the content.html from app/design/frontend/< vendor >/< blank >/Magento_Checkout/web/template/minicart/content.html to my corresponding theme module folder. Doing this the translations from my language package were getting translated.

Those strings that didn't get translated in step 1. and got translated in step 2. by copying the module files to theme folder had the attribute data-bind="i18n.

Reference: Creating a language pack in M2 (official)

  • 5
    This looks kinda like a bug to me: if I understand it correctly, to get the translations to work, you had to copy/paste the html-templates to your theme? All of them? Without any modifications? Doesn't seem right to me. – Giel Berkers May 2 '16 at 11:55
  • Not all, just the ones i had to translate. It was a bug at the beginning of M2 when knockout translation wasn't working properly on some files (i think it should be resolved now). – g5wx May 4 '17 at 5:20
  • 2.2.0 and this issue still appears in some cases – Cladiuss Nov 3 '17 at 10:05

Not the answer you're looking for? Browse other questions tagged or ask your own question.