2

I have a question about translation in block classes. In phtml files, $this->__() (A) is used to translate (or at least most commonly that I've seen). In Mage block classes, Mage::helper('helpername')->__() (B) is commonly used, but I've seen people use $this->__() in block classes, as well.

Whether it's Mage_Core_Block_Abstract::__() or Mage_Core_Helper_Abstract::__(), it doesn't effectivelty matter for now, as they are identical functions (EE 1.13).

Question 1

But which is more proper? I understand that this could be somewhat opinion-based, but to be more future-resistant, I would think using the helper is more proper.

Question 2

Is there a particular reason the functions defined in block classes use the helper's method, whereas the template uses the block's method?

4

I prefer to use Mage::helper('somehelper')->__('...') because that way I'm sure the translation is done using the specified language file.
You can avoid issues in this case when you rewrite core classes.
Let's say for example you rewrite some block from the catalog module.

If you use in your new block $this->__() then Magento will try to use the translation file for your new module. This can lead to trouble.

But there are 2 options here.
Option 1.
use Mage::helper('catalog')->__('...')

Option 2.
add in your new block class this method

public function getModuleName() {
    return 'Mage_Catalog';
}

This will make $this->__() use the Mage_Catalog module for translation.

For controllers that doesn't work. You need to add

protected function _getRealModuleName(){
    return 'Mage_Catalog';
}

But this second option can lead to troubles also, because you may add in your new block texts that don't exist in the Mage_Catalog.csv file.

Hence I say using Mage::helper('somehelper')->__('...') is safer.

As for templates using $this->__() here is my opinion.
Not sure if it's true or not.

In theory a template can be used by multiple blocks from different modules. (although it rarely happens). using $this->__() gives you the opportunity to translate the same template in the same language but get different results depending on the module that is using that template.
Sorry but I don't have an example for this. I've never used it like that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.