1. type require 'yaml' at the top of your code.
2. Next type MESSAGES = YAML.load_file('calculator.yml')
3. For example your YAML file is like this,
welcome: "Welcome to Calculator! Enter your name:" valid_name: "Make sure to end a valid name."
Then, all your MESSAGES is a hash file like this.
{ "welcome" => "Welcome to Calculator! Enter your name:" ,
"valid_name" => "Make sure to end a valid name." }
You can extract the message with code like this:
MESSAGES["welcome"]
3. If you want to internationalize your program. Your yaml file will have another layer of language keys like en or es
en: welcome: "Welcome to Calculator! Enter your name:" valid_name: "Make sure to enter a valid name." es: welcome: "Bienvenido a la calculadora! Entre su nombre:" valid_name: "Asegúrese de entrar un nombre válido."
Then, your new hash will look like this.
{"en"=>{"welcome"=>"Welcome to Calculator! Enter your name:",
"valid_name"=>"Make sure to enter a valid name."},
"es"=>{"welcome"=>"Bienvenido a la calculadora! Entre su nombre:",
"valid_name"=>"Asegúrese de entrar un nombre válido."}}
You can only extract the message with code like this:
MESSAGES["en"]["welcome"]
2. Next type MESSAGES = YAML.load_file('calculator.yml')
3. For example your YAML file is like this,
welcome: "Welcome to Calculator! Enter your name:" valid_name: "Make sure to end a valid name."
Then, all your MESSAGES is a hash file like this.
{ "welcome" => "Welcome to Calculator! Enter your name:" ,
"valid_name" => "Make sure to end a valid name." }
You can extract the message with code like this:
MESSAGES["welcome"]
3. If you want to internationalize your program. Your yaml file will have another layer of language keys like en or es
en: welcome: "Welcome to Calculator! Enter your name:" valid_name: "Make sure to enter a valid name." es: welcome: "Bienvenido a la calculadora! Entre su nombre:" valid_name: "Asegúrese de entrar un nombre válido."
Then, your new hash will look like this.
{"en"=>{"welcome"=>"Welcome to Calculator! Enter your name:",
"valid_name"=>"Make sure to enter a valid name."},
"es"=>{"welcome"=>"Bienvenido a la calculadora! Entre su nombre:",
"valid_name"=>"Asegúrese de entrar un nombre válido."}}
You can only extract the message with code like this:
MESSAGES["en"]["welcome"]
Comments
Post a Comment