Ser*_*ruk 6 ruby-on-rails internationalization globalize3 rails-i18n globalize
我有小桌子:
create_table :cities do |t|
t.string :name
end
Run Code Online (Sandbox Code Playgroud)
我需要国际化"名称"列,我不想为此创建单独的表.是否可以将翻译列添加到"城市"表中?结果我希望这个表的迁移看起来像这样:
create_table :cities do |t|
t.string :en_name
t.string :de_name
t.string :fr_name
end
Run Code Online (Sandbox Code Playgroud)
目前我正在尝试使用"globalize"gem,也许我应该使用其他解决方案,请指教.
小智 1
标准做法是将翻译表与 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
Run Code Online (Sandbox Code Playgroud)
这仅显示了访问器(名称函数)的代码,您可能还需要编写一个修改器(名称=函数),以便您可以根据当前区域设置设置值。I18n.locale 将为您提供当前的语言环境。
| 归档时间: |
|
| 查看次数: |
322 次 |
| 最近记录: |