Wesley Smits
Wesley Smits

Reputation: 1344

Magento changing the language selector

In my project I have to display the webshop in two languages. By default you can choose languages with the following code in:

app/design/frontend/base/default/template/page/switch/language.phtml

<?php if(count($this->getStores())>1): ?>
<div class="form-language">
<label for="select-language"><?php echo $this->__('Your Language:') ?></label>
<select id="select-language" title="<?php echo $this->__('Your Language') ?>" onchange="window.location.href=this.value">
<?php foreach ($this->getStores() as $_lang): ?>
    <?php $_selected = ($_lang->getId() == $this->getCurrentStoreId()) ? ' selected="selected"' : '' ?>
    <option value="<?php echo $_lang->getCurrentUrl() ?>"<?php echo $_selected ?>><?php      echo $this->htmlEscape($_lang->getName()) ?></option>
<?php endforeach; ?>
</select>
</div>
<?php endif; ?>

This of course displaya a select box with the options being all the languages.

I however want to change this so that it become seperate links. I just don't really know how to do this.

This is what I have now.

<?php if(count($this->getStores())>1): ?>
<div class="form-language">
<?php foreach ($this->getStores() as $_lang):?>
    <a href="" title=""><?php echo $this->htmlEscape($_lang->getName()) ?></a>
<?php endforeach;?>
</div>
<?php endif; ?>

PS: I did not change this in the default magento code. I am working in app/design/frontend/default/projectname/template/page/switch/language.phtml.

So I managed to get this working by myself with this code:

<?php if(count($this->getStores())>1): ?>
<div class="form-language">
<?php foreach ($this->getStores() as $_lang):?>
    <a href="<?php echo Mage::getUrl() . '?___store=' . $_lang->getId()?>" title=""><?php echo $this->htmlEscape($_lang->getName()) ?></a>
<?php endforeach;?>
</div>
<?php endif; ?>

But now when I switch language. It redirects to the home page. I found I should use:

$_lang->getCurrentUrl()

But I have no idea where to place this in my code.

Upvotes: 1

Views: 9068

Answers (3)

Palanikumar
Palanikumar

Reputation: 1746

Try this code. I tested in EE 1.12.

<?php if(count($this->getStores())>1): ?>
    <ul>
    <?php foreach ($this->getStores() as $_lang): ?>
      <?php if($_lang->getId()!=$this->getCurrentStoreId()): ?>
        <li class="language-<?php echo $this->htmlEscape($_lang->getCode()); ?>">
        <a href="<?php echo $_lang->getCurrentUrl() ?>"><?php echo $this->htmlEscape($_lang->getName()) ?></a>
        </li>
      <?php endif; ?>
     <?php endforeach; ?>
    </ul>
    <?php endif; ?>

Upvotes: 0

Lucas Moeskops
Lucas Moeskops

Reputation: 5443

This seems to be different in Enterprise compared to Community. This is what the code is like in Magento Enterprise v1.12. Maybe it is of use, or maybe it works as well.

<?php if(count($this->getStores())>1): ?>
<div class="switch switcher-language">
    <label><?php echo $this->__('Language') ?>:</label>
    <div class="switch-wrapper" id="languageSelect">
        <strong class="current language-<?php echo $this->htmlEscape(Mage::app()->getStore()->getCode()) ?>">
            <?php echo $this->htmlEscape(Mage::app()->getStore()->getName()) ?>
        </strong>
        <span class="switcher-holder">(<span onclick="popUpMenu(this);" class="switcher"><?php echo $this->__('Change')?></span>)</span>
        <ul style="display:none" id="popId-languageSelect">
            <li class="current language-<?php echo $this->htmlEscape(Mage::app()->getStore()->getCode()) ?>">
                <span><?php echo $this->htmlEscape(Mage::app()->getStore()->getName()) ?></span>
            </li>   
            <?php foreach ($this->getStores() as $_lang): ?>
                <?php if($_lang->getId()!=$this->getCurrentStoreId()): ?>
                    <li class="language-<?php echo $this->htmlEscape($_lang->getCode()); ?>">
                        <a href="<?php echo $_lang->getCurrentUrl() ?>"><?php echo $this->htmlEscape($_lang->getName()) ?></a>
                    </li>
                <?php endif; ?>
            <?php endforeach; ?>
        </ul>
    </div>
</div>
<?php endif; ?>

Upvotes: 1

1000Nettles
1000Nettles

Reputation: 2334

You're very close, you just need to include the URL!

<?php if(count($this->getStores())>1): ?>
<div class="form-language">
<?php foreach ($this->getStores() as $_lang):?>
    <a href="<?php echo $_lang->getCurrentUrl() ?>" title="<?php echo $this->htmlEscape($_lang->getName()) ?>"><?php echo $this->htmlEscape($_lang->getName()) ?></a>
<?php endforeach;?>
</div>
<?php endif; ?>

Upvotes: 4

Related Questions