Class: FractualI18n::Phrases

Inherits:
Object
  • Object
show all
Defined in:
lib/fractual_i18n/phrases.rb

Instance Method Summary collapse

Instance Method Details

#joined(locale:) ⇒ Object



4
5
6
7
8
9
10
11
# File 'lib/fractual_i18n/phrases.rb', line 4

def joined(locale:)
  original_load_path = I18n.load_path

  I18n.load_path = fractual_files
  I18n.with_locale(locale) { I18n.t(".") }
ensure
  I18n.load_path = original_load_path
end

#store(translations, locale:) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/fractual_i18n/phrases.rb', line 13

def store(translations, locale:)
  fractual_files.each do |filename|
    content = YAML.load_file(filename)

    fractual_path = FractualI18n.configuration.fractual_paths.find { |path| filename.starts_with?(path) }
    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

    if new_content = translations.dig(locale.to_s, *keys)
      content[locale.to_s] = content.fetch(locale.to_s, {}).merge(new_content)
    end

    File.write(filename, content.to_yaml(line_width: 200))
  end
end