<img src=“https://travis-ci.org/sideshowcoder/activerecord_translatable.png” />
ActiveRecordTranslatable
Make attributes of an ActiveRecord Model translatable, and store the translations in the provided I18n backend. This is really helpful if there already is, a interface to provide ie missing translations for elements in I18n.
Usage
Use inside of the model
class MyModel < ActiveRecord::Base
translate :title
end
I18n.locale = :en
mymodel = MyModel.create(title: "My title", title_de: "Mein Title)
mymodel.title_wk = "Woohhaakkk"
mymodel.title # => "My title"
mymodel.title_en # => "My title"
mymodel.title_wk # => "Woohhaakkk"
Prerequisites
To save the locales ActiveRecordTranslatable saves an array of the specified locale, for this to work, the database needs the ability to save arrays. The easiest way to handle this in postgres is via the postgres array
gem ‘activerecord-postgres-array’
To add the array to the model the migration looks like this
class CreateMyModel < ActiveRecord::Migration
def change
create_table :my_model do |t|
t.string_array :locales
end
end
end
If your database does not support native array types you can use ActiveRecords ability to serialize the locales array
class MyModel < ActiveRecord::Base
serialize :locales
translate :name
end
And add the column as a string
class CreateMyModel < ActiveRecord::Migration
def change
create_table :my_model do |t|
t.string :locales
end
end
end
How it works
Translateable saves the translation via I18n.backend.store_translations, this means that the backend has to be able to store new items. So backend needs to be for example the KeyValue or ActiveRecord one. More railscasts.com/episodes/256-i18n-backends
Miscellaneous
Tests
The tests are using rspec, and there is a dummy app setup in the spec directory. Since the example is using postgres, to get the tests working there needs to be a setup postgres user with the needed rights.
$ createuser -h localhost
Enter name of role to add: translatable
Shall the new role be a superuser? (y/n) n
Shall the new role be allowed to create databases? (y/n) y
Shall the new role be allowed to create more new roles? (y/n) n
Afterwards the db needs to be setup
$ cd spec/dummy
$ rake db:create
$ rake db:migrate
$ rake db:test:prepare
Now the test can be run via
$ rspec
Current State
The gem is still in early development and has just been extracted from a project. So any bug reports and contributions are highly welcome.