Class: PoAndXliffConsolidator::Extract

Inherits:
Object
  • Object
show all
Includes:
Logging, FileHandle, Manipulate, Transform
Defined in:
lib/po_and_xliff_consolidator/extract.rb

Instance Attribute Summary

Attributes included from FileHandle

#app_name, #path_templates, #root_file_path

Attributes included from Manipulate

#reset_identical_msgid_and_msgstr, #skip_regexes, #skip_strings

Instance Method Summary collapse

Methods included from Logging

included, logger, #logger, logger=

Methods included from Transform

#chomped, intelligent_chomp, #match_with_ending, #matched_string

Methods included from FileHandle

#combined_file_name, #get_path, #initialize, #web_app_file_pointer, #web_app_filename, #xcode_doc, #xcode_file_name

Methods included from Manipulate

#add_translation_unit, #initialize, #reset_stores, #set_language_codes, #should_skip?

Instance Method Details

#check_string_format_specifiers(msgid, msgstr) ⇒ Object



193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# File 'lib/po_and_xliff_consolidator/extract.rb', line 193

def check_string_format_specifiers(msgid, msgstr)
  if msgid.include? '%'
    logs = []
    logs << 'Checking for matching string format specifiers... '
    logs << msgid
    logs << msgstr

    msgid_temp = msgid.dup
    msgstr_temp = msgstr.dup

    extract_regexes.each do |intent, regex|
      results = msgid_temp.scan(regex)
      results.each do |result|
        result = result.join('') if result.is_a? Array
        if msgstr_temp.index result
          logs << "Matched #{result}"
          msgid_temp.sub!(result, '')
          msgstr_temp.sub!(result, '')
        else
          logs.each do |log|
            logger.debug log
          end
          throw "Cannot find #{result}"
        end
      end
    end

    if msgid_temp.include? '%'
      throw "Missed something in #{msgid}"
    end

    if msgstr_temp.include? '%'
      throw "Something extra in #{msgstr}"
    end
  end
end

#extract_regexesObject



184
185
186
187
188
189
190
191
# File 'lib/po_and_xliff_consolidator/extract.rb', line 184

def extract_regexes
  @extract_regexes ||= {
      fastgettext_regex: /%\{[a-zA-Z0-9_]+\}/,
      xyz_regex: /%[a-zA-Z0-9_]+%/,
      xcode_regex: /(%)([\d]+[$]+)*(h|hh|l|ll|q|L|z|t|j)*(\$)*(.02f)*(@|%|d|D|u|U|x|X|o|O|f|e|E|g|G|c|C|s|S|p|a|A|F)/,
      n_percent_regex: /\d+%/
  }
end

#get_msgstr(msgid) ⇒ Object



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/po_and_xliff_consolidator/extract.rb', line 162

def get_msgstr(msgid)
  return msgid if should_skip?(msgid)

  key = TranslateUnit::msgid_key(msgid)
  tu = @translation_units.find { |tu| tu.msgid_downcase == key }
  if tu
    if msgid.include? '%'
      check_string_format_specifiers(msgid, tu.msgstr)
    end

    if msgid == tu.msgid
      return tu.msgstr
    else
      return match_with_ending(msgid, tu)
    end
  else
    throw "Cannot find `#{msgid}` - looking for `#{key}`"
    return msgid
  end

end

#process(language_code) ⇒ Object



18
19
20
21
22
23
24
# File 'lib/po_and_xliff_consolidator/extract.rb', line 18

def process(language_code)
  set_language_codes(language_code)
  reset_stores
  process_combined_file
  update_po_file
  update_xliff_file
end

#process_combined_fileObject



26
27
28
29
30
31
32
33
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
# File 'lib/po_and_xliff_consolidator/extract.rb', line 26

def process_combined_file
  fp = File.open(combined_file_name, 'r')

  while (line = fp.gets)
    @headers << line
    break if line.strip == ''
  end

  while (line = fp.gets)
    if match1 = @msgid_regex.match(line)
      msgid = match1[1]
      if line = fp.gets
        while match1c = @continuation_regex.match(line)
          msgid += match1c[1]
          line = fp.gets
        end
        if match2 = @msgstr_regex.match(line)
          msgstr = match2[1]
          if line = fp.gets
            while match2c = @continuation_regex.match(line)
              msgstr += match2c[1]
              line = fp.gets
            end
          end
          add_translation_unit(msgid, msgstr)
        elsif match2 = @msgid_plural_regex.match(line)
          block = [match1[0], match2[0]]
          line = fp.gets
          while line && line.strip != ''
            block << line
            line = fp.gets
          end
          @unsolved_blocks << block
        else
          throw "I don't know what to do.."
        end
      end

    end
  end
  fp.close
end

#update_po_fileObject



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/po_and_xliff_consolidator/extract.rb', line 69

def update_po_file
  fp = web_app_file_pointer(:need_translating, 'r')
  fp2 = web_app_file_pointer(:translated, 'w')

  while (line = fp.gets)
    fp2.puts line
    break if line.strip == ''
  end

  while (line = fp.gets)
    if match1 = @msgid_regex.match(line)
      msgid = match1[1]
      msgid_line = line
      if line = fp.gets
        if @msgstr_regex.match(line)
          msgstr = get_msgstr(msgid)
          fp2.puts msgid_line
          fp2.puts "msgstr \"#{msgstr}\""
          fp2.puts
        elsif @msgid_plural_regex.match(line)
          line = fp.gets
          while line.strip != ''
            line = fp.gets
          end
        else
          throw "I don't know what to do.."
        end
      end
    end
  end

  @unsolved_blocks.each do |ub|
    ub.each do |ube|
      fp2.puts ube
    end
    fp2.puts
  end


  fp.close
  fp2.close

end

#update_xliff_fileObject



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/po_and_xliff_consolidator/extract.rb', line 113

def update_xliff_file
  changes = 0

  xcode_doc = xcode_doc(:need_translating)

  xcode_doc.xpath('//xmlns:file').each do |xcode_file_node|
    xcode_file_node.xpath('xmlns:body/xmlns:trans-unit').each do |xcode_trans_unit_node|
      xcode_source_nodes = xcode_trans_unit_node.xpath('xmlns:source')
      if xcode_source_nodes.count != 1
        throw "I don't know what to do!"
      end
      msgid = xcode_source_nodes.first.text
      msgstr = get_msgstr(msgid)

      xcode_targets = xcode_trans_unit_node.xpath('xmlns:target')
      xtc = xcode_targets.count
      if xtc == 0
        if msgstr != ''
          new_node = Nokogiri::XML::Node.new('target', xcode_doc)
          new_node.content = msgstr
          xcode_source_nodes.last.add_next_sibling new_node
          changes += 1
        else
          logger.info "Not creating #{msgid} because content is blank"
        end

      elsif xtc == 1
        if xcode_targets.text.strip != msgstr.strip
          xcode_targets.last.content = msgstr
          changes += 1
        else
          logger.debug "No change for #{msgid}"
        end
      else
        throw "I don't know what to do if there are two target nodes"
      end
    end
  end

  if changes > 0
    logger.debug 'Saving xliff..'
    doc_string = xcode_doc.to_s.gsub('</source><target>', "</source>\n        <target>")
    File.write(xcode_file_name(:translated), doc_string)
  else
    logger.debug 'No changes - not saving!'
  end

end