One of Magento's biggest strengths is it's ability to manage multiple websites and store views from a single installation. The most common use is to have multi-lingual versions of your store. But how do we go about adding translations to third party or custom made modules?

Start by going to the modules configuration xml here;


MAGE ROOT/app/code/local/COMPANY/MODULE/etc/config.xml


If you can't find the module here, it may be in the 'community' folder, here;


 MAGE ROOT/app/code/community/COMPANY/MODULE/etc/config.xml


Within the frontend block, add the following;


<translate>
<modules>
<Company_Module>
<files>
<default>Company_Module.csv</default>
</files>
</Company_Module>
</modules>
</translate>



NOTE: Don't forget to insert the correct values for 'company' and 'module'.

Save the config, flush the caches and the module is now ready to start accepting translated values. You will need modify the modules output files such as phtml template files and index controllers.

Wherever you previously had output like this;


<h1><?php echo 'Hello world'; ?></h1>
<p>Welcome to the module home page</p> 


You need to modify it like this;


<h1><?php echo $this->__('Hello world'); ?></h1>
<p><?php echo $this->__('Welcome to the module home page'); ?></p>


Calling "$this->" instantiates the current modules helper class. Following it with "__('xxxx')" calls the translation for this module. If you have any ambiguity about the current module, you can also call it explicitly like this;


<h1><?php echo Mage::Helper('module')->__('Hello world'); ?></h1>
<p><?php echo Mage::Helper('module')->__('Welcome to the module home page'); ?></p>


Where 'module' is the name of the module you are trying to call. Note the omission of the company name here.

With your translatable values in place, you can now create the versions for your alternative language. Let's say you have a storeview that is set to the German locale. Create a csv file with these values;


"Hello world","Hallo welt"
"Welcome to the module home page","Willkommen auf der Startseite Modul"


And save the file here;


MAGE ROOT/app/locale/de_DE/Company_Module.csv


Flush your Magento cache's and visit the German store page that displays your module, et voila! (or 'los gehts')

Adding variables in translations is a doddle!

That's the basics covered, but what about entering variables in the translations? This is a doddle too. Lets say you have a string like this in your module home page;


if (Mage::getSingleton('customer/session')->isLoggedIn()) {
$name = Mage::getSingleton('customer/session')->getCustomer()->getName();
echo 'Hello '.$name.', welcome back';
}


To translate the welcome string without the need to split it up, modify it to this;


echo $this->__('Hello %s, welcome back', $name);


Add the translation for this to your csv like this;


"Hello %s, welcome back","Hallo %s, willkommen zurück"


You can add multiple variables to the same string simply by declaring them in the same order;


<a href="<?php echo Mage::getBaseUrl(); ?>">Go back to the home page <?php echo $name; ?>?</a>


Would change to;


<?php echo $this->__('<a href="%s">Go back to the home page %s?</a>', Mage::getBaseUrl(), $name); ?>


And add this to your translation file - note how to handle speech marks within a translation by doubling them;


"<a href=""%s"">Go back to the home page %s?</a>","<a href=""%s"">Zurück zur homepage %s?</a>"


And there you have it. Happy translating!