Class: PhraseApp::Android::FileFormatter

Inherits:
PhraseAppClient show all
Defined in:
lib/phraseapp_android/file_formatter.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

#apply(file_name, locale) ⇒ Object



5
6
7
8
9
# File 'lib/phraseapp_android/file_formatter.rb', line 5

def apply(file_name, locale)
  doc = read_locale_file file_name, locale
  formatted = apply_to_xml_doc doc
  File.write locale_file_name(file_name, locale), formatted
end

#apply_to_all_filesObject



49
50
51
52
53
54
55
56
57
# File 'lib/phraseapp_android/file_formatter.rb', line 49

def apply_to_all_files
  count = 0
  ([nil] + locales).each do |locale|
    apply 'strings', locale
    apply 'arrays', locale
    count += 2
  end
  puts "#{count} files were reformatted.".green
end

#apply_to_xml_doc(doc) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/phraseapp_android/file_formatter.rb', line 11

def apply_to_xml_doc(doc)
  data = {}
  ignore_keys = []

  match_elements doc, 'integer-array', data, ignore_keys
  match_elements doc, 'string-array', data, ignore_keys
  match_elements doc, 'string', data, ignore_keys

  last_name = nil
  builder = Nokogiri::XML::Builder.new(encoding: 'UTF-8') do |xml|
    xml.resources('xmlns:tools' => 'http://schemas.android.com/tools') do
      data.keys.sort.each do |k|
        name = k.split('_').first.to_s

        if last_name != name
          xml.comment " #{name} * "
          last_name = name
        end

        attrs = {name: k}
        attrs['tools:ignore'] = 'MissingTranslation' if ignore_keys.include? k

        xml.send data[k][:name], attrs do
          if data[k][:name].end_with?('array')
            data[k][:value].each do |t|
              xml.item t
            end
          else
            xml.text data[k][:value]
          end
        end
      end
    end
  end

  builder.to_xml indent: 4
end