How to internationalize content on ruby on rails?

眉间皱痕 提交于 2021-02-18 07:03:12

问题


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?


回答1:


Have you taken a look at: 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://github.com/svenfuchs/rails-i18n
  • http://github.com/svenfuchs/rails-i18n/tree/master/rails/locale

Update: 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.

Please find details here: https://github.com/globalize/globalize




回答2:


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...

Take a look

http://railscasts.com/episodes/338-globalize3?view=asciicast




回答3:


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:

Products table name field:

  • name_en
  • name_fr
  • name_nl

Fetch the correct value:

def i18n_db_value(object, attribute)
  object.send("#{attribute.to_s}_#{I18n.locale}") if object
end



回答4:


"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




回答5:


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:

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


来源:https://stackoverflow.com/questions/1508033/how-to-internationalize-content-on-ruby-on-rails

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!