Globalize3 is the successor of Globalize for Rails. Globalize is targeted at ActiveRecord 3. It is compatible with and builds on the new I18n api in Ruby on Rails and adds model translations to ActiveRecord.
Globalize3 is much more lightweight and compatible than its predecessor Globalize for Rails was. Model translations in Globalize3 use default ActiveRecord features and do not limit any ActiveRecord functionality any more.
ActiveRecord > 3.0.0
I18n
To install Globalize3 with its default setup just use:
$ gem 'globalize3', :git => 'git://github.com/galetahub/globalize3.git'
Model translations allow you to translate your models’ attribute values. E.g.
class Post < ActiveRecord::Base
translates :title, :text
end
Allows you to translate the attributes :title and :text per locale:
I18n.locale = :en
post.title # => Globalize3 rocks!
I18n.locale = :he
post.title # => גלובאלייז2 שולט!
post.title_en # => Globalize3 rocks!
post.title_he # => גלובאלייז2 שולט!
post.title_en = "Globalize3 Super"
post.title_he = "Globalize3 שולט"
Configuration available locales list (by default it returns I18n.available_locales):
Globalize.available_locales = [:en, :ru]
In order to make this work, you’ll need to add the appropriate translation tables. Globalize3 comes with a handy helper method to help you do this. It’s called create_translation_table!
. Here’s an example:
class CreatePosts < ActiveRecord::Migration
def self.up
create_table :posts do |t|
t.timestamps
end
Post.create_translation_table! :title => :string, :text => :text
end
def self.down
drop_table :posts
Post.drop_translation_table!
end
end
Note that the ActiveRecord model Post
must already exist and have a translates
directive listing the translated fields.
Globalize3 nicely integrates with vestal_versions:
require 'globalize/versioning/vestal_versions'
As of writing (2010-08-05) the original vestal_versions respository has not been updated to be compatible with Rails 3 though. You can use this fork though. Globalize3’s Gemfile is currently set up accordingly.
Please also note that update_attribute
currently hides itself from dirty tracking in ActiveRecord >= 3.0.0.beta (which is considered a regression). That means that you currently need to use attribute writers or update_attributes
in order to track changes/versions for your models.
Also, please see the tests in test/globalize3/versioning_test.rb for some current gotchas.
- `translation_table_name` was renamed to `translations_table_name`
- `available_locales` has been removed. please use `translated_locales`
See this script by Tomasz Stachewicz: http://gist.github.com/120867
- Veger’s fork – uses default AR schema for the default locale, delegates to the translations table for other locales only
- TranslatableColumns – have multiple languages of the same attribute in a model (Iain Hecker)
- localized_record – allows records to have localized attributes without any modifications to the database (Glenn Powell)
- model_translations – Minimal implementation of Globalize2 style model translations (Jan Andersson)
- globalize2_versioning – acts_as_versioned style versioning for globalize2 (Joshua Harvey)
- i18n_multi_locales_validations – multi-locales attributes validations to validates attributes from globalize2 translations models (Sébastien Grosjean)
- globalize2 Demo App – demo application for globalize2 (Sven Fuchs)
- migrate_from_globalize1 – migrate model translations from Globalize1 to globalize2 (Tomasz Stachewicz)
- easy_globalize2_accessors – easily access (read and write) globalize2-translated fields (astropanic, Tomasz Stachewicz)
- globalize2-easy-translate – adds methods to easily access or set translated attributes to your model (bsamman)
- batch_translations – allow saving multiple globalize2 translations in the same request (Jose Alvarez Rilla)