简体   繁体   English

用rails翻译数据库字段

[英]translate database fields with rails

I know about the built-in I18n in Rails, but how can I select a database field per locale? 我知道Rails中内置的I18n,但如何选择每个语言环境的数据库字段?

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? 是否有插件或解决方案,可针对像我的结构的不同语言环境设置使用不同的字段?

Try using: 尝试使用:

http://github.com/joshmh/globalize2 http://github.com/joshmh/globalize2

It may require renaming your columns (to a different standard). 可能需要重命名列(以其他标准命名)。

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. 或者,只要您以某种方式使当前区域设置对模型可见,则可以类似地覆盖默认的title访问器方法。

Edit: You can take a look at http://github.com/iain/translatable_columns , it seems pretty much compatible with your architecture.... 编辑:您可以看一下http://github.com/iain/translatable_columns ,它似乎与您的体系结构非常兼容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM