Search code examples
rubytranslationrails-i18n

Translate German country names to French


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?


Solution

  • 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"