简体   繁体   中英

Multilingual Text Calculator in Javascript

I'm trying to put together a Multilingual Text Calculator using just plain Javascript (not JQuery) as this is to help with revision on a past paper for uni. I've had a good go at it and created a version that does work based on two languages - English and French, however not without its flaws.

How would I go about creating a two dimensional array for each language? So for example if I wanted to do:

myEnglishArray = ["one", 1];

or

myEnglishArray = ["one", "1"];

I don't even know if thats correct or not but basically I need to go from 1 to 10 in English and the same with french and map each number in textual sense (ie "one") to the number in integer sense. I could of course convert a number within a string (ie "1") using parseInt();

Any help would be great here!

I'd write it like this:

var myArray = {
    "english": { "one": 1, "two": 2, "three": 3, "four": 4, ... },
    "spanish": { "uno": 1, "dos": 2, "tres": 3, "quattro": 4, ... },
    ...
};

That way you can access each array by name:

console.log(myArray.english["one"]);  // 1
console.log(myArray.spanish["uno"]);  // 1

And cycle through the numbers using for :

for (var num in myArray.spanish) {
    console.log(num + " = " + myArray.spanish[num]);   // "uno" = 1, etc
}
var dictionary = 
    [["one", "uno"],
     ["two", "dos"],
     ["three", "tres"]];

dictionary[0][0];  //"one"
dictionary[0][1];  //"uno"

Here you just have to create 1 variable to contain 3 information at a time (number, English, Spanish). And also, you can easily add more languages after Spanish too!

DEMO: http://jsfiddle.net/DerekL/5fhTw/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM