Last active
December 26, 2015 20:19
-
-
Save nootanghimire/7208124 to your computer and use it in GitHub Desktop.
A localization mapping logic. Check http://jsfiddle.net/V92cK/5/ for a demo.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Obj = '{"my":"मेरो","name": "नााम","is-xyz": "xyz-हो",".":"।","1":"१","2":"२","3":"३","4":"४","5":"५","6":"६","7":"७","8":"८","9":"९","0":"०"}'; | |
//^^ The Localisation Objecy (Read from file or any other source) | |
ObjReal = JSON.parse(Obj); //Parsing the JSON | |
//The function starts | |
var l10n = function(localeObject, content){ | |
var arr = content.split(" ").reverse(); //Split by space. and reverse (actually non-reversed) the order | |
var newArr =[]; //Container | |
for (var i = arr.length - 1; i >= 0; i--) { | |
// if the word does not have one-to-one mapping: | |
if(localeObject[arr[i]] == undefined){ | |
if(isNaN(Number(arr[i]))){ | |
//^^ if the word is not a number | |
newArr.push(arr[i]); //do not translate. | |
} else { //if the word is collection of digits | |
var nums = arr[i].split('').reverse(); //Same old split and (non)-reverse | |
var convNums = []; //Same old container | |
for (var j = nums.length - 1; j >= 0; j--) { | |
convNums.push(localeObject[nums[j]]); | |
//iterate and push digits | |
} | |
newArr.push(convNums.join('')); //Make those digits a word! :) | |
} | |
} else { //This means the word has a one-to-one mapping: | |
newArr.push(localeObject[arr[i]]); | |
} | |
} | |
return newArr.join(" "); //Join all with spaces. | |
}; | |
//The magic | |
alert(l10n(ObjReal, "my name is-xyz as . 123 456 7 8")); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment