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
85
86
# File 'lib/phraseapp_android/missing_translations.rb', line 41

def pull_locale(locale)
  strings_updated = 0
  arrays_updated = 0
  missing = find locale
  if missing.size > 0
    strings = read_locale_file 'strings', locale
    arrays = read_locale_file 'arrays', locale

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

    missing.each do |el|
      name = el.attr 'name'
      translated = doc.at('//resources').search("#{el.name}[@name=#{name}]").first
      if translated
        if el.name == 'string'
          str = Nokogiri::XML::Node.new 'string', strings
          str['name'] = name
          str.content = translated.text
          strings.at('//resources').children.last.after str
          strings_updated += 1
        elsif el.name == 'string-array'
          str = Nokogiri::XML::Node.new 'string-array', arrays
          str['name'] = name
          translated.element_children.each do |child|
            item = Nokogiri::XML::Node.new 'item', arrays
            item.content = child.text
            str.add_child item
          end
          arrays.at('//resources').children.last.after str
          arrays_updated += 1
        end
      end
    end

    if strings_updated + arrays_updated > 0
      PhraseApp::Android::FileFormatter.new.apply('strings', locale) if strings_updated > 0
      PhraseApp::Android::FileFormatter.new.apply('arrays', locale) if arrays_updated > 0
      puts "#{strings_updated + arrays_updated} translation keys were updated.".green
    else
      puts 'no keys were updated.'.yellow
    end
  end

  [strings_updated, arrays_updated]
end