1

对不起,如果这真的很容易。但是我尝试了很多方法来添加 I18n 并且似乎不起作用。

这是视图

 =  @event.categories.map(&:name).to_sentence

这是在语言环境中

 #Categories
   categories:
   gastronomy: Gastronomy
   family: Family
   sports: Sports
   scene: Scene
   traditional: Tradition
   music: Music
   party: Party

我设法让翻译工作在表格中,但不是在这里。知道为什么吗?

4

1 回答 1

1

选项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。观看此截屏视频了解更多详情。

于 2012-04-18T19:23:05.443 回答