Friday, November 29th 2013


Magento: The order of translations
posted @ 7:45 am in [ Magento -Magento Certified Developer ]

Magento supports localised translations of text used within the site, using a number of translation methods which are perfect for localising the text used within your e-commerce website.

However it is often confusing what order they are prioritised in and where to look to find out which files should be translating your text, so here’s the orders and a little explanation for reference:

The order of checking translation files by Magento is:

1. Extension translation files
2. Theme translation files
3. Database translation table


        foreach ($this->getModulesConfig() as $moduleName=>$info) {
            $info = $info->asArray();
            $this->_loadModuleTranslation($moduleName, $info['files'], $forceReload);
        }

        $this->_loadThemeTranslation($forceReload);
        $this->_loadDbTranslation($forceReload);

1. Extension translation files

Extension translation files are defined in your (or other 3rd party) extensions using the extension’s etc/config.xml file.

Here is an example for our made-up extension, Skywire_Translation

/app/code/local/Skywire/Translation/etc/config.xml




    
        
            0.1.0
        
    
    
        
            
                
                    
                        Skywire_Translation.csv
                    
                
            
        
    


Extension translation files are stored in:

/app/locale/your_LOCALE/Your_Translation_File.csv

so in our case as we are using en_GB as our locale and our above config.xml, this would be:

/app/locale/en_GB/Skywire_Translation.csv

and the contents has the usual Magento format of “Thing to translate”,”What you want it to be instead”, for example

"Add to Cart","Add to Bag"

2. Theme translation files

Theme translation files are applied after Extension translation files so will not get used if text is already re-defined in the extension translation files.

These files reside in:

/app/design/frontend/PACKAGE/THEME/locale/your_LOCALE/translate.csv

so in our case would be

/app/design/frontend/skywire/default/locale/en_GB/translate.csv

and are the same format as all other Magento translate files, namely:

"Add to Cart","Add to Bag"

3. Database translation table

The Database translation table, core_translate, can also be used to store translations, but seems to be largely not used by core or other extensions.

The table structure is:


mysql> desc core_translate;
+-----------+----------------------+------+-----+------------------+----------------+
| Field     | Type                 | Null | Key | Default          | Extra          |
+-----------+----------------------+------+-----+------------------+----------------+
| key_id    | int(10) unsigned     | NO   | PRI | NULL             | auto_increment |
| string    | varchar(255)         | NO   |     | Translate String |                |
| store_id  | smallint(5) unsigned | NO   | MUL | 0                |                |
| translate | varchar(255)         | YES  |     | NULL             |                |
| locale    | varchar(20)          | NO   |     | en_US            |                |
+-----------+----------------------+------+-----+------------------+----------------+

so we would use the ‘string’ field for our source text, and the ‘translate’ string for our desired text.

Interestingly this is used by the Magento inline translation system – which is something I didn’t even know existed (did you?!)

There’s a video of it here and it appears to allow you to translate all the text on your website, using your browser from the frontend.

4. Debugging

If you want to see what extension is claiming what text on your page, a quick check is to echo out the $code for each translate object as the page is rendered.

A quick way to do this (only for testing mind you) is to edit

Mage_Core_Model_Translate

and around line 405 in function translate, echo out the $code value after it is set.

For example:


    /**
     * Translate
     *
     * @param   array $args
     * @return  string
     */
    public function translate($args)
    {
...
        if ($text instanceof Mage_Core_Model_Translate_Expr) {
            $code = $text->getCode(self::SCOPE_SEPARATOR);
            $module = $text->getModule();
            $text = $text->getText();
            $translated = $this->_getTranslatedString($text, $code);
        }
        else {
            if (!empty($_REQUEST['theme'])) {
                $module = 'frontend/default/'.$_REQUEST['theme'];
            } else {
                $module = 'frontend/default/default';
            }
            $code = $module.self::SCOPE_SEPARATOR.$text;
            $translated = $this->_getTranslatedString($text, $code);
        }
        // debug line below
        echo $code;
...
        return $result;
    }

5. One Final Tip…

Magento Translation Files are cached which means after you have changed them or added new files, you have to clear the Translations cache in order to see the effects

System -> Cache Management -> Cache Storage Management -> Translations [select] -> Actions [Referesh] -> Submit

You’ll be happy you read this line 🙂

References:

What is the difference between translate.csv translations and the database method via the table core_translate?

How to add your own translations to Extensions?

Usefull tools:

Exports the DB translations in Magento to CSV files

Magento in-line translations


Leave a Reply