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-4.0.0.beta14 lib/active_admin/localizers/resource_localizer.rb
activeadmin-4.0.0.beta13 lib/active_admin/localizers/resource_localizer.rb
activeadmin-4.0.0.beta12 lib/active_admin/localizers/resource_localizer.rb
activeadmin-3.2.5 lib/active_admin/localizers/resource_localizer.rb
activeadmin-4.0.0.beta11 lib/active_admin/localizers/resource_localizer.rb
activeadmin-3.2.4 lib/active_admin/localizers/resource_localizer.rb
activeadmin-4.0.0.beta10 lib/active_admin/localizers/resource_localizer.rb
activeadmin-4.0.0.beta9 lib/active_admin/localizers/resource_localizer.rb
activeadmin-3.2.3 lib/active_admin/localizers/resource_localizer.rb
activeadmin-4.0.0.beta8 lib/active_admin/localizers/resource_localizer.rb
activeadmin-4.0.0.beta7 lib/active_admin/localizers/resource_localizer.rb
activeadmin-3.2.2 lib/active_admin/localizers/resource_localizer.rb
activeadmin-4.0.0.beta6 lib/active_admin/localizers/resource_localizer.rb
activeadmin-3.2.1 lib/active_admin/localizers/resource_localizer.rb
activeadmin-4.0.0.beta5 lib/active_admin/localizers/resource_localizer.rb
activeadmin-4.0.0.beta4 lib/active_admin/localizers/resource_localizer.rb
activeadmin-4.0.0.beta3 lib/active_admin/localizers/resource_localizer.rb
activeadmin-4.0.0.beta2 lib/active_admin/localizers/resource_localizer.rb
activeadmin-4.0.0.beta1 lib/active_admin/localizers/resource_localizer.rb
activeadmin-3.2.0 lib/active_admin/localizers/resource_localizer.rb