jQuery:繁简转换
一般JS的繁简转换的原理是列出所有繁简中文字型不同的字,然后在网页注意查找这些字,然后替换掉。当然这样机械式的转换不会转出什么好的结果,如前面的面就会转成面条的面(麵)。
//传统的:
var s="万与丑专业丛东丝丢两严丧个丬丰临为丽举么义乌乐 ... ";
var t="萬與醜專業叢東絲丟兩嚴喪個爿豐臨為麗舉麼義烏樂 ... ";
//我的做法:
//simp_trad.json:
// {"\u4e07":"\u842c","\u4e0e":"\u8207","\u4e11":"\u919c", ... }
//用jQuery:
var dx,dy;
$.getJSON('simp_trad.json',function(d){
dx=d;
dy=new Array();
$.each(d,function(a,b){
dy[b]=a;
});
/* 示例 */
$('title, body > *').each(function(){
switch(true){
case !!$(this).html():
$(this).html(simp_to_trad($(this).html()));
}
});
});
function simp_to_trad(input){
if(input.match(/[\u4e00-\u9fa5]/g)){
input=input.replace(/[\u4e00-\u9fa5]/g,function(w){
return dx[w]||w;
});
}
return input;
}
function trad_to_simp(input){
if(input.match(/[\u4e00-\u9fa5]/g)){
input=input.replace(/[\u4e00-\u9fa5]/g,function(w){
return dy[w]||w;
});
}
return input;
}
下载simp_trad.zip,包含simp_trad.json
No related posts.
