How can I internationalize say a categories table (with a name column) into different languages. How about a products table (consisting of a name and description columns). Which is the best way to internationalize the content of these database tables using Ruby on Rails?
我如何国际化将类别表(带有名称列)说成不同的语言。产品表(由名称和描述列组成)如何?哪个是使用Ruby on Rails使这些数据库表的内容国际化的最佳方法?
5 个解决方案
#1
6
Have you taken a look at: http://guides.rubyonrails.org/i18n.html
您是否看过:http://guides.rubyonrails.org/i18n.html
It describes in some detail how to internationalise your application and
它详细描述了如何使您的应用程序国际化
"provides an easy-to-use and extensible framework for translating your application to a single custom language other than English or for providing multi-language support in your application."
“提供了一个易于使用且可扩展的框架,用于将您的应用程序翻译成除英语之外的单一自定义语言,或者在您的应用程序中提供多语言支持。”
Some useful links:
一些有用的链接:
- http://rails-i18n.org/
- http://rails-i18n.org/
- http://github.com/svenfuchs/rails-i18n
- http://github.com/svenfuchs/rails-i18n
- http://github.com/svenfuchs/rails-i18n/tree/master/rails/locale
- http://github.com/svenfuchs/rails-i18n/tree/master/rails/locale
Update: 2018
更新:2018年
Since answering this question nearly nine years ago, the same author of i18n has created Globalize which builds on the I18n API in Ruby on Rails to add model translations to ActiveRecord models.
自从近九年前回答这个问题以来,i18n的同一作者创建了Globalize,它基于Ruby on Rails中的I18n API构建,为ActiveRecord模型添加模型转换。
Please find details here: https://github.com/globalize/globalize
请在此处查找详细信息:https://github.com/globalize/globalize
#2
3
On RailsCasts there is a nice article about, using a gem called Globalize3. That just let you set which Models will be translated and manage a translate tables for each model, and works just like i18n is to static pages...
在RailsCasts上有一篇很好的文章,使用名为Globalize3的gem。这只是让你设置将翻译哪些模型并管理每个模型的翻译表,并且像i18n一样工作就是静态页面......
Take a look
看一看
http://railscasts.com/episodes/338-globalize3?view=asciicast
http://railscasts.com/episodes/338-globalize3?view=asciicast
#3
2
If you want to store the values for the different languages in the db next to the standard Rails i18n (yml), you could do something like this:
如果要在标准Rails i18n(yml)旁边的db中存储不同语言的值,可以执行以下操作:
Products table name field:
产品表名称字段:
- name_en
- name_en
- name_fr
- name_fr
- name_nl
- name_nl
Fetch the correct value:
获取正确的值:
def i18n_db_value(object, attribute)
object.send("#{attribute.to_s}_#{I18n.locale}") if object
end
#4
0
"store multiple versions of content in the model and have one site" vs. "store only one version of content in the model but have multiple sites"
“在模型中存储多个版本的内容,并且有一个网站”与“只存储模型中的一个版本的内容,但有多个网站”
http://ruby-lang.info/blog/localization-jfw
http://ruby-lang.info/blog/localization-jfw
#5
0
You can overwrite "name" method in model Category, there can search the correct translation in another table.
您可以覆盖模型类别中的“名称”方法,可以在另一个表中搜索正确的翻译。
So that, in categories table, you should have in the field "name" the default language translated, for example "Other". And then seek "Other" in a table like:
因此,在类别表中,您应该在字段“name”中翻译默认语言,例如“Other”。然后在表格中寻找“其他”,如:
transtations table
en_text "Other" <--- You search this (default language)
es_text "Otros" ---> You retrun this
ca_text "Altres" ---> or this
# Category table
class Category < ActiveRecord::Base
def name
Translation.translate(read_attribute("name"))
end
end
# Your transltation model
class Translation < ActiveRecord::Base
def self.translate(text)
locale=I18n.locale
if locale!="en" # default locale: what is on the table "category"
trad=self.find_by_en_text(text)
if trad
return eval("trad.#{locale}_text")
end
end
return text
end
end
#1
6
Have you taken a look at: http://guides.rubyonrails.org/i18n.html
您是否看过:http://guides.rubyonrails.org/i18n.html
It describes in some detail how to internationalise your application and
它详细描述了如何使您的应用程序国际化
"provides an easy-to-use and extensible framework for translating your application to a single custom language other than English or for providing multi-language support in your application."
“提供了一个易于使用且可扩展的框架,用于将您的应用程序翻译成除英语之外的单一自定义语言,或者在您的应用程序中提供多语言支持。”
Some useful links:
一些有用的链接:
- http://rails-i18n.org/
- http://rails-i18n.org/
- http://github.com/svenfuchs/rails-i18n
- http://github.com/svenfuchs/rails-i18n
- http://github.com/svenfuchs/rails-i18n/tree/master/rails/locale
- http://github.com/svenfuchs/rails-i18n/tree/master/rails/locale
Update: 2018
更新:2018年
Since answering this question nearly nine years ago, the same author of i18n has created Globalize which builds on the I18n API in Ruby on Rails to add model translations to ActiveRecord models.
自从近九年前回答这个问题以来,i18n的同一作者创建了Globalize,它基于Ruby on Rails中的I18n API构建,为ActiveRecord模型添加模型转换。
Please find details here: https://github.com/globalize/globalize
请在此处查找详细信息:https://github.com/globalize/globalize
#2
3
On RailsCasts there is a nice article about, using a gem called Globalize3. That just let you set which Models will be translated and manage a translate tables for each model, and works just like i18n is to static pages...
在RailsCasts上有一篇很好的文章,使用名为Globalize3的gem。这只是让你设置将翻译哪些模型并管理每个模型的翻译表,并且像i18n一样工作就是静态页面......
Take a look
看一看
http://railscasts.com/episodes/338-globalize3?view=asciicast
http://railscasts.com/episodes/338-globalize3?view=asciicast
#3
2
If you want to store the values for the different languages in the db next to the standard Rails i18n (yml), you could do something like this:
如果要在标准Rails i18n(yml)旁边的db中存储不同语言的值,可以执行以下操作:
Products table name field:
产品表名称字段:
- name_en
- name_en
- name_fr
- name_fr
- name_nl
- name_nl
Fetch the correct value:
获取正确的值:
def i18n_db_value(object, attribute)
object.send("#{attribute.to_s}_#{I18n.locale}") if object
end
#4
0
"store multiple versions of content in the model and have one site" vs. "store only one version of content in the model but have multiple sites"
“在模型中存储多个版本的内容,并且有一个网站”与“只存储模型中的一个版本的内容,但有多个网站”
http://ruby-lang.info/blog/localization-jfw
http://ruby-lang.info/blog/localization-jfw
#5
0
You can overwrite "name" method in model Category, there can search the correct translation in another table.
您可以覆盖模型类别中的“名称”方法,可以在另一个表中搜索正确的翻译。
So that, in categories table, you should have in the field "name" the default language translated, for example "Other". And then seek "Other" in a table like:
因此,在类别表中,您应该在字段“name”中翻译默认语言,例如“Other”。然后在表格中寻找“其他”,如:
transtations table
en_text "Other" <--- You search this (default language)
es_text "Otros" ---> You retrun this
ca_text "Altres" ---> or this
# Category table
class Category < ActiveRecord::Base
def name
Translation.translate(read_attribute("name"))
end
end
# Your transltation model
class Translation < ActiveRecord::Base
def self.translate(text)
locale=I18n.locale
if locale!="en" # default locale: what is on the table "category"
trad=self.find_by_en_text(text)
if trad
return eval("trad.#{locale}_text")
end
end
return text
end
end