I made a translator for the English language. As of now it only translates from Spanish, Russian, and Arabic (will be adding more languages).
The program pulls the input from a yaml file with a list of words and phrases inside of it. It searches the users input and if it finds a match it returns with the English translation of the word or phrase!
require 'yaml'
def welcome
puts "Welcome to the Translation Center! Please enter the language you would like to translate to English or you can choose from the list below:
1. Spanish
2. Russian
3. Arabic"
input = language = gets.chomp!
case language
when /spanish/i
spanish_translate
when "1"
spanish_translate
when /russian/i
russian_translate
when "2"
russian_translate
when /arabic/i
arabic_translate
when "3"
arabic_translate
else
puts "Language is not yet supported within Translation Center."
exit
end
end
def spanish_translate
translations = YAML.load_file 'spanish.yml'
puts "Enter word or phrase to be translated to English, press 'Q' to quit:"
input = gets.chomp
case
when translations[input]
puts "The translation from Spanish to English is: #{translations[input]}"
when input =~ /q/i
exit
else
puts "Invalid word or phrase redirecting..."
spanish_translate
end
end
def russian_translate
translation = YAML.load_file 'russian.yml'
puts "Enter word or phrase you would like translated to English, press 'Q' to quit:"
input = gets.chomp!
case
when translations[input]
puts "The translation from Russian to English is: #{translations[input]}"
when input =~ /q/i
exit
else
puts "Invalid word or phrase redirecting..."
russian_translate
end
end
def arabic_translate
translation = YAML.load_file 'arabic.yml'
puts "Enter word or phrase you woukd like translated to English, press 'Q' to quit:"
input = gets.chomp!
input = gets.chomp!
case
when translations[input]
puts "The translation from Arabic to English is: #{translations[input]}"
when input =~ /q/i
exit
else
puts "Invalid word or phrase redirecting..."
arabic_translate
end
end
welcome
Here's the Spanish yaml file:
a: to abajo: down el abancio: fan buenos dias: good morning buenos tardes: good afternoon como se llama usted: what is your name como esta usted: how are you estoy bien: i am fine mucho gusto: nice to meet you perdoname: excuse me por favor: please gracias: thank you lo siento: i'm sorry salud: bless you de nada: you are welcome hay muchos: there are many si: 'yes' no: 'no' yo no comprendo: i do not understand yo quiero una tortilla: i want a tortilla
Russian yaml file:
и: and, though в: in, at не: not он: he на: on, it, at, to я: I что: what, that, why тот: that быть: to be с: with and, from, of а: while, and, but весь: all, everything это: that, this, it как: how, what, as, like
Arabic yaml file:
salam: hi سلام: hi Sabah el kheer: good morning صباح الخير: good morning massa el kheer: good evening مساء الخير: good evening marhaban: welcome مرحبا: welcome Kaifa haloka: how are you(male) kaifa haloki: how are you(female) كيف حالك؟: how are you ana bekhair shokran: i'm fine thanks أنا بخير شكر: im fine thanks wa ant: and you(male) wa anti: and you (female) و أنت؟: and you jayed: good 'aadee: so-so عادي: good جيد: so-so
It now has the ability to translate to three different languages.
How's this looking as an OOP language overall? What can I do differently next time?