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.
-
#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, cache, #cache, #call, #lookup, #lookup_paths, #namespaced, #root, #rule, #rule_lookup_paths, #translate
Constructor Details
Instance Attribute Details
#t ⇒ Method (readonly)
Translation function
15 16 17 |
# File 'lib/dry/schema/messages/i18n.rb', line 15 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.
57 58 59 |
# File 'lib/dry/schema/messages/i18n.rb', line 57 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
31 32 33 |
# File 'lib/dry/schema/messages/i18n.rb', line 31 def get(key, = EMPTY_HASH) t.(key, locale: .fetch(:locale, default_locale)) if key end |
#key?(key, options) ⇒ Boolean
Check if given key is defined
40 41 42 43 |
# File 'lib/dry/schema/messages/i18n.rb', line 40 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
52 53 54 |
# File 'lib/dry/schema/messages/i18n.rb', line 52 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.
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/dry/schema/messages/i18n.rb', line 62 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 |