lib/dry/schema/messages/i18n.rb in dry-schema-1.4.1 vs lib/dry/schema/messages/i18n.rb in dry-schema-1.4.2

- old
+ new

@@ -27,11 +27,28 @@ # # @return [String] # # @api public def get(key, options = EMPTY_HASH) - t.(key, locale: options.fetch(:locale, default_locale)) if key + return unless key + + opts = { locale: options.fetch(:locale, default_locale) } + + translation = t.(key, opts) + text_key = "#{key}.text" + + if !translation.is_a?(Hash) || !key?(text_key, opts) + return { + text: translation, + meta: EMPTY_HASH + } + end + + { + text: t.(text_key, opts), + meta: extract_meta(key, translation, opts) + } end # Check if given key is defined # # @return [Boolean] @@ -96,9 +113,16 @@ I18n.available_locales |= locales locales.each do |locale| I18n.backend.store_translations(locale, data[locale.to_s]) + end + end + + def extract_meta(parent_key, translation, options) + translation.keys.each_with_object({}) do |k, meta| + meta_key = "#{parent_key}.#{k}" + meta[k] = t.(meta_key, options) if k != :text && key?(meta_key, options) end end end end end