Class: Dry::Schema::Messages::I18n

Inherits:
Abstract
  • Object
show all
Defined in:
lib/dry/schema/messages/i18n.rb

Overview

I18n message backend

Constant Summary

Constants inherited from Abstract

Abstract::DEFAULT_PATH

Instance Attribute Summary collapse

Attributes inherited from Abstract

#config

Instance Method Summary collapse

Methods inherited from Abstract

cache, #cache, #call, #hash, #lookup, #lookup_paths, #namespaced, #root, #rule

Constructor Details

#initializeI18n

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of I18n.



15
16
17
18
# File 'lib/dry/schema/messages/i18n.rb', line 15

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

Instance Attribute Details

#tObject (readonly)



10
11
12
# File 'lib/dry/schema/messages/i18n.rb', line 10

def t
  @t
end

Instance Method Details

#default_localeObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



55
56
57
# File 'lib/dry/schema/messages/i18n.rb', line 55

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

#get(key, options = {}) ⇒ String

Get a message for the given key and its options

Parameters:

  • key (Symbol)
  • options (Hash) (defaults to: {})

Returns:

  • (String)


28
29
30
# File 'lib/dry/schema/messages/i18n.rb', line 28

def get(key, options = {})
  t.(key, options) if key
end

#key?(key, options) ⇒ Boolean

Check if given key is defined

Returns:

  • (Boolean)


37
38
39
40
# File 'lib/dry/schema/messages/i18n.rb', line 37

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

#merge(path) ⇒ Messages::I18n

Merge messages from an additional path

Parameters:

  • path (String)

Returns:



49
50
51
52
# File 'lib/dry/schema/messages/i18n.rb', line 49

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