Virtus::Localized
Virtus::Localized is an extension for virtus that adds the localized
feature. It's based on the localized
feature of MongoID and was requested via this issue
Installation
Add this line to your application's Gemfile:
gem 'virtus-localized'
And then execute:
$ bundle
Or install it yourself as:
$ gem install virtus-localized
Usage
You need to require it first:
require 'virtus/localized'
Then it gives you the following flag when definig the attribute
:
class Post
include Virtus.model
attribute :title, String, localized: true
attribute :body, String, localized: true
end
It uses the i18n to detect the locale
and creates an internal hash for the attribute:
post = Post.new(title: 'Hello', body: 'English')
p post.title
# => 'Hello'
p post.body
# => 'English'
I18n.locale = :es
post.title = 'Hola'
post.body = 'Español'
p post.title
# => 'Hola'
p post.body
# => 'Español'
p post.instance_variable_get('@title')
# { "en" => "Hello", "es" => "Hola" }
p post.instance_variable_get('@body')
# { "en" => "English", "es" => "Español" }
Contributing
- Fork it ( https://github.com/[my-github-username]/virtus-localized/fork )
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create a new Pull Request