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

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

Overview

I18n message backend

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Abstract

build, #call, #filled_lookup_paths, #looked_up_paths, #lookup_paths, #namespaced, #root, #rule, #rule_lookup_paths, #translate

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.



18
19
20
21
# File 'lib/dry/schema/messages/i18n.rb', line 18

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

Instance Attribute Details

#tMethod (readonly)

Translation function

Returns:

  • (Method)


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

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.



72
73
74
# File 'lib/dry/schema/messages/i18n.rb', line 72

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

#get(key, options = EMPTY_HASH) ⇒ String

Get a message for the given key and its options

Parameters:

  • key (Symbol)
  • options (Hash) (defaults to: EMPTY_HASH)

Returns:

  • (String)


31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/dry/schema/messages/i18n.rb', line 31

def get(key, options = EMPTY_HASH)
  return unless key

  result = t.(key, locale: options.fetch(:locale, default_locale))

  if result.is_a?(Hash)
    text = result[:text]
    meta = result.dup.tap { |h| h.delete(:text) }
  else
    text = result
    meta = EMPTY_HASH.dup
  end

  {
    text: text,
    meta: meta
  }
end

#interpolatable_data(_key, _options, **data) ⇒ Object

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.



98
99
100
# File 'lib/dry/schema/messages/i18n.rb', line 98

def interpolatable_data(_key, _options, **data)
  data
end

#interpolate(key, options, **data) ⇒ Object

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.



103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/dry/schema/messages/i18n.rb', line 103

def interpolate(key, options, **data)
  text_key = "#{key}.text"

  opts = {
    locale: default_locale,
    **options,
    **data
  }

  resolved_key = key?(text_key, opts) ? text_key : key

  t.(resolved_key, **opts)
end

#key?(key, options) ⇒ Boolean

Check if given key is defined

Returns:

  • (Boolean)


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

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

#merge(paths) ⇒ Messages::I18n

Merge messages from an additional path

Parameters:

  • paths (String, Array<String>)

Returns:



67
68
69
# File 'lib/dry/schema/messages/i18n.rb', line 67

def merge(paths)
  prepare(paths)
end

#prepare(paths = config.load_paths) ⇒ Object

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.



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/dry/schema/messages/i18n.rb', line 77

def prepare(paths = config.load_paths)
  paths.each do |path|
    data = YAML.load_file(path)

    if custom_top_namespace?(path)
      top_namespace = config.top_namespace

      mapped_data = data
        .map { |k, v| [k, {top_namespace => v[DEFAULT_MESSAGES_ROOT]}] }
        .to_h

      store_translations(mapped_data)
    else
      store_translations(data)
    end
  end

  self
end