Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

Magento Forum

why <?php echo $this->__(’some text’) ?> ? 
 
BlueKai
Member
 
Avatar
Total Posts:  42
Joined:  2008-09-04
Madrid Spain
 

I’ve found this all over the design templates, but can’t trace what exactly this does to figure out why they do it this way..

For example, on the register page (app/design/frontend/xxx/xxx/template/customer/form/register.phtml), the heading is:

<h2><?php echo $this->__('Create an Account'?></h2>

The class it seems to be using is:

Mage/Customer/Block/Form/Register.php
extends Mage/Directory/Block/Data.php
extends Mage/Core/Block/Template.php
extends Mage/Core/Block/Abstract.php
extends Varien_Object, which I can’t find..

What does this do and how can I trace it when I encounter this kind of thing again?

 
Magento Community Magento Community
Magento Community
Magento Community
 
Kara Heinrichs
Guru
 
Avatar
Total Posts:  482
Joined:  2008-01-17
aa, mi, us
 

There may be additional reasons why, but using the echo instead of just hard coding the text in the template has the effect of making the echo’ed text available to the translation engine.  It you echo it, you can translate it using the csv files or the inline translation tool.  If you don’t, you can’t.

 
Magento Community Magento Community
Magento Community
Magento Community
 
mrspeaker
Member
 
Total Posts:  44
Joined:  2009-01-19
 

Is that documented somewhere? Things like this would be great to know when starting magento dev, so we follow best practices etc…

 
Magento Community Magento Community
Magento Community
Magento Community
 
Kara Heinrichs
Guru
 
Avatar
Total Posts:  482
Joined:  2008-01-17
aa, mi, us
 

School of hard magento knocks.  It’s why we share in the forums.

 
Magento Community Magento Community
Magento Community
Magento Community
 
Manos Dilaverakis
Member
 
Total Posts:  49
Joined:  2009-02-25
 

The __ function is a translator function. Properly used you can make use of your own CSV file in /app/locale/ for your own modules. More information here.

 
Magento Community Magento Community
Magento Community
Magento Community
 
peterfh
Jr. Member
 
Total Posts:  7
Joined:  2010-12-13
Granada, Spain
 

It seems to be a good programming practise, but this function is not being used in file:
magento\app\code\core\Mage\Customer\Model\Entity\Setup.php

So, how can I translate the labels for new client and new address forms? Inline translation functionality doesn’t seem to work for this page in the backend.

Does it mean that I have to override the code?

Kind regards.

 
Magento Community Magento Community
Magento Community
Magento Community
 
nelk
Jr. Member
 
Avatar
Total Posts:  25
Joined:  2011-01-19
Canada
 
Pedro A. - 28 January 2011 01:15 AM

It seems to be a good programming practise, but this function is not being used in file:
magento\app\code\core\Mage\Customer\Model\Entity\Setup.php

So, how can I translate the labels for new client and new address forms? Inline translation functionality doesn’t seem to work for this page in the backend.

Does it mean that I have to override the code?


Kind regards.

Any written strings that are stored in the Database used Magento’s EAV system.  Since labels are written in the database it’s the case for that as well.  That means that you will have to set store views for each language if you don’t already have them, then set the locale for each store view (if you don’t already have that as well), then finally update the label caption only for each store view language using the top left scope selector you’ll see on some pages.

Hope that helps!

 
Magento Community Magento Community
Magento Community
Magento Community
 
peterfh
Jr. Member
 
Total Posts:  7
Joined:  2010-12-13
Granada, Spain
 

Thank you for your prompt answer, nil. But I think that it doesn\’t solve my problem. I’ve set the store views yet and I have been translating the locale strings. Firstable, I used the inline translation capability, but, sadly, some strings can’t be translated through this system, so I searched for the original text in the en_US folder and updated the DB by hand, adding a record to the core_translate table. Something like this:
(ID, Mage_CSVFilename::OriginalString, 0, TranslatedString, es_ES)

It has worked perfectly, but I’ve found that labels in the mentioned setup.php file don’t use the DB, they are written directly inside the PHP file. Maybe I’m wrong and I\’ve missed something.

And, by the way, I’ve also noticed that some strings appear twice in the locale CSV files and the only difference may be an uppercase letter. The backend uses both of them but I can add only one to the DB, as I’m using UTF-8 case insensitive codification and can’t find how to set to case sensitive. I’ve read that this happens because developers tend to make slight changes to the strings any time a new version of Magento is released, something wich makes tranlating a really hard task, unless you decide not to update your store.

Any advice would be appreciated. Thank you in advance.

Kind regards.

P. S.: I hope my English doesn’t sound rude, it’s not my intention wink.

 
Magento Community Magento Community
Magento Community
Magento Community
 
peterfh
Jr. Member
 
Total Posts:  7
Joined:  2010-12-13
Granada, Spain
 

Finally I’ve found the solution. I was wrong, there is no need to modify any PHP file. The missing label strings for the new client form are located in the eav_attribute database table. I just modified the frontend_label for the untranslated strings and it works. Anyway, I’m not sure why inline translation doesn’t work with these labels.

I hope this is helpful for someone.

Kind regards.

 
Magento Community Magento Community
Magento Community
Magento Community
 
winzter143
Jr. Member
 
Avatar
Total Posts:  10
Joined:  2011-04-04
 

Can I call the function in the Controller action?

 
Magento Community Magento Community
Magento Community
Magento Community
 
tzyganu
Mentor
 
Avatar
Total Posts:  2205
Joined:  2009-11-18
Bucharest, Romania
 

Yes you can but not in this form:

$this->__('some texts');
You can translate texts using helpers.
Here is an example
$text Mage::helper('core')->__('some text');
$text Mage::helper('customer')->__('some text');
$text Mage::helper('catalog')->__('some text');
//and so on
Basically any module should have a helper that allows you to translate texts. (all helpers extend one main helper).
The recommendation is to use the main helper of the module you are working on.
For example if you are in the ProductController.php use
Mage::helper('catalog')->__('some text');

If you are in a custom controller in a custom module: (module: Custom, controller: IndexController) create also a helper in your module

class Mycompany_Custom_Helper_Data extends Mage_Core_Helper_Data{
//no additional methods required
}
and you can use it like this:
Mage::helper('custom')->__('some text');

Int his case make sure you declare you helper in the config.xml file of your module. Inside the <global> tag put this:

<helpers>
<
custom>
<class>
Mycomany_Custom_Helper</class>
</
custom>
</
helpers>
(replace Mycompany with the namespace and Custom with the module name).
 
Magento Community Magento Community
Magento Community
Magento Community
 
Ed Baxter
Member
 
Avatar
Total Posts:  53
Joined:  2009-05-27
 

As the guys above said, it’s for the translations. You should be using it if you intend to distribute your theme (i.e: sell it) or if terms you use could change in the future. Say you launch the site in another country and need to make changes.

I’m not sure how it impacts on performance though. I hard code things that I know for sure will not need to be changed.

Cheers!

 
Magento Community Magento Community
Magento Community
Magento Community
 
reillyk
Jr. Member
 
Total Posts:  2
Joined:  2011-09-21
 

Does anyone know how i would apply the $this__() function to cms pages or static blocks?

 
Magento Community Magento Community
Magento Community
Magento Community
 
tzyganu
Mentor
 
Avatar
Total Posts:  2205
Joined:  2009-11-18
Bucharest, Romania
 

Hello.
I don’t think you can use that directly in the block or page.
Here is how I use it.
In the block or page add this code

{{block type="core/template" tempalte="static/my_page.phtml"}}
Now put your html code in app/design/frontend/{{interface}}/{{theme}}/template/static/my_page.phtml. INside it you can use $this->__() without any problem.

The folder you put your files is is not important. I just called it ‘static’ just as an example.

Cheers,
Marius.

 
Magento Community Magento Community
Magento Community
Magento Community
 
reillyk
Jr. Member
 
Total Posts:  2
Joined:  2011-09-21
 

This is helpful - thank you!

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top