为什么我们使用$ this __("Some text")而不是简单的echo magento

2 magento

我正在使用magento并且有一个问题,为什么我们使用$ this __("Some text")而不是简单的echo magento.有人可以吗?

Key*_*hah 5

你打电话的时候

echo $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);
}
Run Code Online (Sandbox Code Playgroud)

接下来是 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;
}
Run Code Online (Sandbox Code Playgroud)

这是交给谁的 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;
}
Run Code Online (Sandbox Code Playgroud)

返回结果文本.这是一个如何处理所有内容的快速演练,您应该自己查看类以获得更深入的理解.

简单地说,当你调用echo $this->__('some text')它时,查找位于的CSV文件中的相同文本

app>locale

要么

app>design>frontend>YOUR_PACKAGE>YOUR_THEME_NAME>locale>translate.csv

如果存在相同的单词,那么它会翻译一个单词

意味着它在多语言网站中非常有用