Module: FractualI18n::Backend

Defined in:
lib/fractual_i18n/backend.rb

Instance Method Summary collapse

Instance Method Details

#load_yml(filename) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/fractual_i18n/backend.rb', line 4

def load_yml(filename)
  if (fractual_path = FractualI18n.configuration.fractual_paths.find { |path| filename.starts_with?(path) })
    content = load_yml_file_unsafely(filename)
    keys = filename.delete_prefix(fractual_path).delete_prefix("/").split("/")
    last_key = keys.pop.delete_suffix(".yml")
    keys << last_key
    keys.map! { |key| key.delete_prefix("_") } # remove underscore from partial name
    FractualI18n.configuration.available_locales.each_with_object({}) do |locale, translations|
      next unless content[locale.to_s]

      translations[locale] = keys.reverse.inject(content[locale.to_s]) { |translation, key| { key => translation } }
    end
  else # default
    load_yml_file_unsafely(filename)
  end
rescue TypeError, ScriptError, StandardError => e
  raise I18n::InvalidLocaleData.new(filename, e.inspect)
end