Virtus::Localized

Build Status

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

  1. Fork it ( https://github.com/[my-github-username]/virtus-localized/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request

License

MIT