class Dry::Schema::Messages::YAML

@api public
Plain YAML message backend

def self.build(options = EMPTY_HASH)

Other tags:
    Api: - private
def self.build(options = EMPTY_HASH)
  super do |config|
    config.default_locale = :en unless config.default_locale
    config.root = "%<locale>s.#{config.root}"
    config.rule_lookup_paths = config.rule_lookup_paths.map { |path|
      "%<locale>s.#{path}"
    }
  end
end

def self.flat_hash(hash, path = [], keys = {})

Other tags:
    Api: - private
def self.flat_hash(hash, path = [], keys = {})
  hash.each do |key, value|
    flat_hash(value, [*path, key], keys) if value.is_a?(Hash)
    if value.is_a?(String) && hash['text'] != value
      keys[[*path, key].join(DOT)] = {
        text: value,
        meta: EMPTY_HASH
      }
    elsif value.is_a?(Hash) && value['text'].is_a?(String)
      keys[[*path, key].join(DOT)] = {
        text: value['text'],
        meta: value.dup.delete_if { |k| k == 'text' }.map { |k, v| [k.to_sym, v] }.to_h
      }
    end
  end
  keys
end

def evaluated_key(key, options)

Other tags:
    Api: - private
def evaluated_key(key, options)
  return key unless key.include?(LOCALE_TOKEN)
  key % { locale: options[:locale] || default_locale }
end

def get(key, options = EMPTY_HASH)

Other tags:
    Api: - public

Returns:
  • (String) -

Parameters:
  • options (Hash) --
  • key (Symbol) --
def get(key, options = EMPTY_HASH)
  data[evaluated_key(key, options)]
end

def initialize(data: EMPTY_HASH, config: nil)

Other tags:
    Api: - private
def initialize(data: EMPTY_HASH, config: nil)
  super()
  @data = data
  @config = config if config
  @t = proc { |key, locale: default_locale| get("%<locale>s.#{key}", locale: locale) }
end

def key?(key, options = EMPTY_HASH)

Other tags:
    Api: - public

Returns:
  • (Boolean) -
def key?(key, options = EMPTY_HASH)
  data.key?(evaluated_key(key, options))
end

def load_translations(path)

Other tags:
    Api: - private
def load_translations(path)
  data = self.class.flat_hash(YAML.load_file(path))
  return data unless custom_top_namespace?(path)
  data.map { |k, v| [k.gsub(DEFAULT_MESSAGES_ROOT, config.top_namespace), v] }.to_h
end

def merge(overrides)

Other tags:
    Api: - public

Returns:
  • (Messages::I18n) -

Parameters:
  • overrides (String) --
def merge(overrides)
  if overrides.is_a?(Hash)
    self.class.new(
      data: data.merge(self.class.flat_hash(overrides)),
      config: config
    )
  else
    self.class.new(
      data: Array(overrides).reduce(data) { |a, e| a.merge(load_translations(e)) },
      config: config
    )
  end
end

def prepare

Other tags:
    Api: - private
def prepare
  @data = config.load_paths.map { |path| load_translations(path) }.reduce(:merge)
  self
end