jquery.i18n.properties使用小记

http://blog.sina.com.cn/s/blog_5919afb30100fw84.html

项目主页: https://code.google.com/p/jquery-i18n-properties/

一直在寻找js的i18n解决方案,今天使用了一下jquery.i18n.properties觉得很不错,向大家推荐一下。

在jquery.1.3.2下调试过程

1、在js/i18n/目录下创建message.properties,message_zh_CN.properties,message_en_US.properties三个文件,推荐使用eclipse下的插件resourcebundleeditor编辑文档,文档存为ISO-8859-1或utf-8.
英文文件中插入
   msg_hello = Hello
    msg_world = World
    msg_complex = Good morning {0}!

中文同上就不写了。
2、在使用前初始化jquery.i18n.properties。
 
// This will initialize the plugin
// and show two dialog boxes: one with the text "Olá World"
// and other with the text "Good morning John!"
jQuery.i18n.properties({
    name:'message', //使用资源文件名称
    path:'js/i18n/', //资源文件所在路径
    mode:'both',    //调用方式,可用值‘vars’ (default) 变量方式, ‘map’map方式 or ‘both’两者均支持
    language:'en_US', //语言
    callback: function() {
        //回调函数
        // We specified mode: 'both' so translated values will be
        // available as JS vars/functions and as a map

        // Accessing a simple value through the map 通过map调用
        jQuery.i18n.prop('msg_hello');
        // Accessing a value with placeholders through the map 带参数Map调用,john会替换相应参数,支持多个参数
        jQuery.i18n.prop('msg_complex', ['John']);

        // Accessing a simple value through a JS variable通过变量调用
        alert(msg_hello +' '+ msg_world);
        // Accessing a value with placeholders through a JS function通过变量函数方式替换占位符,支持多个参数,不过在360浏览器下测试失败,在chrome下成功
        alert(msg_complex('John'));
    }
});

使用心得:
建议使用工具编辑i18n文件,方便。
为了获得最好的兼容性,还是使用map方式访问吧

猜你喜欢

转载自panyongzheng.iteye.com/blog/1851321
今日推荐