0

I would like to make a multilingual tool in JavaScript. I need to match words with their pair in other language. I would like to use 3 different languages. User inputs names of vegetables, chooses one of several languages and gets the right output.

 var VegetablesInput = ["tomato", "potato", "cucumber", "carrot"]
 var Italian = ["pomodoro", "patata", "cetriolo", "carota"]
 var Croatian = ["rajčica", "krumpir", "krastavac", "mrkva"]
 var Spanish = ["tomate", "patata", "pepino", "zanahoria"]
 var languageInput = prompt ("Choose Italian, Croatian or Spanish.");
 var languageInput = languageInput.toLowerCase();
   function MatchTheWord (language, word) if (languageInput=="italian" && 
 WordInput 
 == "croatian" && WordInput == "spanish") {
 }

I would like to match an element of the VegetablesInput array with the right word according to the language chosen. But I am not sure how to do it simply by looping through the arrays inside the function. Any help would be appreciated.

5
  • Syntax: function functionName(params) { code }. Put { between ) and if, and remove from after "Spanish"). Also, if you toLowerCase(), then don't capitalize in the if` ("Italian", for example). = for starters. Commented Jul 6, 2020 at 4:52
  • After verifying language, use indexOfWord = VegetablesInput.indexOf(word), and if -1, then "Not found", else, return language[indexOfWord]. May I recommend putting the language arrays in an object with key as language? translatedWords={"italian":["pomodoro", "patata", "cetriolo", "carota"], "croatian":["rajčica", "krumpir", "krastavac", "mrkva"], "spanish":["tomate", "patata", "pepino", "zanahoria"]} - then, if language=="italian" and word=="potato", translatedWords[language][VegetablesInput.indexOf(word)] will return "patata". Commented Jul 6, 2020 at 5:03
  • @iAmOren Just write an answer. Those comment blobs are hopelessly difficult to read, especially for someone that isn't code savvy Commented Jul 6, 2020 at 5:14
  • @charlietfl, Thanks - I'm with you on that, but I've had answers marked down - "SO is not a code writing service"... I see that Pavel wrote a pretty good answer similar to (the comment form of) mine. Commented Jul 6, 2020 at 5:31
  • 1
    @iAmOren, Thank you for you advice to put the language arrays in an object with key as language. It works great. Commented Jul 7, 2020 at 0:20

1 Answer 1

1

Hope this help:

var Vegetables = {
  it: {
    tomato: "pomodoro",
    potato: "patata",
    cucumber: "cetriolo",
    carrot: "carota",
  },
  cr: {
    tomato: "rajčica",
    potato: "krumpir",
    cucumber: "krastavac",
    carrot: "mrkva",
  },
  sp: {
    tomato: "tomate",
    potato: "patata",
    cucumber: "pepino",
    carrot: "zanahoria",
  },
};
var languageInput = prompt("Choose Italian, Croatian or Spanish.");
var languageInput = languageInput.toLowerCase();
var wordInput = prompt("Choose Word: tomato, potato, cucumber, carrot.");

var translatedWord = Vegetables[languageInput][wordInput]
Sign up to request clarification or add additional context in comments.

5 Comments

Hi, Thank you everyone for your answers. However, it is not still clear for me how do I to match an element of the VegetablesInput array with the right word according to the language chosen (looping through the arrays inside the function). I am new to coding. I will appreciate if someone could explain what to do in this situation. What array method to use? And how to match them and return the right value. I would like to understand the principle behind it.
In this solution we didn't loop through array. Vegetables - it's an object with keys and values. Our object has depth 2. Upper level - language selector, second level - is word selector. Object type provide value accessor by bracket notation or dot notation. In our logic we can't access to value by dot notation because of dynamic key.
Pavel, does it mean that for this particular case I need to use the object method get.KeyByValue?
You can use array in this case, but it's not convenient. If array reach more than 100 words then it's difficult to manage indexes for each translatable word. Imagine that you have ten languages and you need to update some word.
I used the arrays of object and managed to make the translation using the for loop. Is it a way I can put all of this inside the function with return of the "translated word? Any advice would be apprecitated. My for loops look like that: for (var c=0; c<spanish.length; c++) {if (spanish[c].word == wordInput && languageInput == "spanish") {alert (result= spanish[c].translation); } };

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.