Skip to content

Instantly share code, notes, and snippets.

@bithavoc
Last active December 12, 2019 06:41
Show Gist options
  • Select an option

  • Save bithavoc/5883954 to your computer and use it in GitHub Desktop.

Select an option

Save bithavoc/5883954 to your computer and use it in GitHub Desktop.
Using multilang-hstore in Rails 4
# add the gem to your Gemfile
gem 'multilang-hstore', '~> 1.0.0'
# execute bundle install
# enable hstore extension in the development database of our project
$ psql -d demomultilang_development -c "CREATE EXTENSION IF NOT EXISTS hstore"
# new model with hstore attribute, we will enable translation for the hstore field.
$ rails g model Post title:hstore
$ rake db:migrate
I18n.available_locales #=> [:en. :es]
Post.create(title_en: "In English", title_es: "En Español")
#<Post id: 1, title: {"en"=>"In English", "es"=>"En Español"}, created_at: "...", updated_at: "...">
I18n.locale = :en
# => :en
Post.last.title
# => "In English"
I18n.locale = :es
Post.last.title
# => "En Español"
# have fun! => https://github.com/heapsource/multilang-hstore
class Post < ActiveRecord::Base
multilang :title # enables multilang on top of our new hstore field
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment