Class: XliffTransReader

Inherits:
Object
  • Object
show all
Defined in:
lib/transync/xliff_trans/xliff_trans_reader.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dir, file, language, languages) ⇒ XliffTransReader

Returns a new instance of XliffTransReader.



6
7
8
9
10
11
# File 'lib/transync/xliff_trans/xliff_trans_reader.rb', line 6

def initialize(dir, file, language, languages)
  self.dir = dir
  self.file = file
  self.language = language
  self.languages = languages
end

Instance Attribute Details

#dirObject

Returns the value of attribute dir.



4
5
6
# File 'lib/transync/xliff_trans/xliff_trans_reader.rb', line 4

def dir
  @dir
end

#fileObject

Returns the value of attribute file.



4
5
6
# File 'lib/transync/xliff_trans/xliff_trans_reader.rb', line 4

def file
  @file
end

#languageObject

Returns the value of attribute language.



4
5
6
# File 'lib/transync/xliff_trans/xliff_trans_reader.rb', line 4

def language
  @language
end

#languagesObject

Returns the value of attribute languages.



4
5
6
# File 'lib/transync/xliff_trans/xliff_trans_reader.rb', line 4

def languages
  @languages
end

Instance Method Details

#get_translationsObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/transync/xliff_trans/xliff_trans_reader.rb', line 13

def get_translations
  data = {
    language: language,
    translations: []
  }

  open_file do |doc|
    # hacky hack, xliff is dirty as hell
    doc.remove_namespaces!
    doc.xpath('//trans-unit').each do |node|
      data[:translations] << {
        key: node.xpath('source').text,
        value: node.xpath('target').text,
      }
    end
  end

  data
end

#open_fileObject

Reading from source tags in xliff



74
75
76
77
78
79
80
81
82
# File 'lib/transync/xliff_trans/xliff_trans_reader.rb', line 74

def open_file
  begin
    xml_file = File.open(file_path)
    doc = Nokogiri::XML(xml_file)
    yield doc
  rescue Errno::ENOENT => e
    abort(e.message)
  end
end

#valid?(create = false) ⇒ Boolean

will go through each and find if any xliff is missing keys for translations

Returns:

  • (Boolean)


34
35
36
37
38
39
40
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
# File 'lib/transync/xliff_trans/xliff_trans_reader.rb', line 34

def valid?(create = false)
  missing = 0
  missing_translation_text = GdocTrans::CONFIG['MISSING_TRANSLATION_TEXT'] || '#MISSING-TRANS#'
  all_translations_for_language = {language: nil, translations: []}

  self.get_translations[:translations].each do |x_trans|
    self.languages.each do |key_lang|
      next if key_lang == language

      xliff_reader = XliffTransReader.new(self.dir, self.file, key_lang, self.languages)
      xliff_lang = xliff_reader.get_translations[:translations]
      xliff_lang_value = xliff_lang.detect{ |xt| xt[:key] == x_trans[:key] }

      all_translations_for_language[:language] = key_lang

      if xliff_lang_value.nil?
        p "#{file}.#{key_lang} is missing translation for key '#{x_trans[:key]}'" unless create
        return false unless create

        p "#{missing + 1}. #{file}.#{key_lang} was missing translation for key '#{x_trans[:key]}'."
        all_translations_for_language[:translations] << {
            key: x_trans[:key],
            value: "#{missing_translation_text} - #{x_trans[:key]}"
        }
        missing += 1
      else
        all_translations_for_language[:translations] << xliff_lang_value
      end
    end
  end

  if missing > 0 and create
    xliff_trans_writer = XliffTransWriter.new(dir, file, all_translations_for_language)
    xliff_trans_writer.save
  end

  missing == 0
end