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(path, file, languages) ⇒ XliffTransReader

Returns a new instance of XliffTransReader.



9
10
11
12
13
14
# File 'lib/transync/xliff_trans/xliff_trans_reader.rb', line 9

def initialize(path, file, languages)
  self.path       = path
  self.file       = file
  self.languages  = languages
  self.all_translations_for_language = {file: file, language: nil, translations: {}}
end

Instance Attribute Details

#all_translations_for_languageObject

Returns the value of attribute all_translations_for_language.



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

def all_translations_for_language
  @all_translations_for_language
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

#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

#pathObject

Returns the value of attribute path.



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

def path
  @path
end

Instance Method Details

#open_file(language) ⇒ Object

Reading from source tags in xliff



58
59
60
61
62
63
64
65
66
# File 'lib/transync/xliff_trans/xliff_trans_reader.rb', line 58

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

#translations(language) ⇒ Object



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

def translations(language)
  data = { file: file, language: language, translations: {} }

  open_file(language) do |doc|
    doc.remove_namespaces!
    doc.xpath('//trans-unit').each do |node|
      key   = node.xpath('source').text
      value = node.xpath('target').text
      data[:translations][key] = value
    end
  end

  data
end

#valid?Boolean

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

Returns:

  • (Boolean)


32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/transync/xliff_trans/xliff_trans_reader.rb', line 32

def valid?
  missing = 0
  missing_translation_text = TransyncConfig::CONFIG['MISSING_TRANSLATION_TEXT'] || '#MISSING-TRANS#'

  self.translations(self.languages.first)[:translations].keys.each do |x_trans_key|
    self.languages.each do |key_lang|
      xliff_reader = XliffTransReader.new(self.path, self.file, self.languages)
      xliff_lang = xliff_reader.translations(key_lang)[:translations]
      xliff_lang_value = xliff_lang[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}'"
        all_translations_for_language[:translations][x_trans_key] = "#{missing_translation_text} - #{x_trans_key}"
        missing += 1
      else
        all_translations_for_language[:translations][x_trans_key] = xliff_lang_value
      end
    end
  end

  missing == 0
end