How to internationalize ruby ​​content on rails?

How can I internationalize using a category table (with a column of names) in different languages. How about a product table (consisting of name and description columns). What is the best way to internationalize the contents of these database tables using Ruby on Rails?

+5
source share
5 answers

RailsCasts , Globalize3. , , , i18n - ...

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

+3

db Rails i18n (yml), - :

:

  • name_fr
  • name_nl

:

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

"name" , .

, "" , "". "" , :

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
0

All Articles