【问题标题】:Translate German country names to French将德国国家名称翻译成法语
【发布时间】:2016-04-13 12:00:35
【问题描述】:

我得到了一个包含以下结构的 YAML 文件,其中包含德语国家/地区名称:

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

我可以将文件导入 Ruby 并将其保存为 Hash

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

我想把它们翻译成法语。我认为使用I18nCountryTranslations 这应该是一个相当简单的任务,但我被困住了。有没有办法使用内置的 Ruby 工具来做到这一点?或者我需要整个 Rails 框架吗?还是外部 API?

【问题讨论】:

  • “卡住”是什么意思?
  • 另一个词——“我不知道下一步该做什么”;)

标签: ruby translation rails-i18n


【解决方案1】:

您可以使用countries gem 获取翻译

只需安装 gem 并将 require 'countries' 添加到您的 ruby​​ 文件中。可以关注文档here

c = ISO3166::Country.find_country_by_name('united states')
c.translations['fr'] #=> "États-Unis"

【讨论】:

  • 问题是我没有翻译...我想翻译它们,这样它就不是“瑞士”而是“瑞士”
  • 我认为countries gem 可以帮助你
  • 查看我回答中的第二个链接。你可以找到所有的翻译。
【解决方案2】:

国家/地区名称已被翻译,几乎可以以任何格式使用。只需Google it。这是 CVS、XML 和 YAML 格式的example

如果您的某些项目需要此文件,我只需在原始 yaml 文件中包含这些国家/地区,如下所示:

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"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-08-22
    • 2016-10-29
    • 1970-01-01
    • 2018-02-10
    • 2012-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多