Locale Aware Number Conversion In Javascript
Solution 1:
You could use a simple function like this one for the translation from Arabic numerals:
functiontranslateNumerals(input,target) {
varsystems= {
devanagari:2406, tamil:3046, kannada:3302,
telugu:3174, marathi:2406, malayalam:3430,
oriya:2918, gurmukhi:2662, nagari:2534, gujarati:2790
},
zero=48, //charcodeforArabiczeronine=57, //charcodeforArabicnineoffset=(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);
}
}
returnoutput.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
};
returnfunction (target) {
// for each selected element...returnthis.each(function () {
// ...look at each child node and
$(this).contents().each(function () {
// ...translate text nodes, recurse into elementsif (this.nodeType === this.TEXT_NODE) {
$(this).text(translateNumerals($(this).text(), target));
} elseif (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:
functiontranslateNumerals(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" ) {
returninput;
}
input = input.toString();
offset = systems[target] - systems[source];
zero = systems[source];
nine = systems[source] + 9;
for (var i=0, l=input.length; i<l; i++) {
var char = input.charCodeAt(i);
if (char >= zero && char <= nine) {
output.push( String.fromCharCode(char + offset) );
} else {
output.push( input[i] );
}
}
returnoutput.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 ১৩.৫৬"
Solution 2:
I think your best bet is the jQuery Globalize plugin: https://github.com/jquery/globalize
Post a Comment for "Locale Aware Number Conversion In Javascript"