Skip to content

webervin/globalize3

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Globalize3

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.

Requirements

ActiveRecord > 3.0.0.rc
I18n

Installation

To install Globalize3 with its default setup just use:


$ gem install globalize3

Model translations

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 שולט!

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:

Note that if you’re using rails ~> 3.1.0.rc migrations has only the change instance method

Rails 3.0


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

Rails ~> 3.1.0.rc

Do not use the change method


class CreatePosts < ActiveRecord::Migration
  def up
    create_table :posts do |t|
      t.timestamps
    end
    Post.create_translation_table! :title => :string, :text => :text
  end
  def 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.

Migrating existing data to and from the translated version

As well as creating a translation table, you can also use create_translation_table! to migrate across any
existing data to the default locale. This can also operate in reverse to restore any translations from the default locale
back to the model when you don’t want to use a translation table anymore using drop_translation_table!

This feature makes use of untranslated_attributes which allows access to the model’s attributes as they were before
the translation was applied. Here’s an example (which assumes you already have a model called Post and its table exists):


  class TranslatePosts < ActiveRecord::Migration
    def self.up
      Post.create_translation_table!({
        :title => :string,
        :text => :text
      }, {
        :migrate_data => true
      })
    end
    def self.down
      Post.drop_translation_table! :migrate_data => true
    end
  end

Versioning with Globalize3

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.

I18n fallbacks for empty translations

It is possible to enable fallbacks for empty translations. It will depend on the configuration setting you have set for I18n translations in your Rails config.

You can enable them by adding the next line to config/application.rb (or only config/environments/production.rb if you only want them in production)

config.i18n.fallbacks = true

By default, globalize3 will only use fallbacks when your translation model does not exist or the translation value for the item you’ve requested is nil. However it is possible to also use fallbacks for blank translations by adding :fallbacks_for_empty_translations => true to the translates method.


  class Post < ActiveRecord::Base
    translates :title, :name
  end

  puts post.translations.inspect
  # => [#<Post::Translation id: 1, post_id: 1, locale: "en", title: "Globalize3 rocks!", name: "Globalize3">, #<Post::Translation id: 2, post_id: 1, locale: "nl", title: '', name: nil>]

  I18n.locale = :en
  post.title # => 'Globalize3 rocks!'
  post.name  # => 'Globalize3'

  I18n.locale = :nl
  post.title # => ''
  post.name  # => 'Globalize3'

  class Post < ActiveRecord::Base
    translates :title, :name, :fallbacks_for_empty_translations => true
  end

  puts post.translations.inspect
  # => [#<Post::Translation id: 1, post_id: 1, locale: "en", title: "Globalize3 rocks!", name: "Globalize3">, #<Post::Translation id: 2, post_id: 1, locale: "nl", title: '', name: nil>]

  I18n.locale = :en
  post.title # => 'Globalize3 rocks!'
  post.name  # => 'Globalize3'

  I18n.locale = :nl
  post.title # => 'Globalize3 rocks!'
  post.name  # => 'Globalize3'

Scoping objects by those with translations

To only return objects that have a translation for the given locale we can use the `with_translations` scope. This will only return records that have a translations for the passed in locale.



Post.with_translations('en') # => [#<Post::Translation id: 1, post_id: 1, locale: "en", title: "Globalize3 rocks!", name: "Globalize3">, #<Post::Translation id: 2, post_id: 1, locale: "nl", title: '', name: nil>]

Post.with_translations(I18n.locale) # => [#<Post::Translation id: 1, post_id: 1, locale: "en", title: "Globalize3 rocks!", name: "Globalize3">, #<Post::Translation id: 2, post_id: 1, locale: "nl", title: '', name: nil>]

Post.with_translations('de') # => []

Changes since Globalize2

  • `translation_table_name` was renamed to `translations_table_name`
  • `available_locales` has been removed. please use `translated_locales`

Migration from Globalize for Rails (version 1)

See this script by Tomasz Stachewicz: http://gist.github.com/120867

Alternative Solutions

  • 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)

Related solutions

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Ruby 100.0%