1

我想让一个多语言的 magento 站点使用标志图像而不是语言选择框供用户选择页面语言。在http://www.atwix.com/magento/replace-language-selector-flag-icons/上有一篇很好的文章, 唯一的问题是我们使用“将商店代码添加到 url”选项。我破解了这段代码,但它可以使用一些改进并使它看起来更像 Magento。

    <?php if(count($this->getStores())>1): ?>
    <div class="form-language">
        <div class="langs-wrapper">
        <?php foreach ($this->getStores() as $_lang): ?>
            <?php if ($_lang->getCode() != 'default'): ?>   
            <?  
                $base_url = Mage::getBaseUrl();
                // remove language in base url
                $base_url = str_replace('/en/' , "" , $base_url);       
                $base_url = str_replace('/fr/' , "" , $base_url);                   
                $current_url = $this->helper('core/url')->getCurrentUrl();
                // take out base url and language code
                $rest_of_url = str_replace($base_url , "" , $current_url);                  
                $rest_of_url = str_replace('/en/' , "" , $rest_of_url);     
                $rest_of_url = str_replace('/fr/' , "" , $rest_of_url);      
                // assmble new url
                $new_url = $base_url . '/' . $_lang->getCode()  . '/' . $rest_of_url;                       
            ?>      
            <a class="lang-flag" href="<?php echo $new_url ;?>"><img src="<?php echo $this->getSkinUrl('images/flags/' . $_lang->getCode() . '.png');?>" alt=""></a>
            <?php endif;?>
        <?php endforeach;?>
        </div>
    </div>
    <?php endif;?>
4

1 回答 1

3

模板文件(路径/到/模板/file.phtml):

<?php if(count($this->getStores()) > 1): ?>
<div class="form-language">
    <?php foreach ($this->getStores() as $_lang): ?>
        <?php $selected = $_lang->getId() == $this->getCurrentStoreId() ?>
        <a class="lang-flag<?php $selected && print ' selected' ?>" href="<?php echo $_lang->getCurrentUrl() ?>">
            <img src="<?php echo $this->getSkinUrl('images/flags/' . $_lang->getCode() . '.png');?>" alt="<?php echo $this->htmlEscape($_lang->getName()) ?>">
        </a>
    <?php endforeach ?>
</div>
<?php endif ?>

将此行添加到您的布局更新 xml 文件(如果您尚未定义 switch 块):

<block type="page/switch" name="lang.switcher" template="path/to/template/file.phtml" />

如果您已经拥有一个:

<reference name="store_language">
    <action method="setTemplate"><tmpl>path/to/template/file.phtml</tmpl></action>
</reference>
于 2012-07-02T21:14:16.100 回答