Magento如何翻译作品? [英] How does Magento translate works?

查看:120
本文介绍了Magento如何翻译作品?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想只使用一个csv文件翻译前端Magento商店。所以我这样做:

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

我创建了一个名为Translator的自定义模块。在它的config.xml中,我把这些行:

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>

然后我在我的Helper文件夹中创建了de default Data.php helper。

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

然后在app / locale / de_DE中创建了MyPackage_Translator.csv文件,其中放入了所有的字符串。

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

如果在一个phtml(不管模块)文件中,如果我做的像:

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

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

它工作得很好。但我是好奇为什么Magento看起来在MyPackage_Translator.csv的字符串?

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

我也注意到,如果我使用$ this - > __('some string');在Customer模块中,Magento会首先在app / locale / de_DE / Mage_Customer.csv文件中查找字符串,如果没有找到该字符串,那么它将显示在MyPackage_Translator.csv文件中。这是为什么?为什么它不会在Enterprise_Customer.csv文件中查找?

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?

有人可以解释我Magento在寻找翻译文件时的工作吗?

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

推荐答案

当您调用$ this - > __('some text');

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

您可以先查看 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);
}

接下来是 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;
}

这是交给 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.

这篇关于Magento如何翻译作品?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆