acts_as_translatable is a Ruby on Rails plugin for easy translation of models and database tables.
In your Gemfile:
gem 'acts_as_translatable'
And run bundle install
.
Generate translations for a category model with name and description fields, with default locale set to ‘en’ (English):
$ rails generate acts_as_translatable en category name description
This will create the necessary migration. Then run:
$ rake db:migrate
Then add to app/models/category.rb
:
class Category < ActiveRecord::Base acts_as_translatable_on :name, :description ... end
And play around with I18n:
I18n.locale = "en" Category.first.update_attribute :name, "English name" I18n.locale = "de" Category.first.update_attribute :name, "Deutsche Name" puts Category.first.name # => "English name" puts Category.first.name_en # => "Deutsche Name" puts Category.first.name_es # => nil Category.find_by_name("Programming") Category.find_by_name_de("Programmierung")
And it supports I18n fallbacks.
Use it in conjunction with the translate_acts_as_translatable_models (lassebunk/translate_acts_as_translatable_models) to automatically translate your acts_as_translatable models.
Have fun! Please send comments and suggestions to [email protected], and issues here at GitHub, please :)
-
Add finder methods such as
find_by_name
andfind_by_name_en
. -
Cache translations on application level using
Rails.cache
so they won’t be loaded for each translated record?
Copyright © 2012 Lasse Bunk, released under the MIT license