How does Magento translate works?
Asked Answered
H

1

6

I want to translate a frontend Magento store using only one csv file. So I've done this:

I've created a custom module called Translator. In its config.xml I've put these lines:

<config>
....
<translate>
<modules>
<MyPackage_Translator>
<files>
<default>MyPackage_Translator.csv</default>
</files>
</MyPackage_Translator>
</modules>
</translate>

</config>

Then I've created de default Data.php helper in my Helper folder.

Then in app/locale/de_DE I've created the MyPackage_Translator.csv file where I put all my strings.

Now, if in a phtml(regardless the module) file, if I do something like:

echo $this->__('My string'); 

it works pretty well. But I'm curios why does Magento looks in the MyPackage_Translator.csv for the string?

I also noticed that, if I'm using $this->__('some string'); in the Customer module, Magento will look first in app/locale/de_DE/Mage_Customer.csv file for the string, and if it doesn't find the string there, then it will look in my MyPackage_Translator.csv file. Why is that? Why it doesn't look in Enterprise_Customer.csv file for example?

Can someone explain me the workflow Magento does when it's looking for the translation file?

Handicapper answered 6/4, 2012 at 14:2 Comment(0)
A
10

When you call $this->__('some text');

You can start by looking at Mage_Core_Helper_Abstract

/**
 * Translate
 *
 * @return string
 */
public function __()
{
    $args = func_get_args();
    $expr = new Mage_Core_Model_Translate_Expr(array_shift($args), $this->_getModuleName());
    array_unshift($args, $expr);
    return Mage::app()->getTranslator()->translate($args);
}

Next is Mage_Core_Model_App

/**
 * Retrieve translate object
 *
 * @return Mage_Core_Model_Translate
 */
public function getTranslator()
{
    if (!$this->_translator) {
        $this->_translator = Mage::getSingleton('core/translate');
    }
    return $this->_translator;
}

Which is handed to Mage_Core_Model_Translate

/**
 * Translate
 *
 * @param   array $args
 * @return  string
 */
public function translate($args)
{
    $text = array_shift($args);

    if (is_string($text) && ''==$text
        || is_null($text)
        || is_bool($text) && false===$text
        || is_object($text) && ''==$text->getText()) {
        return '';
    }
    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);
    }

    //array_unshift($args, $translated);
    //$result = @call_user_func_array('sprintf', $args);

    $result = @vsprintf($translated, $args);
    if ($result === false) {
        $result = $translated;
    }

    if ($this->_translateInline && $this->getTranslateInline()) {
        if (strpos($result, '{{{')===false || strpos($result, '}}}')===false || strpos($result, '}}{{')===false) {
            $result = '{{{'.$result.'}}{{'.$translated.'}}{{'.$text.'}}{{'.$module.'}}}';
        }
    }

    return $result;
}

which returns the resulting text. This is a quick walk through of how everything would be handled, you should view the classes themselves to get a more in depth understanding.

Alpenstock answered 6/4, 2012 at 18:37 Comment(3)
Maybe you can translate directly at Mage_Core.csvAgist
I wouldn't recommend such an action as it is not recommend to change any of the core files that Magento provides. It wouldn't be upgrade proof. He was moving in the right direction with overloading the core class for TranslateAlpenstock
I mean, use the Mage_Core.csv in your specific language. Not overriding the original one.Agist

© 2022 - 2024 — McMap. All rights reserved.