Class: PhraseApp::Android::MissingTranslations

Inherits:
PhraseAppClient show all
Defined in:
lib/phraseapp_android/missing_translations.rb

Instance Attribute Summary

Attributes inherited from PhraseAppClient

#client, #locale_files, #locales, #project_id, #sub_path

Instance Method Summary collapse

Methods inherited from PhraseAppClient

#initialize, #locale_file_name, #read_locale_file, #read_xml_file

Constructor Details

This class inherits a constructor from PhraseApp::Android::PhraseAppClient

Instance Method Details

#find(locale) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/phraseapp_android/missing_translations.rb', line 18

def find(locale)
  list = []

  default_strings, default_arrays = load_locale_files nil
  localized_strings, localized_arrays = load_locale_files locale

  default_strings.each do |name, el|
    list << el unless localized_strings.has_key?(name)
  end

  default_arrays.each do |name, el|
    list << el if !localized_arrays.has_key?(name) || el.search('item').size != localized_arrays[name].search('item').size
  end

  list
end

#listObject



5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/phraseapp_android/missing_translations.rb', line 5

def list
  count = 0
  locales.each do |locale|
    missing = find locale
    if missing.size > 0
      puts "Missing translations for #{locale.upcase} locale:".red
      missing.each { |s| puts s.to_s.yellow }
      count += missing.size
    end
  end
  puts 'No missing translations found.'.green if count == 0
end

#pullObject



35
36
37
38
39
# File 'lib/phraseapp_android/missing_translations.rb', line 35

def pull
  locales.each do |locale|
    pull_locale locale
  end
end

#pull_locale(locale) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/phraseapp_android/missing_translations.rb', line 41

def pull_locale(locale)
  stats = {
      strings: {added: 0, updated: 0},
      arrays: {added: 0, updated: 0}
  }

  default_strings = doc_to_hash read_locale_file('strings', nil)
  default_arrays = doc_to_hash read_locale_file('arrays', nil)
  default = default_strings.merge default_arrays

  strings = doc_to_hash read_locale_file('strings', locale)
  arrays = doc_to_hash read_locale_file('arrays', locale)
  current = strings.merge arrays

  params = PhraseApp::RequestParams::LocaleDownloadParams.new file_format: 'xml'
  doc = Nokogiri::XML client.locale_download(project_id, locale, params)
  recent = doc_to_hash doc

  merge_translations default, current, recent, :strings, stats, locale
  merge_translations default, current, recent, :arrays, stats, locale

  formatter = PhraseApp::Android::FileFormatter.new

  if stats[:strings][:added] + stats[:strings][:updated] > 0
    updated_strings = formatter.apply_to_xml_doc hash_to_doc(current[:strings])
    write_to_file locale_file_name('strings', locale), updated_strings
  end

  if stats[:arrays][:added] + stats[:arrays][:updated] > 0
    updated_arrays = formatter.apply_to_xml_doc hash_to_doc(current[:arrays])
    write_to_file locale_file_name('arrays', locale), updated_arrays
  end

  added = stats[:strings][:added] + stats[:arrays][:added]
  updated = stats[:strings][:updated] + stats[:arrays][:updated]

  if added + updated > 0
    puts "#{added + updated} keys were updated for #{locale} locale!".green
  else
    puts "no keys were updated for #{locale} locale.".yellow
  end

  stats
end