Module: PoAndXliffConsolidator::Transform

Includes:
Logging
Included in:
Extract
Defined in:
lib/po_and_xliff_consolidator/transform.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Logging

included, logger, #logger, logger=

Class Method Details

.intelligent_chomp(str) ⇒ Object



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
48
# File 'lib/po_and_xliff_consolidator/transform.rb', line 16

def self.intelligent_chomp(str)
  beginning = ''
  ending = ''

  @leading_space_regex ||= /^[ \t]+/

  if match = @leading_space_regex.match(str)
    beginning += match[0]
    str = str.sub(@leading_space_regex, '')
  end

  @trailing_regexes ||= [
      /[ \t\n]+$/,
      /[!:…]+$/,
      /\.{1,3}$/,
      /[ \t\n]+$/
  ]

  @trailing_regexes.each do |trailing_regex|
    if match = trailing_regex.match(str)
      ending = match[0] + ending
      str = str.sub(trailing_regex, '')
    end
  end

  {
      str: str,
      beginning: beginning,
      ending: ending
  }


end

Instance Method Details

#chomped(string) ⇒ Object



50
51
52
# File 'lib/po_and_xliff_consolidator/transform.rb', line 50

def chomped(string)
  string.chomp(':').chomp('!').chomp('...').chomp('..').chomp('').strip
end

#match_with_ending(msgid, tu) ⇒ Object



8
9
10
11
12
13
14
# File 'lib/po_and_xliff_consolidator/transform.rb', line 8

def match_with_ending(msgid, tu)

  msgid_chomped = Transform.intelligent_chomp(msgid)
  tu_msgid_chomped = Transform.intelligent_chomp(tu.msgid)

  msgid_chomped[:beginning] + matched_string(msgid_chomped[:str], tu_msgid_chomped[:str], tu.msgstr) + msgid_chomped[:ending]
end

#matched_string(s1, s2, t) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/po_and_xliff_consolidator/transform.rb', line 54

def matched_string(s1, s2, t)
  if s1 == s2.upcase
    t.upcase
  elsif s1 == s2.downcase
    t.downcase
  elsif s1 == s2.capitalize
    t.capitalize
  elsif s1 == s2.split.map(&:capitalize).join(' ')
    t.split.map(&:capitalize).join(' ')
  else
    logger.info "Couldn't 100% match #{s1} to #{s2}.. using #{t}"
    t
  end
end