Skip to content

Store values for multiple languages in an ActiveRecord attribute using PostgreSQL's hstore.

License

Notifications You must be signed in to change notification settings

ComboStrikeHQ/ar_translate

Repository files navigation

ArTranslate

Circle CI Code Climate Test Coverage

Store values for multiple languages in an ActiveRecord attribute using PostgreSQL's hstore.

Installation

Add the gem to your application's Gemfile:

gem 'ar_translate'

Usage

Create a hstore column of the pluralized attribute name:

add_column :posts, :descriptions, :hstore

Add the following to your model:

class Post < ActiveRecord::Base
  extend ArTranslate

  translates :descriptions, languages: %w(de en es)
end

Now you have access to the following methods:

post = Post.new

post.description_de = 'Hallo wie geht es dir?'
post.description_en = 'Whats up?'

post.descriptions
# => { 'de' => 'Hallo wie geht es dir?', 'en' => 'Whats up?' }

post.description_de
# => 'Hallo wie geht es dir?'

post.description_languages
# => ['de', 'en', 'es']

post.description_attributes
# => [:description_de, :description_en, :description_es]

# The two methods above also work at class level:
Post.description_languages # => [...]
Post.description_attributes # => [...]

This makes it really easy to use forms with translated attributes:

= form_for @post do |f|
  = f.text_field :name
  - @post.description_attributes.each do |attr|
    = f.text_field attr

Or you can add validations to your model:

class Post < ActiveRecord::Base
  extend ArTranslate

  translates :descriptions, languages: %w(de en es)

  description_attributes.each do |attr|
    validates attr, length: { in: 20..200 }
  end
end

License

MIT, see LICENSE.txt

Contributing

Feel free to fork and submit pull requests!

You need to set the DATABASE_URL environment variable to a valid PostgreSQL database for testing.

$ createdb ar_translate_test
$ export DATABASE_URL="postgres://localhost/ar_translate_test"
$ bundle exec rspec

About

Store values for multiple languages in an ActiveRecord attribute using PostgreSQL's hstore.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages