Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
ActiveRecord plugin to create translations for your models.
Ruby
Tag: 0.2

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
generators/has_translations
lib
tasks
test
MIT-LICENSE
README.md
Rakefile
init.rb
install.rb
uninstall.rb

README.md

HasTranslations v0.2

This simple plugin creates translations for your model.

Uses delegation pattern: http://en.wikipedia.org/wiki/Delegation_pattern

Example

For example you have Article model and you want to have title and text to be translated.

Create model named ArticleTranslation (Rule: [CamelCaseModelName]Translation)

Migration should have locale as a string with two letters and belongs_to associative id, like:

class CreateArticleTranslations < ActiveRecord::Migration
  def self.up
    create_table :article_translations do |t|
      t.integer :article_id, :null => false
      t.string :locale, :null => false, :limit => 2
      t.string :title, :null => false
      t.text :text, :null => false
    end

    add_index :article_translations, [:article_id, :locale], :unique => true
  end

  def self.down
    drop_table :article_translations
  end
end

Add to article model translations :value1, :value2:

class Article < ActiveRecord::Base
  translations :title, :text
end

And that's it. Now you can add your translations using:

article = Article.create

article.translations.create(:locale => 'en', :title => 'title', :text => 'text') # or ArticleTranslation.create(:article => article, :locale => 'en', :title => 'title', :text => 'text')
article.translations.create(:locale => 'ru', :title => 'заголовок', :text => 'текст')
article.reload # reload cached translations association array
I18n.locale = :en
article.text # text
I18n.locale = :ru
article.title # заголовок

You can use text filtering plugins, like acts_as_sanitiled and validations, and anything else that is available to the ActiveRecord:

class ArticleTranslation < ActiveRecord::Base
  acts_as_sanitiled :title, :text

  validates_presence_of :title, :text
  validates_length_of :title, :maximum => 100
end

Options:

:fallback => true [default: false] - fallback 1) default scope; 2) first from translations; 3) empty string

PS

Plugin also have small monkeypatch for the I18n gem. Using it you can define your own available_locales through:

I18n.available_locales = :en, :ru, :de

And get those values through:

I18n.available_locales

This is done because of some plugins have own (for example ActiveScaffold has "ru, es, hu" in-box) locales, and this can be a problem for the all_translation method to build an array. To override this, you can place I18n.available_locales= to your environment.rb file, e.g.:

I18n.available_locales = :en, :ru, :ee

TODO

  • active record 3 (rails 3) support
  • add installation description to readme
  • model and migration generators
  • caching
  • write more examples: fallback feature
  • write blog post about comparison and benefits of this plugin between another translation model plugins

Alternatives

I know three of them:

  • puret - special for Rails 3 and almost the same as this project.
  • globalite2 - a lot of magic.
  • model_translations - almost the same as this project, but more with more code in lib.
  • translatable_columns - different approach: every column have own postfix "_#{locale}" in the same table (sometimes it could be fine).

Used in

Copyright (c) 2009-2010 [Dmitry Polushkin], released under the MIT license

Something went wrong with that request. Please try again.