简体   繁体   中英

Translating word arrays in Ruby on Rails I18n

I've searched and fiddled, but I'm having no luck. I'm trying to figure out the best way to translate a word array (model file):

  def self.valid_batman_characters
    %w{batman joker}
  end

I thought something like I18n.t ['activerecord.lookup', 'activerecord.lookup'] would work, but no dice. I'm not entirely sure what I'm missing?

This should work:

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

Alternatively, you could use symbols:

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

Or strings, without specifying scope to I18n, like so:

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

Or symbols and strings, just because you can:

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

That is all assuming that you have the keys in your language config set up the following way:

active_record:
  batman: Translation1
  joker: Translation2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM