Get Locale Language Name in Store Selector for Magento Multistore

localisationmultistore

When looking through the multistore language selector (in our theme) we see that the dropdown is populated as follows:

foreach ($this->getStores() as $_lang) {
    $select_stores[] = '<option '.$_selected_option.' value="'. $_lang->getCurrentUrl() .'">'. $this->htmlEscape($_lang->getName()) .'</option>';
}

The problem is that the (real) language is not the store name we give it and is fetched through '$_lang->getName()'

But rather the Locale set in the backend in storeconfig

$_language_code = substr(Mage::getStoreConfig('general/locale/code', $_lang->getId()),0,2);

I know how to fetch ths ID, but ….

Question: How can we now fetch the Locale full language name to populate the dropdown? (so German or Deutsch for Germany and English or Engels for English)

(And maybe cut off the the piece between brackets, so English (United States) becomes English)

Best Answer

Here is possible solution. Not the most elegant but it should work.
First you should get all the languages that appear in the backend in the locale config section.

$allLanguages = Mage::app()->getLocale()->getOptionLocales();

This will get you an array like this:

Array
(
    [0] => Array
        (
            [value] => af_ZA
            [label] => Afrikaans (South Africa)
        )

    [1] => Array
        (
            [value] => sq_AL
            [label] => Albanian (Albania)
        )

    ...
)

You can get the current locale code like you already do

$localeCode = Mage::getStoreConfig('general/locale/code', $_lang->getId());

Now all you have to do is to loop through all the languages and see if one matches your code.

$languageLabel = '';
foreach ($allLanguages as $language) {
    if ($language['value'] == $localeCode) {
        $languageLabel = $language['label'];
        break;
    }
}

If you want to lose the country name just do this.

$parts = explode('(', $languageLabel);
$languageLabel = trim($parts[0]);

Or you can use a regex that seams cleaner.

Of course you should wrap all this code in a method and just use that method.

[EDIT]
An other approach would be to get all the languages based on the language code only:

$languages = Zend_Locale::getTranslationList('language', Mage::app()->getLocale()->getLocaleCode());//replace with own locale.

this should return something like

[en] -> English
[fr] -> French

Then you have to get the lang code like this (you already do that):

$_languageCode = substr(Mage::getStoreConfig('general/locale/code', $_lang->getId()),0,2);

And just check

`if (isset($languages[$langCode]))`  

This seams cleaner but I'm not sure if it works correctly for all the values.
The first method seams safer because it uses the same options that you see in the backend.
Give either one of them a try.

[EDIT] Example of the final code from the question author (works like a charm)

    <!-- LANGUAGES BOF -->
<?php if(count($this->getStores())>1): ?>
<?php
$stores = array();
$_current = null;

$languages = Zend_Locale::getTranslationList('language', Mage::app()->getLocale()->getLocaleCode());

foreach ($this->getStores() as $_lang) {

    $_selected = $_selected_option = $_language_name = '';
    $_language_code = substr(Mage::getStoreConfig('general/locale/code', $_lang->getId()),0,2);
    if (isset($languages[$_language_code])) {$_language_name = $languages[$_language_code];} 
    $parts = explode('(', $_language_name);
    $_language_name = trim($parts[0]);

    if ( ($_lang->getId() == $this->getCurrentStoreId()) ) {
     $_current =  $_lang;
     $_current_lang = $_language_name;
     $_selected = ' class="selected"';
     $_selected_option = ' selected="selected"';
    }

    // $stores[] = '<li'. $_selected .'><a style="background-image:url(\''. $this->getSkinUrl('images/flags/'.$_lang->getCode().'.png') .'\');" href="'. $_lang->getCurrentUrl() .'">'. $this->htmlEscape($_lang->getName()) .'</a></li>';
    $stores[] = '<li'. $_selected .'><a style="background-image:url(\''. $this->getSkinUrl('images/flags/'.$_language_code.'.png') .'\');" href="'. $_lang->getCurrentUrl() .'">'. $this->htmlEscape($_language_name) .'</a></li>';
    $select_stores[] = '<option '.$_selected_option.' value="'. $_lang->getCurrentUrl() .'">'. $this->htmlEscape($_lang->getName()) .'</option>';
}

?>
<div class="header-switch language-switch">
    <span><?php echo $this->__('Your Language:') ?>
        <span class="current"><?php echo $this->htmlEscape($_current_lang); ?></span>
    </span>
    <div class="header-dropdown">
        <ul><?php echo implode('', $stores); ?></ul>
    </div>
</div>
Related Topic