Sha256: bf0cdb6bb2f79f13d18a0d2f9dc563b68c975f34adb657facdfcc38d3d722afc

Contents?: true

Size: 1.46 KB

Versions: 2

Compression:

Stored size: 1.46 KB

Contents

# frozen-string-literal: true
module I18nAccessors
=begin

Defines methods for a set of locales to access translated attributes in those
locales directly with a method call, using a suffix including the locale:

  article.title_pt_br

If no locales are passed as an option to the initializer,
+I18n.available_locales+ will be used by default.

@example
  class Post
    def title
      "title in #{I18n.locale}"
    end
    include I18nAccessors::Methods.new("title", locales: [:en, :fr])
  end

  I18n.locale = :en
  post = Post.new
  post.title
  #=> "title in en"
  post.title_fr
  #=> "title in fr"

=end
  class Methods < Module
    # @param [String] One or more attributes
    # @param [Array<Symbol>] Locales
    def initialize(*attributes, locales: I18n.available_locales)
      attributes.each do |attribute|
        locales.each do |locale|
          normalized_locale = I18nAccessors.normalize_locale(locale)
          define_method "#{attribute}_#{normalized_locale}" do |*args|
            I18nAccessors.i18n_class.with_locale(locale) { send(attribute, *args) }
          end
          define_method "#{attribute}_#{normalized_locale}?" do |*args|
            I18nAccessors.i18n_class.with_locale(locale) { send("#{attribute}?", *args) }
          end
          define_method "#{attribute}_#{normalized_locale}=" do |value, *args|
            I18nAccessors.i18n_class.with_locale(locale) { send("#{attribute}=", value, *args) }
          end
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
i18n_accessors-1.0.0 lib/i18n_accessors/methods.rb
i18n_accessors-0.1.2 lib/i18n_accessors/methods.rb