【问题标题】:Translating word arrays in Ruby on Rails I18n在 Ruby on Rails I18n 中翻译单词数组
【发布时间】:2015-01-15 21:48:27
【问题描述】:

我已经搜索和摆弄,但我没有运气。我正在尝试找出翻译单词数组(模型文件)的最佳方法:

  def self.valid_batman_characters
    %w{batman joker}
  end

我认为像 I18n.t ['activerecord.lookup', 'activerecord.lookup'] 这样的东西会起作用,但没有骰子。我不完全确定我错过了什么?

【问题讨论】:

  • ['lookup', 'lookup'].map {|lookup| I18.t lookup}?

标签: ruby-on-rails ruby activerecord internationalization


【解决方案1】:

这应该可行:

%w{batman joker}.map{ |key| I18n.t(key, scope: "active_record") }

或者,您可以使用符号:

%i{batman joker}.map{ |key| I18n.t(key, scope: :active_record) }

或字符串,不指定 I18n 的范围,如下所示:

%w{batman joker}.map{ |key| I18n.t("active_record.#{key}") }

或者符号字符串,仅仅因为你可以:

%w{batman joker}.map{ |key| I18n.t(key, scope: :active_record) }

这一切都假设您在语言配置中按以下方式设置了键:

active_record:
  batman: Translation1
  joker: Translation2

【讨论】:

  • 非常感谢!我查看了文档,但没有任何意义。
猜你喜欢
  • 1970-01-01
  • 2013-07-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多