Magento translate Javascript file in custom module

Hello, how are you? Recently, I was asked to make translation for text in Javascript file in our Daily Deal extension. Well, I just realized it did not work for the our extension and I had to spend some times for fixing it. In this article, I will show you how to do it for a custom module.

To follow this article, I assume you that you already knew how to translate text for a Magento custom module (in controller, block or html files). And it is only helpful if you don't know how to translate the text in Javascript file. It was tested in Magento 1.5, 1.6, 1.7 and 1.8 but I think it can also work with other Magento version > 1.3.

Translation is done by using the Javascript object Translator which already exists in Magento. For any text in Javascript, you need to use this function:

Translator.translate('Some Text to Translate');

It is the same for all Magento version. The difference is in how we declare the text.

In Magento 1.5 and 1.6, you need to declare the text somewhere in your html file, like below:

Translator.add('Some Text to Translate', '__('Some Text to Translate')?>');

With Magento 1.7++, it won't work. In Magento 1.7++, you need to add a jstranslator.xml file in your etc folder (app/code/local/[Your_Namespace]/[Module_Name]/etc). This file looks like:

    <[un-identifiant-unique] translate="message" module="[monmodule]">
        Some Text to Translate

Well, that's all. I think it's really easy and you can try it yourself. Happy to answer any question and comment!

One thought on “Magento translate Javascript file in custom module”

1 Response to Magento translate Javascript file in custom module