magento 翻译使用实例

在自定义的模块中若想要使用翻译,需在config.xml中加入如下配置

<config>    
    <adminhtml>  //后台
        <translate>  
            <modules>  
                <Jago_translate>  
                    <files>  
                        <default>test.csv</default>  
                    </files>  
                </Jago_translate>  
            </modules>  
        </translate>  
    </adminhtml>  
    <frontend>  //前台
        <translate>  
            <modules>  
                <Jago_translate>  
                    <files>  
                        <default>test.csv</default>  
                    </files>  
                </Jago_translate>  
            </modules>  
        </translate>  
     </frontend>  
</config>   

以登陆界面为例

在app/code/core/Mage/Customer/etc/config.xml中我们可以看到这样的声明

        <translate>
            <modules>
                <Mage_Customer>
                    <files>
                        <default>Mage_Customer.csv</default>
                    </files>
                </Mage_Customer>
            </modules>
        </translate>    

那么只需在app/locale/zh_CN中建立Mage_Customer.csv文件,然后往里输入

"Log In","登陆"
"Login or Create an Account","登陆或创建新账号"

接着就是进入后台,在System->Configuration->General->Locale Options 

将Locale选成Chinses就可以了,打开页面可以看到如下效果

接下去讲讲为什么建立的文件夹名字叫zh_CN

这个的原因可以看appcodecoreMageCoreModelLocaleConfig.php,就不多说了。

最后讲一讲translate.csv文件

进入app/design/frontend/default/default/locale

然后建立一个zh_CN文件夹,在里面建立translate.csv,往里面写入

"Login or Create an Account","覆盖"

这时候重新刷新页面,就会发现界面变了

其实从这个文件放的位置就可以理解,这个csv文件是专门给所在的模板用的,当使用这个模板时,translate.csv里的翻译项会覆盖掉语言包里的同名项,也就是在我们第一步配置了翻译的情况下,这里的优先级会比较高。

而在编写程序时,若希望自己的语句会被翻译,在模板界面需这样编写

<?php echo $this->__('Login or Create an Account') ?>

 

原文地址:https://www.cnblogs.com/cangzhou/p/3754479.html