Mel Mel - 1 year ago 85
PHP Question

Add hreflang to magento language switcher

I am trying to add hreflang="ca-fr" or "ca-en" my code is below

<?php if ($_lang->getId() != $this->getCurrentStoreId()): ?>
<li class="language-<?php echo $this->htmlEscape($_lang->getCode()); ?>">
<a href="#" onclick="changeLang('<?php echo $_lang->getCurrentUrl() ?>')"
hreflang="<?php echo $this->htmlEscape($_lang->getCode()); ?>"
id="link-<?php echo $_lang->getId() ?>"><?php echo $frontendNameAssign[$_lang->getId()]; ?></a>
<?php endif; ?>

I added the 4th line to the file but this displays as "ca_us_english", is there a way to amend this?

I have also tried amending Miscellaneous Scripts with no joy, any help would be grateful.


Answer Source

Well in this case you are "right" and Magento is "wrong". What Magento is actually presenting you as $_lang is not really the lang, but one of your store.

So ca_us_english is the code of your english candian store in your backend (you can have a look at it in the admin under System > Manage Stores)

To get the actual local set for this store you have to use this line of code :

Mage::getStoreConfig('general/locale/code', $_lang->getId())
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download