user177468
user177468

Reputation: 255

How to internationalize content on ruby on rails?

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?

Upvotes: 5

Views: 4129

Answers (6)

SJU
SJU

Reputation: 3272

I would use the built-in feature of Rails to handle I18n, meaning that I would create a category DB with a key column instead of a name column.

So you would have :

id | key
--------
1  | cat_1
2  | cat_2
3  | cat_3

Then in your locale files (en.yml, pt.yml, etc.) create a line for each categories :

categories:
  name:
    cat_1: "Category #1"
    cat_2: "Category #2"
    cat_3: "Category #3"

Then in your Category model create a method to get the name of the category :

class Category < ApplicationRecord
  def name
    I18n.t("categories.name.#{self.key}")
  end
end

This way you avoid create a new column every time you want to add support for a new language.

Upvotes: 0

Spasm
Spasm

Reputation: 805

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:

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

Upvotes: 7

Albert Catal&#224;
Albert Catal&#224;

Reputation: 2044

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

Upvotes: 0

cefigueiredo
cefigueiredo

Reputation: 738

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

Upvotes: 3

Alex Craft
Alex Craft

Reputation: 15336

"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

Upvotes: 0

atog
atog

Reputation: 49

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

Upvotes: 2

Related Questions