Can someone indicate me where I can find the code that the lang attribute for the html tag determines?
I'd like to make the lang attribute to show <html lang="en-US">
instead of <html lang="en">
3 Answers
The Source code you are looking for is located in vendor/magento/module-theme/view/base/templates/root.phtml.
To change this behavior copy the above file to your current theme directory.
For Example:- app/design/frontend/YOURVENDORNAME/YOURTHEME/Magento_Theme/templates/root.phtml
Around line 11 Change the below line
<html <?= /* @escapeNotVerified */ $htmlAttributes ?>>
To
<html <?= /* @escapeNotVerified */ "lang=en-US"; ?>>
-
Thanks for reacting. Given the fact that i have different store views, i’d like to have it by means of php variables instead of a harcoded value Commented Nov 18, 2018 at 10:11
-
Yes it is possible to have it in php variable, you can add a condition to check the current store view by either store id or storename. Base on the store you can set the value in a php variable.– mydonsCommented Nov 19, 2018 at 6:48
I guess there is no need to overwrite the root template file.
- Just listen to "layout_generate_blocks_after" event
<event name="layout_generate_blocks_after"> <observer name="sgecom_seo_html_lang" instance="SgEcom\Seo\Observer\Frontend\HtmlLang" /> </event>
- In your observer :
use Magento\Framework\Event\Observer; use Magento\Framework\Event\ObserverInterface; use Magento\Framework\View\Page\Config as PageConfig; class HtmlLang implements ObserverInterface { protected $pageConfig; public function __construct ( PageConfig $pageConfig ) { $this->pageConfig = $pageConfig; } /** * @inheritDoc */ public function execute(Observer $observer) { $this->pageConfig->setElementAttribute ( PageConfig::ELEMENT_TYPE_HTML, PageConfig::HTML_ATTRIBUTE_LANG, 'TEST' //Put here the value you want ); } }
-
Not sure why this is downvoted. This is the correct way to modify the HTML Language Code. The 2 other answers are not best practice.– puntableCommented Nov 16, 2021 at 16:38
You can either check with store name or store id and assign the lang attribute
<?php
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$storeManager = $objectManager->get('\Magento\Store\Model\StoreManagerInterface');
$storeID = $storeManager->getStore()->getStoreId();
$storeName = $storeManager->getStore()->getName();
$lang = "lang=en-US"; //Default value for lang attribute
if($storeID==1)
{
$lang = "lang=en-US";
}
else if($storeID==2) {
$lang = "lang=en-GB";
}
?>
<html <?= /* @escapeNotVerified */ $lang ?>>