Last active
December 12, 2019 06:41
-
-
Save bithavoc/5883954 to your computer and use it in GitHub Desktop.
Using multilang-hstore in Rails 4
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # add the gem to your Gemfile | |
| gem 'multilang-hstore', '~> 1.0.0' | |
| # execute bundle install |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # enable hstore extension in the development database of our project | |
| $ psql -d demomultilang_development -c "CREATE EXTENSION IF NOT EXISTS hstore" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # new model with hstore attribute, we will enable translation for the hstore field. | |
| $ rails g model Post title:hstore | |
| $ rake db:migrate |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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