-1

I got a YAML file with the following structure containing country names in German:

countries:
      country_1:  "Schweiz"
      country_2:  "Deutschland"
      country_3:  "Italien"
      country_4:  "Frankreich"
      country_6:  "Afghanistan"

I can import the file to Ruby and save it as a Hash.

require 'yaml'
yml = YAML.load_file('countries.yml')
countries = yml['countries'] 

I want to translate them into French. I thought that this should be a fairly simple task using I18nCountryTranslations, but I'm stuck. Is there a way to do this with built-in Ruby tools? Or do I need the whole Rails framework for this? Or an external API?

sawa
  • 165,429
  • 45
  • 277
  • 381
Bergrebell
  • 4,263
  • 4
  • 40
  • 53

2 Answers2

3

You can use countries gem to get the translation

Just install the gem and add require 'countries' to your ruby file. You can follow the documentation here

c = ISO3166::Country.find_country_by_name('united states')
c.translations['fr'] #=> "États-Unis"
Abdoo Dev
  • 1,216
  • 11
  • 16
0

The country names are already translated and available in almost any format. Just Google it. Here is an example in CVS, XML and YAML formats.

If this file is required for some of your projects, I would simply have those country's in the original yaml file like so:

de:
   countries:
      country_1:  "Schweiz"
      country_2:  "Deutschland"
      country_3:  "Italien"
      country_4:  "Frankreich"
      country_6:  "Afghanistan"
fr:
   countries:
      country_1:  "Schweiz in French"
      country_2:  "Deutschland in French"
Uzbekjon
  • 11,655
  • 3
  • 37
  • 54