Skip to content

Mage_Core_Block_Abstract::__() use singleton for expr var #26

Closed
@stalniy

Description

@stalniy
    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);
    }

You can use singleton $expr var, because if we look at the Mage_Core_Model_Tranlate::translate

public function translate() {
.............
        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);
        }
.................
}

we will see that expr object is used only once to fetch args for Mage_Core_Model_Tranlate::_getTranslatedString(). You have a lot calls of __() method in templates, models, etc. It can improve perfomance.

And why do you use array_shift/array_unsfhit to get and pass first element of arguments array? Just use array index:

    public function __()
    {
        $args = func_get_args();
        if (!empty($args[0]) {
            $args[0] = new Mage_Core_Model_Translate_Expr($args[0], $this->getModuleName());
            return Mage::app()->getTranslator()->translate($args);
        }
    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions