magento i18n

Magento Translations Module

One of best things in magento is you can easily add new language versions to the site. It's all done using CSV files - 1 file for 1 module per language. For most popular languages translations are available as free Magento modules on magento connect.

What if we want to change the translations and don't lose changes after updating magento and translations? Or if we want to have translation common independently of module? Or just list all the texts that were added by us and are not original magento texts? We can create our own very simple translations module.

You need to edit only 2 files:

  • etc/config.xml
    <?xml version="1.0"?>
    <config>
            <modules>
                    <Baobaz_Translations>
                            <version>0.1.0</version>
                    </Baobaz_Translations>
            </modules>
           
            <global>
                    <helpers>
                            <translations>
                                    <class>Baobaz_Translations_Helper</class>
                            </translations>
                    </helpers>
            </global>
           
            <frontend>
                    <translate>
                            <modules>
                                    <Baobaz_Translations>
                                            <files>
                                                    <default>Baobaz_Translations.csv</default>
                                            </files>
                                    </Baobaz_Translations>
                            </modules>
                    </translate>
            </frontend>

    </config>
  • and helper/data.php:
    class Baobaz_Translations_Helper_Data extends Mage_Core_Helper_Abstract
    {

    }

and that's it... Module must be turned on (in etc/modules) and it will work fine.

in app/locale/fr_fr/ we create file Baobaz_Translations.csv and in it we put all the new translations we need.

Then, anywhere in magento - doesn't matter if it's template, block or controller - we can use:

echo Mage::helper('translations')->__('text to translate');

Yes. It is that simple.

Translation in Magento

Translations between languages is quite simple in Magento. For an advanced developer, who has already worked with PHP frameworks, it is not a matter to discuss, but for a beginner it may need a few words of explanation. All texts on the page that should be translated, are stored in CSV files, in /app/locale/[language] directory. The [language] part of the path is build of language code, e. g. 'en_US'.

If you have a look at the files content, you will find that every file structure is the same, one line contains two texts: one in your default language, and one in the destination language. To use the files - also called dictionaries - they must be declared in module's config file (config.xml). You should put this code directly inside your frontend or admin node of the config file.

<translate>
        <modules>
                <companyname_modulename>
                        <files>
                        <default>CompanyName_ModuleName.csv</default>
                        </files>
                </companyname_modulename>
        </modules>
</translate>

That's all. Now, when you want to use a phrase that should be translated, use:

echo $this->__('text to translate');

Magento will look in you dictionary files for the phrase 'text to translate' and will return the corresponding text in the proper language.