Sha256: 72fd5e46e7f39eb5a1d4bfb8043c57874319aa528eebaacb2f8235fbb5298f2c

Contents?: true

Size: 1021 Bytes

Versions: 1

Compression:

Stored size: 1021 Bytes

Contents

require 'i18n'
require 'dry/validation/messages/abstract'

module Dry
  module Validation
    class Messages::I18n < Messages::Abstract
      attr_reader :t

      ::I18n.load_path.concat(config.paths)

      def initialize
        super
        @t = I18n.method(:t)
      end

      def call(predicate, options = EMPTY_HASH)
        super do |path, opts|
          get(path, opts)
        end
      end
      alias_method :[], :call

      def get(key, options = {})
        t.(key, locale: options.fetch(:locale, default_locale)) if key
      end

      def rule(name, options = {})
        path = "rules.#{name}"
        get(path, options) if key?(path, options)
      end

      def key?(key, options)
        ::I18n.exists?(key, options.fetch(:locale, default_locale)) ||
        ::I18n.exists?(key, I18n.default_locale)
      end

      def merge(path)
        ::I18n.load_path << path
        self
      end

      def default_locale
        I18n.locale || I18n.default_locale || super
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
dry-validation-0.13.3 lib/dry/validation/messages/i18n.rb