2

I'm looking for a way to add the language switcher to my top links menu. My store has 2 languages and I've modified the languages.phtml file to show the alternate language.

Example: if you are on the English site, then the language switcher will show French as a clickable option to bring you to that store view.

I know that in order to add options to the top links menu, you need to modify the customer.xml file. I know how to add alternate links as I've read plenty of "how to's" however none cover how to add the language bar.

Here is the modifications to languages.phtml

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

The end result should be:

Login | My Cart | Compare | Wish List | LANGUAGE

How can I add the language bar to the top links menu items?

UPDATE

I've tried adding the following to the customer.xml file under :

<block type="page/switch" name="store_language" as="store_language" template="page/switch/languages.phtml"/>

Nothing appears.

9
+50

Create your extension with it's own layout file and block that adds links to the top menu.
Let's call that Easylife_Locale. You will need the following files:

app/etc/modules/Easylife_Locale.xml - the declaration file

<?xml version="1.0"?>
<config>
    <modules>
        <Easylife_Locale>
            <codePool>local</codePool>
            <active>true</active>
            <depends>
                <Mage_Core />
            </depends>
        </Easylife_Locale>
    </modules>
</config>

app/code/local/Easylife/Locale/etc/config.xml - the configuration file

<?xml version="1.0"?>
<config>
    <modules>
        <Easylife_Locale>
            <version>1.0.0</version>
        </Easylife_Locale>
    </modules>
    <global>
        <blocks>
            <easylife_locale>
                <class>Easylife_Locale_Block</class>
            </easylife_locale>
        </blocks>
    </global>
    <frontend>
        <layout>
            <updates>
                <easylife_locale>
                    <file>easylife_locale.xml</file>
                </easylife_locale>
            </updates>
        </layout>
    </frontend>
</config>

app/design/frontend/base/default/layout/easylife_locale.xml - the frontend layout file

<?xml version="1.0"?>
<layout>
    <default>
        <reference name="top.links">
            <block type="easylife_locale/stores" name="stores_links">
                <action method="addStoreLinks"></action>
            </block>
        </reference>
    </default>
</layout>

app/code/local/Easylife/Locale/Block/Stores.php - the block that adds the links to the top.

<?php

class Easylife_Locale_Block_Stores extends Mage_Page_Block_Switch {
    public function addStoreLinks() {
        $parentBlock = $this->getParentBlock();
        if ($parentBlock) {
            $position = 200; //start position
            foreach ($this->getStores() as $store) {
                $params = null;
                if ($store->getId() == $this->getCurrentStoreId()) {
                    $params = 'class="current-store"';
                    //if you want to skip the current store view just uncomment the next line
                    //continue;
                }
                //Print the language code (en, fr)
                $text = substr(Mage::app()->getLocale()->getLocaleCode(), 0, 2);
                //if your want to display the store name comment the line above and uncomment the one below
                //$text = $store->getName();
                $title = $store->getName();

                $parentBlock->addLink($text, $store->getCurrentUrl(), $title, false, array(), $position, null, $params);
                $position += 10;
            }
        }
        return $this;
    }
}

Clear the cache and try it out.

  • 1
    Really happy to meet you in a couple weeks in Vegas, man! – benmarks Apr 28 '14 at 1:40
  • 1
    @benmarks. I'm looking forward to meeting a lot of people in Vegas. See you there. – Marius Apr 28 '14 at 6:30
  • Got this to work, but how can I only display the alternate language? Currently it shows both available languages. If on English view I'd want to show French as a click-able option and on French view English as a click-able option. Really appreciate it Marius! – SR_Magento Apr 28 '14 at 12:07
  • @sr_magento. See my comment in the code if you want to skip the current store view just uncomment the next line. Below that comment there is a line that says continue;. Uncomment that line and then the current store view will be skipped when showing the links. This way when viewing the En store view you will see only the Fr link and the other way around. – Marius Apr 28 '14 at 12:10
0

Well if you look in these 2 templates: base/default/template/page/switch/flags.phtml and base/default/template/page/switch/languages.phtml you will see that the magic is all about the loop using $this->getStores().

Both phtmls are blocks from Mage_Page_Block_Switch and the method getStore() is implemented there... You can just create your from this class

<block type="page/switch" template="your_phtml" />

and use it where you need.

  • I've tried to implement your suggestion in the customer.xml in the layout and it's just not working. Any other suggestions? – SR_Magento Apr 25 '14 at 20:11
0

Try:

<default>
    <reference name="header">
        <action method="unsetChild"><name>store_language</name></action> <!--not name, is in fact alias -->
        <reference name="top.links">
            <action method="insert"><blockName>store_language</blockName></action> <!-- block name -->
        </reference>
    </reference>
</default>

/* in page/template/links.phtml */
echo $this->getChildHtml('store_language');

If it's good than try to set a different template than page/template/links.phtml for top.links so you won't have the call to the child block in all blocks that use Mage_Page_Block_Template_Links, like in footer links which is cached.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.