博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
jQuery资源国际化
阅读量:6869 次
发布时间:2019-06-26

本文共 4052 字,大约阅读时间需要 13 分钟。

一、jQuery.i18n.properties

1. jQuery.i18n.properties采用.properties文件对JavaScript进行国际化。

jQuery.i18n.properties插件首先加载默认的资源文件(filename.properties),然后加载针对特定语言环境的资源文件(filename_zh.properties),这就保证了在未提供某种语言的翻译时,默认值始终有效。

2. filename的命名方式:

filename.properties(默认定义)

filename_language.properties(自定义语言)
filename_language_country.properties(自定义语言加国家)

3. 应用说明:

//引入jQuery.i18n.properties插件:/*** Load and parse message bundle files (.properties),* making bundles keys available as javascript variables.** i18n files are named 
.js, or
_
.js or
_
_
.js* Where:* The
argument is a valid ISO Language Code. These codes are the lower-case,* two-letter codes as defined by ISO-639. You can find a full list of these codes at a* number of sites, such as: http://www.loc.gov/standards/iso639-2/englangn.html* The
argument is a valid ISO Country Code. These codes are the upper-case,* two-letter codes as defined by ISO-3166. You can find a full list of these codes at a* number of sites, such as: http://www.iso.ch/iso/en/prods-services/iso3166ma/02iso-3166-code-lists/list-en1.html** Sample usage for a bundles/Messages.properties bundle:* $.i18n.properties({* name: 'Messages',* language: 'en_US',* path: 'bundles'* });* @param name (string/string[], optional) names of file to load (eg, 'Messages' or ['Msg1','Msg2']). Defaults to "Messages"* @param language (string, optional) language/country code (eg, 'en', 'en_US', 'pt_BR'). if not specified, language reported by the browser will be used instead.* @param path (string, optional) path of directory that contains file to load* @param mode (string, optional) whether bundles keys are available as JavaScript variables/functions or as a map (eg, 'vars' or 'map')* @param cache (boolean, optional) whether bundles should be cached by the browser, or forcibly reloaded on each page load. Defaults to false (i.e. forcibly reloaded)* @param encoding (string, optional) the encoding to request for bundles. Property file resource bundles are specified to be in ISO-8859-1 format. Defaults to UTF-8 for backward compatibility.* @param callback (function, optional) callback function to be called after script is terminated*/ jQuery.i18n.properties({ name : 'filename', //资源文件名称 path : '${contentsPath}/static/i18n/', //资源文件路径 mode : 'map', //用Map的方式使用资源文件中的值 language : 'zh', callback : function() { //加载成功后设置显示内容 console.info($.i18n.prop('key'));// key 为properties文件中定义的键值对相应的key,这样既数据key对应的value }});// filename.properties中定义的类容// name=zivs_en// filename_zh.properties中定义的类容// name=zivs_zh//使用$("#my_input").val($.i18n.prop('name'));//获取key=name的value: $.i18n.prop('name')复制代码

4. 总结:

若使用的filename.properties是项目的resource目录中已定义的话,需要将相应.properties文件copy一份到项目static目录(resource目录是在Service端,受保护,Client端不能直接获取),这样也来同一份properties文件就会存在两份。

二、模板(Freemarker)嵌入使用

将资源国际化message定义成一个jQuery变量(在FreeMarker模板中),在将变量通过模板嵌入页面。这样页面就会存在一个动态引入的变量。

1. 定义模板变量:

//i18n配置文件 //新建文件 syssetting_cn.properties和syssetting_en.properties分别如下:syssetting_cn.properties	sys.hint.timeout=请求超时	sys.hint.optionFail=操作失败	sys.hint.serviceException=服务异常syssetting_en.properties	sys.hint.timeout=Request Timeout	sys.hint.optionFail=Operation Failed	sys.hint.serviceException=Service Exception//新建文件i18n_message.ftl 类容如下:i18n_message<#macro i18nMessage>    var i18n_message = {            serviceException : "<@spring.message code="sys.hint.serviceException"/>",	    // <@spring.message code="sys.hint.serviceException"/>Spring i18n 的页面书写方式            optionFail : "<@spring.message code="sys.hint.optionFail"/>",            timeout : "<@spring.message code="sys.hint.timeout"/>"        };
<#-- 在相应的页面引入freemarker宏模板 --><#import "i18n_message.ftl" as i18n_message />复制代码

2. 页面引入模板变量:

<#--页面引入定义的 i18n_message 资源数据--><@i18n_message.i18nMessage/>复制代码

3. 获取值:

<#--页面直接获取 i18n_message 定义的key为 serviceException 的值-->console.info(i18n_message.serviceException);复制代码

4. 总结:

filename.properties在系统中只存在唯一一份,保证了数据的唯一性。

转载于:https://juejin.im/post/5acafca6f265da239f07ab0d

你可能感兴趣的文章
Java NIO使用及原理分析 (一)
查看>>
「技术层面」剖析B2B供应链系统技术架构的部署方案 ...
查看>>
《淘宝网开店 拍摄 修图 设计 装修 实战150招》一一1.3 数码相机日常使用注意事项...
查看>>
《程序员之禅》一一10.10 淡泊宁静
查看>>
《MATLAB图像处理超级学习手册》一一2.1 矩阵的创建
查看>>
还在用密码登录 ECS?ECS 控制台更安全的 SSH 密钥对 来了
查看>>
哪种编程语言最流行?而这到底又意味着什么
查看>>
为什么企业不对 GPL 侵权采取法律行动
查看>>
《Spark大数据分析实战》——第1章Spark简介
查看>>
5.5确认范围
查看>>
Chris Grainger:我们如何才能更好地编程?
查看>>
以黑客教主之名,TK 发现 Windows 史上最大漏洞
查看>>
《IPv6精髓(第2版)》——导读
查看>>
《Windows Server 2012 Hyper-V虚拟化管理实践》一1.2 Hyper-V安装前后的变化
查看>>
Proxmox VE 4.4 发布,新 Ceph 仪表盘上线
查看>>
《CCNP TSHOOT(642-832)学习指南》一1.2 维护进程及维护流程
查看>>
华为宣布开源流处理平台查询语言 StreamCQL
查看>>
2016 年 6 月 RedMonk 编程语言排行榜
查看>>
《Adobe Photoshop CC经典教程(彩色版)》—第1课1.4节在Photoshop中还原操作
查看>>
HttpClient使用详解
查看>>