选项1
假设有以下yaml文件结构
categories:
gastronomy: Gastronomy
family: Family
sports: Sports
scene: Scene
traditional: Tradition
music: Music
party: Party
现在您可以执行以下操作:
@event.categories.map{|n| I18n.t("categories.#{n}"}.to_sentence
选项 2
更好的是,您可以更改Category
模型以返回本地化名称:
class Category < ActiveRecord::Base
def name
key = read_attribute(:name)
return key if key.blank? # return immediately if nil
# use the key as value if the localization value is missing
I18.n("categories.#{key}", :default => key.humanize)
end
end
现在,该name
方法返回一个本地化值:
cat.name # localized name
您的原始陈述也将起作用
@event.categories.map(&:name).to_sentence
选项 3
使用Globalize3 gem。观看此截屏视频了解更多详情。