我希望多语言magento站点使用标志图像而不是语言选择器框来供用户选择页面语言。在http://www.atwix.com/magento/replace-language-selector-flag-icons/有一篇很好的文章 唯一的问题是我们使用“将商店代码添加到网址”选项。我破解了这段代码,但它可以使用一些改进,让它更像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;?>
答案 0 :(得分:3)
模板文件(path / to / template / 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 ?>
将此行添加到layout-update 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>