Magento 1.9 - Translate text in php file
-
08-10-2020 - |
Domanda
How I can translate this text in a php file, I want to translate $message
if (!$this->_getSession()->getNoCartRedirect(true)) {
if (!$cart->getQuote()->getHasError()) {
$notifysymbol = "<img src='".Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA)."/noticeicons/1489953827_warning.png' >";
$message = $this->__('<div class="notit"><span class="notification-symbol">%s</span><div class="rowt2"> <span class="added_cart">Product added to cart</span><br/><span class="small-text"> Go to the checkout to complete your purchase or add more products</span></div></div>',$notifysymbol);
$this->_getSession()->addSuccess($message);
}
$this->_goBack();
}
and this is the text in the Custom field:
<div class="notit"><span class="notification-symbol">%s</span><div class="rowt2"> <span class="added_cart">Product added to cart</span><br/><span class="small-text"> Go to the checkout to complete your purchase or add more products</span></div></div>
The text is the core_translate table too
Soluzione
The $message
is already translatable, because you use $this->__()
to generate it. So, you can enable Inline Translation to translate it.
However, it is not best practise to use HTML code inside a translatable string. Imagine you string will some time be changed by your client - this will surely result in errors when HTML code needs to be changed. I would suggest the following solution:
<?php
$headline = $this->__('Product added to cart');
$text = $this->__('Go to the checkout to complete your purchase or add more products');
$message = $this->__('<div class="notit"><span class="notification-symbol">%s</span><div class="rowt2"> <span class="added_cart">%s</span><br/><span class="small-text">%s</span></div></div>', $notifysymbol, $headline, $text);
Altri suggerimenti
I found the same question on StackOverflow. Try this out:
One argument, in one place:
Mage::helper('module')->__('Hi %s, there is an error', $name);
Two arguments, in two places:
Mage::helper('module')->__('Hi %s, there is an error with code %s', $name, $code);
One argument, in two places:
Mage::helper('module')->__('Hi %1$s, there is an error. Your name is %1$s', $name);
Two arguments, swapping places:
// Template or block file
Mage::helper('module')->__('Hi %1$s %2$s, there is an error', $firstname, $lastname);
// CSV translation
"Hi %1$s %2$s, there is an error", "%2$s, %1$s received an error"