Ruby on rails 同一表中列的翻译

Ruby on rails 同一表中列的翻译,ruby-on-rails,internationalization,rails-i18n,globalize3,globalize,Ruby On Rails,Internationalization,Rails I18n,Globalize3,Globalize,我有一张小桌子: create_table :cities do |t| t.string :name end 我需要国际化“name”列,我不想为此创建单独的表。是否可以将翻译列添加到“cities”表中?因此,我希望此表的迁移如下所示: create_table :cities do |t| t.string :en_name t.string :de_name t.string :fr_name end 目前我正在尝试使用“globalize”gem,也许我应该使用其他解决方

我有一张小桌子:

create_table :cities do |t|
  t.string :name
end
我需要国际化“name”列,我不想为此创建单独的表。是否可以将翻译列添加到“cities”表中?因此,我希望此表的迁移如下所示:

create_table :cities do |t|
 t.string :en_name
 t.string :de_name
 t.string :fr_name
end

目前我正在尝试使用“globalize”gem,也许我应该使用其他解决方案,请给出建议。

标准做法是将转换表与globalize gem一起使用。如果不想使用globalize gem,可以执行以下操作:

class City < ActiveRecord::Base
   AVAILABLE_LOCALES = [:en, :de, :fr]
   def name
     current_locale = I18n.locale
     if AVALIABLE_LOCALES.include? current_locale
        self.send("#{current_locale.to_s}_name")  
     else
        #default language to use
        self.en_name
     end
   end
end
class City
这只是显示了访问器(name函数)的代码,您可能还需要编写一个mutator(name=function),以便根据当前语言环境设置值。I18n.locale将为您提供当前的语言环境。

您可以尝试引用此语言环境