How to internationalize content on ruby on rails?
Asked Answered
O

6

5

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?

Ophidian answered 2/10, 2009 at 6:44 Comment(0)
V
7

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

Vociferation answered 2/10, 2009 at 6:51 Comment(2)
All of these references don't talk about translate database contents... like the gem Globalize3Pile
The original question was asked nearly 9 years ago. It was answered correctly for the time. I will reference globalize and Globalize3 has moved and incidentally written by the same author as i18nVociferation
P
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...

Take a look

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

Patronymic answered 17/9, 2013 at 4:1 Comment(0)
A
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:

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
Apul answered 2/10, 2009 at 10:35 Comment(0)
I
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

Inceptive answered 21/5, 2011 at 4:2 Comment(0)
E
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:

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
Equivocate answered 15/5, 2015 at 13:5 Comment(0)
F
0

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.

Fredela answered 9/4 at 14:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.