Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
[in beta] Rails plugin to handle translating ActiveRecord data. Integrates nicely with tolk.
Ruby
branch: master

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
lib
spec
tasks
.gitignore
MIT-LICENSE
README.markdown
Rakefile
init.rb
install.rb
uninstall.rb

README.markdown

HasTranslation

Provides ability to declare ActiveRecord columns which are translatable. This enables dumping an I18n YAML file in the default language, ready for tolk to sync with.

Example

Step 1: Add declaration to Item model

# product.rb
class Product < ActiveRecord::Base
  has_translation :name
end

Step 2: Generate I18n YAML file

$ rake has_translation:dump # or just `rake ht:dump'

Presuming I18n.default_language is :en, this will generate config/locales/en/activerecord/base/product/name.yml.

Step 3: Sync with "tolk":http://github.com/dhh/tolk

$ rake tolk:sync

Access translated ActiveRecord data

Now, each translated record returns the version for the current I18n.locale. The untranslated version is also available by appending _before_translated to the column_name.

>> I18n.locale
=> :en
>> @product.name
=> "cheese"
>> I18n.locale = :fr
=> :fr
>> @product.name
=> "fromage"
>> @product.name_before_translated
=> "cheese"

Sensible fallbacks

Instead of returning "missing translation" messages, translation lookups fallbacks in this order:

  • options[:locale]
  • I18n.locale
  • I18n.language
  • I18n.default_language
  • I18n.default_locale

This is done by overriding the I18n module's `translate' method.

Tests

Code has rspec test coverage. See spec directory for more use cases.

Happy days!

Copyright (c) 2010 Zubin Henner, released under the MIT license

Something went wrong with that request. Please try again.