No commit activity in last 3 years
No release in over 3 years
Simple ActiveRecord translations for Rails 3
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 2.0.0
~> 2.11.3
~> 1.3.1

Runtime

~> 3.0.0
 Project Readme

SimpleModelTranslations¶ ↑

Yet another implementation of ActiveRecord translations. Created for zn.ua, because globalize3 was too uncomfortable to use. It borrows some things from globalize3 and puret.

Basic Usage¶ ↑

For example, you are dealing with a website for some magazine, and you want your articles to be translated. So, you’ll need the following models to achieve this behaviour (warning: ArticleTranslation should be defined before Article, or Rails should be able to autoload it):

class ArticleTranslation < ActiveRecord::Base
  translation_for :article
end

class Article < ActiveRecord::Base
  translates :name, :content
end

or, if you are not going to add some additional behavior to translation class (for example, validations), you can use translation class, created for you by default:

class Article < ActiveRecord::Base
  translates :name, :content
end

ArticleTranslation will be generated automagically :)

Also, you’ll need a migration:

create_table(:article_translations) do |t|
  t.references :article
  t.string :locale

  t.string :name
  t.text :content
end
add_index :article_translations, [:article_id, :locale], :unique => true

Now you are able to translate values for the attributes :title and :description per locale:

I18n.locale = :en
article = Article.new(:name => 'Translations are so simple!')
I18n.locale = :uk
article.name = 'Hello, from Ukraine!'

I18n.locale = :en
article.name #=> 'Translations are so simple!'
I18n.locale = :uk
article.name #=> 'Hello, from Ukraine!'

Additional features can be discovered by searching the code and specs. Documentation is not available yet. I hope, it’ll be done when releasing zn.ua. :)

Nested attributes¶ ↑

Usually, when creating bootstrap data (e.g. seeds.rb) you want to pass all translations in one statement. You can do it using Rails’ nested attributes, and we propose a very convenient option for that:

class Category < ActiveRecord::Base
  translates :name, :attributes => true
end

Usage:

Category.create!(:translations_attributes => [
    { :locale => :en, :name => 'Science' },
    { :locale => :ru, :name => 'Наука' }
  ])

Fetching records with selected translations¶ ↑

If you want (and you probably do want) to fetch only records with specified translation, you can do it now:

Article.with_translation(:ru)

This code returns an ActiveRecord::Relation object, so you can chain your query furthermore.

Note on Patches/Pull Requests¶ ↑

  • Fork the project.

  • Make your feature addition or bug fix.

  • Add tests for it. This is important so I don’t break it in a future version unintentionally.

  • Commit, do not mess with rakefile, version, or history. (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)

  • Send me a pull request. Bonus points for topic branches.

Copyright © 2010 Pavel Forkert. See LICENSE for details.