Giter Site home page Giter Site logo

globalize's Introduction

Globalize

Build Status Code Climate

You can chat with us using Gitter:

Gitter chat

Globalize builds on the I18n API in Ruby on Rails to add model translations to ActiveRecord models.

Requirements

  • ActiveRecord >= 4.0.0 (see below for installation with ActiveRecord 3.x)
  • I18n

Installation

To install the ActiveRecord 4.x compatible version of Globalize with its default setup, just use:

gem install globalize

When using bundler put this in your Gemfile:

gem 'globalize', '~> 4.0.2'

To use the version of globalize for ActiveRecord 3.1 or 3.2, specify:

gem 'globalize', '~> 3.1.0'

(If you are using ActiveRecord 3.0, use version 3.0: gem 'globalize', '3.0.4'.)

The 3-1-stable branch of this repository corresponds to the latest ActiveRecord 3 version of globalize. Note that globalize3 has been deprecated and you are encouraged to update your Gemfile accordingly.

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 # => Globalize rocks!

I18n.locale = :he
post.title # => גלובאלייז2 שולט!

In order to make this work, you'll need to add the appropriate translation tables. Globalize comes with a handy helper method to help you do this. It's called create_translation_table!. Here's an example:

Note that your migrations can use create_translation_table! and drop_translation_table! only inside the up and down instance methods, respectively. You cannot use create_translation_table! and drop_translation_table! inside the change instance method.

Creating translation tables

Do not use the change method, use up and down!

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

Also, you can pass options for specific columns. Here’s an example:

class CreatePosts < ActiveRecord::Migration
  def up
    create_table :posts do |t|
      t.timestamps
    end
    Post.create_translation_table! :title => :string,
      :text => {:type => :text, :null => false, :default => 'abc'}
  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

NOTE: Make sure you drop the translated columns from the parent table after all your data is safely migrated.

Adding additional fields to the translation table

In order to add a new field to an existing translation table, you can use add_translation_fields!:

class AddAuthorToPost < ActiveRecord::Migration
  def up
    Post.add_translation_fields! author: :text
  end

  def down
    remove_column :post_translations, :author
  end
end

NOTE: Remember to add the new field to the model:

translates :title, :author

Versioning with Globalize

See the globalize-versioning gem.

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, globalize 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: "Globalize rocks!", name: "Globalize">,
      #<Post::Translation id: 2, post_id: 1, locale: "nl", title: '', name: nil>]

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

I18n.locale = :nl
post.title # => ''
post.name  # => 'Globalize'
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: "Globalize rocks!", name: "Globalize">,
      #<Post::Translation id: 2, post_id: 1, locale: "nl", title: '', name: nil>]

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

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

Fallback locales to each other

It is possible to setup locales to fallback to each other.

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

Globalize.fallbacks = {:en => [:en, :pl], :pl => [:pl, :en]}

I18n.locale = :en
en_post = Post.create(:title => 'en_title')

I18n.locale = :pl
pl_post = Post.create(:title => 'pl_title')
en_post.title # => 'en_title'

I18n.locale = :en
en_post.title # => 'en_title'
pl_post.title # => 'pl_title'

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: "Globalize rocks!", name: "Globalize">,
  #<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: "Globalize rocks!", name: "Globalize">,
  #<Post::Translation id: 2, post_id: 1, locale: "nl", title: '', name: nil>
]

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

Show different languages

In views, if there is content from different locales that you wish to display, you should use the with_locale option with a block, as below:

<% Globalize.with_locale(:en) do %>
  <%= render "my_translated_partial" %>
<% end %>

Your partial will now be rendered with the :en locale set as the current locale.

Interpolation

Globalize supports interpolation in a similar manner to I18n.

class Post < ActiveRecord::Base
  translates :title
end

I18n.locale = :en
post.title = "Globalize %{superlative}!"

post.title
# #=> "Globalize %{superlative}!"

post.title(:foo => "bar")
# SomeError: missing interpolation argument :superlative

post.title(:superlative => "rocks")
# #=> "Globalize rocks!"

Fragment caching

Don't forget to add globalize locale into the cache_key to separate different localizations of the record. One of the possible ways to implement it:

# inside translated model
def cache_key
  super + '-' + Globalize.locale.to_s
end

Official Globalize extensions

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)
  • Traco - A newer take on using multiple columns in the same model (Barsoom)
  • 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)
  • hstore_translate - Rails I18n library for ActiveRecord model/data translation using PostgreSQL's hstore datatype (Rob Worley)

Related solutions

globalize's People

Contributors

a-chernykh avatar arturmalecki avatar asnow avatar benhutton avatar brainlock avatar brandonmathis avatar danbeaulieu avatar dandehavilland avatar fluxusfrequency avatar henare avatar hukl avatar inukshuk avatar j15e avatar joshmh avatar matchu avatar mremolt avatar mseppae avatar parndt avatar phiggins avatar phuesler avatar pwim avatar robotblake avatar shioyama avatar shir avatar simi avatar tjwallace avatar tomash avatar webervin avatar ybart avatar zencocoon avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.