Wednesday, June 5, 2013

How to Add Translation to Custom Module in Magento

6/05/2013

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]\app\design\frontend\[theme_folder]\default\locale\[localization_folder]\MyModule.csv where [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 is a web developer, programmer and blogger. He has expertise in PHP, Magento, WordPress, jQuery, JavaScript, HTML5 and CSS3. He loves to develop good looking websites with strong backend.

2 comments :

  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'.

    ReplyDelete

We would love to hear from you...

 

© 2014 Web Speaks . All rights resevered. Designed by Templateism