Sha256: 8752818a8b7ab3f6b501645f55f4c0ce95d97a896bdde1684167e33012caa94d
Contents?: true
Size: 1.86 KB
Versions: 6
Compression:
Stored size: 1.86 KB
Contents
# LocalizableModel LocalizableModel allows any ActiveRecord model to have localized attributes. [](https://travis-ci.org/kord-as/localizable_model) [](https://codeclimate.com/github/kord-as/localizable_model) [](https://codeclimate.com/github/kord-as/localizable_model) [](https://gemnasium.com/kord-as/localizable_model) ## Installation Add the `localizable_model` gem to your Gemfile: ``` ruby gem "localizable_model" ``` Generate the migration: ``` shell bin/rails g localizable_model:migration ``` ## Usage You can now define localizable attributes on your model: ``` ruby class Page < ActiveRecord::Base localizable do attribute :name attribute :body end end ``` You can also use a dictionary. This will let you define attributes dynamically at runtime. ``` ruby class Page < ActiveRecord::Base localizable do dictionary lambda { Page.localizable_attrs } end class << self def localizable_attrs [:foo, :bar, :baz] end end end ``` Usage examples: ``` ruby page = Page.create(locale: "en", name: "Hello") page.name? # => true page.name.to_s # => "Hello" ``` To get a localized version of a page, call `.localize` on it: ``` ruby page = Page.first.localize("en") ``` `.localize` can also take a block argument: ``` ruby page.localize("nb") do |p| p.locale # => "nb" end p.locale # => "en" ``` Multiple locales can be updated at the same time: ``` ruby page.name = { en: "Hello", fr: "Bonjour" } ``` ## License LocalizableModel is licensed under the [MIT License](http://www.opensource.org/licenses/MIT).
Version data entries
6 entries across 6 versions & 1 rubygems