问题描述
是否有 jquery 插件 可用于促进 number 本地化?也就是说,插件应该将数字翻译成它们的本地字形.
<上一页>阿拉伯语 |???? |1234印度语(泰卢固语/印地语) |?????/????? |12345ps:我的要求是数字转换,而不是格式化.
您可以使用像这样的简单函数来翻译来自阿拉伯数字:
function translatenumerals(input, target) {变种系统 = {梵文:2406,泰米尔文:3046,卡纳达文:3302,泰卢固语:3174,马拉地语:2406,马拉雅拉姆语:3430,oriya: 2918, gurmukhi: 2662, nagari: 2534, 古吉拉特语: 2790},zero = 48,//阿拉伯零的字符代码九 = 57,//阿拉伯九的字符代码offset = (systems[target.tolowercase()] || zero) - 零,输出 = input.tostring().split(""),我, l = 输出.长度, cc;对于 (i = 0; i < l; i ) {cc = 输出[i].charcodeat(0);如果 (cc >= 零 && cc <= 九) {输出[i] = string.fromcharcode(cc offset);}}返回 output.join("");}
然后这样称呼它
translatenumerals(12345, "telugu")//"?????"translatenumerals(12345, "梵文")//"?????"translatenumerals("foo", "devanagari")//"foo"
到阿拉伯数字的翻译同样简单.它只需要多一点管道.
上述函数知道任何目标系统中零的 charcode,并进行一些简单的数学运算,将阿拉伯数字逐个字符转换为该系统.
反向函数需要检查输入的每个字符.如果它落在任何源系统的 charcode 范围(即相应的零 9 的 charcode),它就知道所需的偏移量.减去偏移量将使您进入阿拉伯语范围.这很简单,我会留给你.
<小时>编辑#1:既然你提到了jquery,上面可以做成这样的插件:
jquery.fn.extend({texttranslatenumerals: (function () {var translatenumerals = 函数(输入,目标){//上面的函数};返回函数(目标){//对于每个选定的元素...返回 this.each(function () {//...查看每个子节点并$(this).contents().each(function () {//...翻译文本节点,递归成元素if (this.nodetype === this.text_node) {$(this).text(translatenumerals($(this).text(), target));} else if (this.nodetype === this.element_node) {$(this).texttranslatenumerals(target);}});});};})()});
<小时>
编辑#2: fwiw,这是一个可以从任何源系统转换为任何目标系统的函数:
function translatenumerals(input, source, target) {变种系统 = {阿拉伯语:48,梵文:2406,泰米尔语:3046,卡纳达语:3302,泰卢固语:3174,马拉地语:2406,马拉雅拉姆语:3430,奥里亚语:2918,古尔穆基语:2662,纳加里语:2534,古吉拉特语:2790,},输出 = [],偏移量 = 0,零 = 0,九 = 0,字符 = 0;源 = source.tolowercase();目标 = target.tolowercase();if ( !(系统中的源 && 系统中的目标)||输入 == 空||typeof 输入 == "未定义" ||typeof 输入 == "对象" ) {返回输入;}输入 = input.tostring();偏移量 = 系统 [目标] - 系统 [源];零 = 系统 [来源];九 = 系统[来源] 9;for (var i=0, l=input.length; i= 零 && char <= 九) {output.push(string.fromcharcode(char offset));} 别的 {输出.push(输入[i]);}}返回 output.join("");}
示例输出
translatenumerals("0123456789", "arabic", "malayalam")//"??????????"translatenumerals("??????????", "马拉雅拉姆语", "阿拉伯语")//"0123456789"translatenumerals("??????????", "malayalam", "kannada")//"??????????"translatenumerals("??????????", "kannada", "nagari")//"??????????"translatenumerals("??????????", "kannada", "gujarati")//"??????????"translatenumerals("usd 13.56", "arabic", "nagari")//"usd ??.??"
is there a jquery plugin available that facilitates number localization ? that is , the plugin should translate numerals into their local glyphs.
arabic | ???? | 1234 indic (telugu/hindi) | ?????/????? | 12345
ps : my requirement is number conversion , not formatting .
you could use a simple function like this one for the translation from arabic numerals:
function translatenumerals(input, target) { var systems = { devanagari: 2406, tamil: 3046, kannada: 3302, telugu: 3174, marathi: 2406, malayalam: 3430, oriya: 2918, gurmukhi: 2662, nagari: 2534, gujarati: 2790 }, zero = 48, // char code for arabic zero nine = 57, // char code for arabic nine offset = (systems[target.tolowercase()] || zero) - zero, output = input.tostring().split(""), i, l = output.length, cc; for (i = 0; i < l; i ) { cc = output[i].charcodeat(0); if (cc >= zero && cc <= nine) { output[i] = string.fromcharcode(cc offset); } } return output.join(""); }
and call it like this
translatenumerals(12345, "telugu") // "?????" translatenumerals(12345, "devanagari") // "?????" translatenumerals("foo", "devanagari") // "foo"
the translation to arabic numerals is equally simple. it requires just a little more plumbing.
the above function knows the charcode of the zero in any target system and does some simple math to do a character-by-character conversion from arabic numerals to that system.
a reverse function would need to check each character of the input. if it falls the charcode range of any source system (i.e. the charcode of the respective zero 9) it knows the required offset. subtracting the offset will get you into the arabic range. this is pretty trivial and i'll leave it to you.
edit #1: since you mentioned jquery, the above could be made into a plugin like this:
jquery.fn.extend({ texttranslatenumerals: (function () { var translatenumerals = function (input, target) { // the above function }; return function (target) { // for each selected element... return this.each(function () { // ...look at each child node and $(this).contents().each(function () { // ...translate text nodes, recurse into elements if (this.nodetype === this.text_node) { $(this).text(translatenumerals($(this).text(), target)); } else if (this.nodetype === this.element_node) { $(this).texttranslatenumerals(target); } }); }); }; })() });
edit #2: fwiw, here is a function that can convert from any source system into any target system:
function translatenumerals(input, source, target) { var systems = { arabic: 48, devanagari: 2406, tamil: 3046, kannada: 3302, telugu: 3174, marathi: 2406, malayalam: 3430, oriya: 2918, gurmukhi: 2662, nagari: 2534, gujarati: 2790, }, output = [], offset = 0, zero = 0, nine = 0, char = 0; source = source.tolowercase(); target = target.tolowercase(); if ( !(source in systems && target in systems) || input == null || typeof input == "undefined" || typeof input == "object" ) { return input; } input = input.tostring(); offset = systems[target] - systems[source]; zero = systems[source]; nine = systems[source] 9; for (var i=0, l=input.length; i= zero && char <= nine) { output.push( string.fromcharcode(char offset) ); } else { output.push( input[i] ); } } return output.join(""); }
sample outputs
translatenumerals("0123456789", "arabic", "malayalam") // "??????????" translatenumerals("??????????", "malayalam", "arabic") // "0123456789" translatenumerals("??????????", "malayalam", "kannada") // "??????????" translatenumerals("??????????", "kannada", "nagari") // "??????????" translatenumerals("??????????", "kannada", "gujarati") // "??????????" translatenumerals("usd 13.56", "arabic", "nagari") // "usd ??.??"