Class: Localeapp::Updater

Inherits:
Object
  • Object
show all
Defined in:
lib/localeapp/updater.rb

Instance Method Summary collapse

Instance Method Details

#dump(data) ⇒ Object



34
35
36
37
38
39
40
41
# File 'lib/localeapp/updater.rb', line 34

def dump(data)
  data.each do |locale, translations|
    filename = File.join(Localeapp.configuration.translation_data_directory, "#{locale}.yml")
    atomic_write(filename) do |file|
      file.write generate_yaml({locale => translations})
    end
  end
end

#update(data) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/localeapp/updater.rb', line 6

def update(data)
  data['locales'].each do |short_code|
    filename = File.join(Localeapp.configuration.translation_data_directory, "#{short_code}.yml")

    if File.exist?(filename)
      translations = Localeapp.load_yaml_file(filename)
      if data['translations'] && data['translations'][short_code]
        new_data = { short_code => data['translations'][short_code] }
        translations = deep_merge translations, new_data
      end
    else
      translations = { short_code => data['translations'][short_code] }
    end

    if data['deleted']
      data['deleted'].each do |key|
        remove_flattened_key!(translations, short_code, key)
      end
    end

    if translations[short_code]
      atomic_write(filename) do |file|
        file.write generate_yaml(translations)
      end
    end
  end
end