Class: Dry::Schema::Messages::I18n
- Defined in:
- lib/dry/schema/messages/i18n.rb
Overview
I18n message backend
Instance Attribute Summary collapse
-
#t ⇒ Method
readonly
Translation function.
Instance Method Summary collapse
- #default_locale ⇒ Object private
-
#get(key, options = EMPTY_HASH) ⇒ String
Get a message for the given key and its options.
-
#initialize ⇒ I18n
constructor
private
A new instance of I18n.
- #interpolatable_data(_key, _options, **data) ⇒ Object private
- #interpolate(key, options, **data) ⇒ Object private
-
#key?(key, options) ⇒ Boolean
Check if given key is defined.
-
#merge(paths) ⇒ Messages::I18n
Merge messages from an additional path.
- #prepare(paths = config.load_paths) ⇒ Object private
Methods inherited from Abstract
build, #call, #filled_lookup_paths, #looked_up_paths, #lookup_paths, #namespaced, #root, #rule, #rule_lookup_paths, setting_names, #translate
Constructor Details
#initialize ⇒ I18n
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.
19 20 21 22 |
# File 'lib/dry/schema/messages/i18n.rb', line 19 def initialize super @t = ::I18n.method(:t) end |
Instance Attribute Details
#t ⇒ Method (readonly)
Translation function
16 17 18 |
# File 'lib/dry/schema/messages/i18n.rb', line 16 def t @t end |
Instance Method Details
#default_locale ⇒ 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.
74 75 76 |
# File 'lib/dry/schema/messages/i18n.rb', line 74 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
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/dry/schema/messages/i18n.rb', line 32 def get(key, = EMPTY_HASH) return unless key [:locale] ||= default_locale result = t.(key, **) if result.is_a?(Hash) text = result[:text] = result.dup.tap { |h| h.delete(:text) } else text = result = EMPTY_HASH.dup end { text: text, 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.
100 101 102 |
# File 'lib/dry/schema/messages/i18n.rb', line 100 def interpolatable_data(_key, , **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.
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/dry/schema/messages/i18n.rb', line 105 def interpolate(key, , **data) text_key = "#{key}.text" opts = { locale: default_locale, **, **data } resolved_key = key?(text_key, opts) ? text_key : key result = t.(resolved_key, **opts) return result unless result.is_a?(Hash) result[:text] end |
#key?(key, options) ⇒ Boolean
Check if given key is defined
57 58 59 60 |
# File 'lib/dry/schema/messages/i18n.rb', line 57 def key?(key, ) ::I18n.exists?(key, .fetch(:locale, default_locale)) || ::I18n.exists?(key, ::I18n.default_locale) end |
#merge(paths) ⇒ Messages::I18n
Merge messages from an additional path
69 70 71 |
# File 'lib/dry/schema/messages/i18n.rb', line 69 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.
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/dry/schema/messages/i18n.rb', line 79 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.transform_values { |v| {top_namespace => v[DEFAULT_MESSAGES_ROOT]} } store_translations(mapped_data) else store_translations(data) end end self end |