Translation doesn't work on breadcrumbs added by xml
-
15-04-2021 - |
문제
I'm building a custom page.
I add breadcrumbs like this (and it works well).
<brand_brand_index translate="label">
<reference name="breadcrumbs">
<action method="addCrumb">
<crumbName>Home</crumbName>
<crumbInfo>
<label>Home</label>
<title>Home</title>
<link>/</link>
</crumbInfo>
</action>
<action method="addCrumb">
<crumbName>All Brands</crumbName>
<crumbInfo>
<label>All Brands</label>
<title>All Brands</title>
</crumbInfo>
</action>
</reference>
[[...]]
</brand_brand_index>
The problem is label not translated in front-end. It shows Home / All Brands
instead of translated version.
The things I made sure:
- Added
translate="label"
to handler - Cache cleared (acctually I turned off caching)
- Translation syntax is correct (I put in Mage_Core.csv)
- Label is translated properly by php code (eg:
$helper->__('All Brands');
Help me to figure it out, please.
Thank you.
해결책
To translate a crumb without using a helper, you can use the translate
attribute for your action
node by using crumbInfo.label
and crumbInfo.title
.
Exemple:
<reference name="breadcrumbs">
<action method="addCrumb" translate="crumbInfo.label crumbInfo.title">
<crumbName>home</crumbName>
<crumbInfo>
<label>Home</label>
<title>Home</title>
<link>/</link>
</crumbInfo>
</action>
<action method="addCrumb" translate="crumbInfo.label crumbInfo.title">
<crumbName>brands</crumbName>
<crumbInfo>
<label>All Brands</label>
<title>All Brands</title>
</crumbInfo>
</action>
</reference>
It's the better way to translate breadcrumbs, use helpers only if you have the set a custom title depend on URL params or something else.
다른 팁
Note that you can also generate links for breadcrumbs using helper class.
Using helper class, above XML block may look like:
<brand_brand_index translate="label">
<reference name="breadcrumbs">
<action method="addCrumb">
<crumbName>Home</crumbName>
<params helper="module/getHomeUrl" />
</action>
<action method="addCrumb">
<crumbName>All Brands</crumbName>
<params helper="module/getBrandUrl" />
</action>
</reference>
[[...]]
</brand_brand_index>
And add the getHomeUrl()
& getBrandUrl()
methods in your module's Helper/Data.php as:
class Namespace_Module_Helper_Data extends Mage_Core_Helper_Abstract
{
//...
public function getHomeUrl()
{
return array(
'label' => Mage::helper('module')->__('Home'),
'title' => Mage::helper('module')->__('Home'),
'link' => Mage::getUrl(),
);
}
public function getBrandUrl()
{
return array(
'label' => Mage::helper('module')->__('All Brands'),
'title' => Mage::helper('module')->__('All Brands')
);
}
}
I am trying the same as below:
in my xml file:
<reference name="root">
<action method="unsetChild"><alias>breadcrumbs</alias></action>
<block type="page/html_breadcrumbs" name="breadcrumbs" as="breadcrumbs">
<action method="addCrumb">
<crumbName>Home</crumbName>
<params helper="recipe/getHomeUrl" />
<!-- <crumbInfo>
<label>Home</label>
<title>Home</title>
<link>/</link>
</crumbInfo> -->
</action>
<action method="addCrumb">
<crumbName>Recipes</crumbName>
<crumbInfo>
<label>Recipes</label>
<title>Recipe Home Page</title>
<link>/recipe</link>
</crumbInfo>
</action>
<action method="addCrumb">
<crumbName>Current Page</crumbName>
<crumbInfo>
<label>Current Page</label>
<title>Current Page</title>
</crumbInfo>
</action>
</block>
</reference>
\app\code\local\Magenshop\Recipe\Helper\Data.php
public function getHomeUrl()
{
return array(
'label' => Mage::helper('module')->__('Home'),
'title' => Mage::helper('module')->__('Home'),
'link' => Mage::getUrl(),
);
}
Im getting only:
1) /Recipes/Current Page
2) No Home page link...
I am trying like below in Magento 2. But can't translate. I put translation file under /vendor/magento/module-contact/i18n/zh_Hant_TW.csv Is it not correct?
Someone please help me.
<referenceBlock name="breadcrumbs">
<action method="addCrumb" translate="crumbInfo.title crumbInfo.label">
<argument name="crumbName" xsi:type="string">home</argument>
<argument name="crumbInfo" xsi:type="array">
<item name="title" xsi:type="string">Home</item>
<item name="label" xsi:type="string">Home</item>
<item name="link" xsi:type="string">/</item>
</argument>
</action>
<action method="addCrumb" translate="crumbInfo.title crumbInfo.label">
<argument name="crumbName" xsi:type="string">Contact Us</argument>
<argument name="crumbInfo" xsi:type="array">
<item name="title" xsi:type="string">Contact Us</item>
<item name="label" xsi:type="string">Contact Us</item>
</argument>
</action>
</referenceBlock>