可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I know about the built-in I18n in Rails, but how can I select a database field per locale?
My model structure is something like this:
title #default (englisch) title_de #(german) title_it #(italian)
In my template I want to be able to write only
<%= @model.title %>
and should get the value in the right language.
Is there a plugin or a solution to use different fields per different locale settings with a structure like mine?
回答1:
Although your db architecture (different locales hardcoded as table columns) seems wrong to me, I think you can achieve what you want by adding a pseudo-field to your model, something along:
# example not tested class MyModel < ActiveRecord::Base def localized_title(locale) locale = locale == 'en' ? '' : '_' + locale read_attribute("title#{locale}".to_sym") end end
Or, provided that you somehow make your current locale visible to your models, you can similarly overwrite the default title
accessor method.
Edit: You can take a look at http://github.com/iain/translatable_columns, it seems pretty much compatible with your architecture....
回答2:
Try using:
http://github.com/joshmh/globalize2
It may require renaming your columns (to a different standard).