Fix Javascript Translation Not Working Properly in Magento 2

js-translationlocalisationmagento-2.1magento2

I've translated some text in a javascript file with translation.js, I've done it the following way:

I've defined the translator.

define([
'mage/translate',
];

After that, I translated my text.

 $.mage.__('excl. VAT')

And added the translation in the CSV file.
The strange thing is, that sometimes it get's translated and sometimes not…

Did I miss something?

Best Answer

Best approach for this would be to use 'domReady!' in your define to make sure document is fully loaded once translation is applied.

define([
    'jquery',
    'underscore',
    'domReady!'
], function ($, _) {...}
Related Topic