You are here: Browse Railsplugins Localize Models
This plugin servers as an alternative means of localizing ActiveRecord models to the standard globalize method of piggy-backing localized attributes found in a separate table onto every sql query (via COALESCE).
This method depends on adding the localized attributes to the models own table.
e.g. If you have a model called Article and want to localize the title and body attributes to spanish (es), then apart from the ‘title’ & ‘body’ attributes in the articles table, you now also require, ‘title_es’, ‘body_es’. And so on for each language you want to support.
This avoids any extra joins (and thus the standard globalize limitations to ActiveRecord::Base#find) and also means that you have all the localized versions in one query. Changing locale doesn’t necesitate a reload of the model object in order to access the localized data for the new locale.
Example:
class Article < ActiveRecord::Base localizes :title, :body end
(Assuming english is the base locale, and we want to support spanish)
You need a table like this:
create_table :articles do |t|
t.column :id, :integer
t.column :title, :string
t.column :title_es, :string
t.column :body, :string
t.column :body_es, :string
...
end
This plugin allows you to do the following:
Locale.set_base_language(‘en-US’) Locale.set(‘en-US’)
article = Article.create(:title => ‘news’, :body => ‘some news’)
puts article.title #Accesses title column (english) > ‘news’
Locale.set(‘es-ES’) article.title = ‘noticias’ article.save puts article.title #Accesses title_es column (spanish), > ‘noticias’
puts article._title #Accesses original ‘title’ column > ‘news’
Locale.set(‘en-US’) puts article.title #Accesses title column (english) > ‘news’
You can create any ‘find’ query you want without limitation.
A further feature:
Locale.set(‘es-ES’) article = Article.find_by_title(‘noticias’) puts article.title > ‘noticias’
Locale.set(‘en-US’) article = Article.find_by_title(‘news’) puts article.title > ‘news’
Locale.set(‘es-ES’) puts article.title > ‘noticias’
Use migrations to add support for more locales along the line.
NOTE: This description has been extracted from the Plugin README and so the formatting may need updating to make browser friendly