Class: Localeapp::Updater

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

Instance Method Summary collapse

Instance Method Details

#update(data) ⇒ Object



4
5
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
# File 'lib/localeapp/updater.rb', line 4

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 = YAML.load(File.read(filename))
      if data['translations'] && data['translations'][short_code]
        new_data = { short_code => data['translations'][short_code] }
        translations.deep_merge!(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 translations.ya2yaml[5..-1]
      end
    end
  end
end