How to Add Translation to Custom Module in Magento

If you are a Magento extension or module developer then you might want to internationalize your module/extension by translating it into different languages. This article will tell you how to add translation to your Magento module or extension. First of all you need to keep in mind that to make translation work in Magento, you should never write bare strings anywhere in your module.
I mean never write like this:

<?php
echo "Hello World!";
?>

But use Magento inbuilt translation method as:

<?php echo $this->__('Hello World'); ?>!

This method will look into the tanslation CSV file of you module and will replace the string to its complementary value.

Define translation CSV file for your module

You can define the translation file for your module in the config.xml file of your Magento module as:

<config>
...
<frontend>
...
<translate>
<modules>
<MyNamespace_MyModule>
<files>
<default>MyModule.csv</default>
</files>
</MyNamespace_MyModule>
</modules>
</translate>
</frontend>
<config>

Create the translation CSV file

Now create the MyModule.csv at this location: [root]appdesignfrontend[theme_folder]defaultlocale[localization_folder]MyModule.csvwhere [localization_folder] can be fr_FR or en_EN based upon the locale you want to translate into. The CSV file should contain the trannslation strings in the key-value pair format as:

"Hello World", "Bonjour tout le monde"
"Checkout", "Caisse"
"Discount", "Réduction"

This is all and you are done!

Written by Arvind Bhardwaj

Arvind is a certified Magento 2 expert with more than 10 years of industry-wide experience.

Website: http://www.webspeaks.in/

4 thoughts on “How to Add Translation to Custom Module in Magento

  1. Nice explanation but I want to make a small correction. The best practices say that the name of the translation file should be formed by the namespace and the extension name because you can have to modules with the same name but not in the same namespace. This ensures that the file is unique. So the file name in this case should be ‘MyNamespace_MyModule.csv’.

Comments are closed.