Cordys HTML5 多语言代码 解读

10 篇文章 0 订阅
3 篇文章 0 订阅

 Cordys 多语言设计思路


<label for="inputName" data-translatable="true">
	User Name
</label>

 如果页面上需要翻译 则添加自定义标签。 data-translatable='true'   则可以利用JQuery 找到所有的对象$("data-translatable='true'")


 var selector = "[data-translatable='true']";
 $(selector).each(function () {
	var $this = $(this);
	var label_name=$this.text().trim();//得到label name,然后利用label name去匹配,把对应的值找出来。
	$this.text(getMessage($this.text().trim()));
 }

 //利用Ajax读取本地文件
 $.ajax({
	type: "GET",
	url: "translation/html5sdk/sdkmessagebundle_zh-CN.xml",
	async: true,
	cache: true,
	success : function(response){
		res=response;
	}
});

//将本地文件转为Json数组
$.cordys.json.xml2js(res)
//找到某个对象
dictionary=$.cordys.json.find($.cordys.json.xml2js(res), "dictionary");
//
var self = this;
this.path = path;
this.dictionary = dictionary;
this.getMessage = function () {
	var id = arguments[0],
	label = null,
	ttext = "";
	if (self.dictionary) {
		label = $.cordys.json.find(self.dictionary, "@textidentifier", id);
	}
	ttext = label ? (label[language] ? (label[language].text || label[language]) : id) : id;
	if (arguments.length > 1) {
		var args = Array.prototype.slice.call(arguments).slice(1);
		ttext = ttext.replace(/\{(\d+)\}/g, function () {
				return typeof(args[arguments[1]]) !== "undefined" ? args[arguments[1]] : arguments[0];
			});
	}
	return ttext;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值