Socket
Book a DemoInstallSign in
Socket

localizable_model

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

localizable_model

bundlerRubygems
Version
0.6.4
Version published
Maintainers
1
Created
Source

Version Build Status Code Climate Test Coverage Inline docs Security

LocalizableModel

LocalizableModel allows any ActiveRecord model to have localized attributes.

Installation

Add the localizable_model gem to your Gemfile:

gem "localizable_model"

Generate the migration:

bin/rails g localizable_model:migration

Usage

You can now define localizable attributes on your model:

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.

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:

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:

page = Page.first.localize("en")

.localize can also take a block argument:

page.localize("nb") do |p|
  p.locale # => "nb"
end
p.locale # => "en"

Multiple locales can be updated at the same time:

page.name = { en: "Hello", fr: "Bonjour" }

By chaining through .any_locale, you can get results from other locales if a localization is missing.

page = Page.create(locale: "fr", name: "Bonjour").localize("en")
page.any_locale.name? # => true
page.any_locale.name  # => "Bonjour"

License

LocalizableModel is licensed under the MIT License.

FAQs

Package last updated on 17 Aug 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.