Sha256: 2b8dc614f8b9bf0e4d5ef605d3e066ed39bb16dc55352eb791a3fb8a276cfaf9

Contents?: true

Size: 1.05 KB

Versions: 31

Compression:

Stored size: 1.05 KB

Contents

# frozen_string_literal: true
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

31 entries across 31 versions & 1 rubygems

Version Path
activeadmin-3.1.0 lib/active_admin/localizers/resource_localizer.rb
activeadmin-3.0.0 lib/active_admin/localizers/resource_localizer.rb
activeadmin-2.14.0 lib/active_admin/localizers/resource_localizer.rb
activeadmin-2.13.1 lib/active_admin/localizers/resource_localizer.rb
activeadmin-2.13.0 lib/active_admin/localizers/resource_localizer.rb
activeadmin-2.12.0 lib/active_admin/localizers/resource_localizer.rb
activeadmin-2.11.2 lib/active_admin/localizers/resource_localizer.rb
activeadmin-2.11.1 lib/active_admin/localizers/resource_localizer.rb
activeadmin-2.11.0 lib/active_admin/localizers/resource_localizer.rb
activeadmin-2.10.1 lib/active_admin/localizers/resource_localizer.rb
activeadmin-2.10.0 lib/active_admin/localizers/resource_localizer.rb