Sha256: ac04e1c1146deedb7f77e8fc85107d1235f7272f96f7c507c141802f80c57d9f

Contents?: true

Size: 1.02 KB

Versions: 4

Compression:

Stored size: 1.02 KB

Contents

module ActiveAdmin
  module Localizers
    class ResourceLocalizer
      class << self
        def from_resource(resource_config)
          new(resource_config.resource_name.i18n_key, resource_config.resource_label)
        end

        def translate(key, options)
          new(options.delete(:model_name), options.delete(:model)).translate(key, options)
        end
        alias_method :t, :translate
      end

      def initialize(model_name, model = nil)
        @model_name = model_name
        @model = model || model_name.to_s.titleize
      end

      def translate(key, options = {})
        scope = options.delete(:scope)
        specific_key = array_to_key("resources", @model_name, scope, key)
        defaults = [array_to_key(scope, key), key.to_s.titleize]
        ::I18n.t specific_key, **options.reverse_merge(model: @model, default: defaults, scope: "active_admin")
      end
      alias_method :t, :translate

      protected

      def array_to_key(*arr)
        arr.flatten.compact.join(".").to_sym
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 2 rubygems

Version Path
date_n_time_picker_activeadmin-0.1.2 vendor/bundle/ruby/2.6.0/gems/activeadmin-2.9.0/lib/active_admin/localizers/resource_localizer.rb
date_n_time_picker_activeadmin-0.1.1 vendor/bundle/ruby/2.6.0/gems/activeadmin-2.9.0/lib/active_admin/localizers/resource_localizer.rb
activeadmin-2.9.0 lib/active_admin/localizers/resource_localizer.rb
activeadmin-2.8.1 lib/active_admin/localizers/resource_localizer.rb