cancel
Showing results for 
Search instead for 
Did you mean: 

How to translate <title>

How to translate <title>

Hello,

 

For contact page, this is in the layout definition file (contact_index_index.xml):

 

    <head>
        <title>Contact Us</title>
    </head>

That title is not translated.How can it be translated? is it a bug in Magento2?

 

Regards

Jaime

Tags (2)
3 REPLIES

Re: How to translate <title>

Do you have the "Contact Us" entry in your language package?

Re: How to translate <title>

Of course, and in fact, if I add <?php /* @escapeNotVerified */ echo __('Contact Us') ?> to phtml file, it is translated correctly. I have even added a breadcrumb to this layout with:

 

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceContainer name="content">
            <referenceBlock name="breadcrumbs">
                <action method="addCrumb">
                    <argument name="crumbName" xsi:type="string">Home</argument>
                    <argument name="crumbInfo" xsi:type="array">
                        <item name="title" xsi:type="string" translate="true">Home</item>
                        <item name="label" xsi:type="string" translate="true">Home</item>
                        <item name="link" xsi:type="string">/</item>
                    </argument>
                </action>
                <action method="addCrumb">
                    <argument name="crumbName" xsi:type="string">Contact Us</argument>
                    <argument name="crumbInfo" xsi:type="array">
                        <item name="title" xsi:type="string" translate="true">Contact Us</item>
                        <item name="label" xsi:type="string" translate="true">Contact Us</item>
                    </argument>
                </action>                
            </referenceBlock>
        </referenceContainer>
    </body>
</page>

And text is translated correctly. The problem is in head element.

 

Another fact. Both the page title shown for browser window and page title shown above the breadcrumb are not translated.

 

Regards,

Jaime

Re: How to translate <title>

I use the following fix for this bug. You have to override Magento 2 Page renderer. Create a model file in your module `Company/Module/Model/Page/Renderer.php`:

 

<?php

namespace Company\Module\Model\Page;
class Renderer extends \Magento\Framework\View\Page\Config\Renderer {
/**
* @return string
*/
public function renderTitle()
{
return '<title>' . $this->escaper->escapeHtml(__($this->pageConfig->getTitle()->get())) . '</title>' . "\n";
}
}

And then add the following line to the `etc/frontend/di.xml` file:

 

<preference for="Magento\Framework\View\Page\Config\Renderer" type="Company\Module\Model\Page\Renderer" />