Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Translatable attributes for activerecord using the I18n backend provided by rails

branch: master

Fetching latest commit…

Octocat-spinner-32-eaf2f5

Cannot retrieve the latest commit at this time

Octocat-spinner-32 lib
Octocat-spinner-32 spec
Octocat-spinner-32 .gitignore
Octocat-spinner-32 .travis.yml
Octocat-spinner-32 Gemfile
Octocat-spinner-32 Gemfile.lock
Octocat-spinner-32 MIT-LICENSE
Octocat-spinner-32 README.rdoc
Octocat-spinner-32 Rakefile
Octocat-spinner-32 activerecord_translatable.gemspec
README.rdoc

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

Move the the database.yml into place and edit to your needs, and setup the test database

$ rake setup:database_config
$ rake setup:test_database

Now the test can be run via

$ rake spec

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.

Something went wrong with that request. Please try again.