A problem you'll have to tackle first is translating a sentence as a string, to an array of words (which could be a question by itself). Once you have this array of words (let's call it words
), loop through the words and translate each word to its Italian counterpart. Ofcourse, just pasting the translated words together will form a sentence different than the original one (as you'll ignore punctuation, spaces, ...). e.g. "hey, my name is mario" will translate to "ciao mio nome is Mario" (mention the missing comma).
To solve this, you could replace each translated word in the original sentence, to its Italian counterpart, which also retains the original words not in the translation. This produces the following code:
english_italian = {"hey": "ciao", "my": "mio", "name": "nome"}
sentence = "ciao mio nome is Mario"
words = sentence_to_words(sentence) # ["ciao", "mio", "nome", "is", "Mario"]
for word in words:
if word in english_italian:
sentence = sentence.replace(word, english_italian[word])
An optimalization could be to first remove duplicates from the words array, so you don't translate the same word more than once.
All and all, translating like this will not work that well (think about verb conjugation, different order of parts in the sentence such as the subject and verbs, grammatical differences, etc.).