get_pomo 0.7.1 → 0.8.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: bb1e35bef67b57c020e28e8a0695678104b80ed1
4
- data.tar.gz: 5e489f0e04fab3e8510b7bc97ed698d7f20bc280
3
+ metadata.gz: 4a7082c11274ecb41c557230f20679ab1afc2b47
4
+ data.tar.gz: 96a578a70bf2e2690624d6b8f630da7237b55dc5
5
5
  SHA512:
6
- metadata.gz: 4dcbfcd7b5c29ff12b612c85b3edb06c7e7280be55c07f1d75c6fefbebcc9c91021087e6fc32df37fc6736cb33c4e713336b341da26b077b84620eade298e4b7
7
- data.tar.gz: 41da61f15f4bc18050a7cf0532ad39453f58064ec527e16264c6632ebb154eef2100d764e4c9f9e955d8e28fe0cec840f4f8efb2ccb5e17283b4c568118e72a2
6
+ metadata.gz: 435a39400d98fe210781d43bbf80db4a56fbfc7e747e23ba872b107fc6b04c44e60fa6e68c0967a5bf5ebc6d8edb1fcc763230ae09b836da306b5e0d2fd33200
7
+ data.tar.gz: cd614be59a4bbfb31a2b487f7c17bbf4721c14e29c7674cbb6a091fe940c986c74394f61b45a39581764088fd0a663988aa62bde84917255d48713c93a556192
@@ -3,13 +3,17 @@ require 'get_pomo/translation'
3
3
 
4
4
  module GetPomo
5
5
  class PoFile
6
- def self.parse(text)
7
- PoFile.new.add_translations_from_text(text)
6
+ def self.parse(text, options = {})
7
+ default_options = {:parse_obsoletes => false}
8
+ options = default_options.merge(options)
9
+ PoFile.new.add_translations_from_text(text, options)
8
10
  end
9
11
 
10
- def self.to_text(translations)
12
+ def self.to_text(translations, options = {})
13
+ default_options = {:merge => false}
14
+ options = default_options.merge(options)
11
15
  p = PoFile.new(:translations=>translations)
12
- p.to_text
16
+ p.to_text(options)
13
17
  end
14
18
 
15
19
  attr_reader :translations
@@ -21,7 +25,9 @@ module GetPomo
21
25
  #the text is split into lines and then converted into logical translations
22
26
  #each translation consists of comments(that come before a translation)
23
27
  #and a msgid / msgstr
24
- def add_translations_from_text(text)
28
+ def add_translations_from_text(text, options = {})
29
+ default_options = {:parse_obsoletes => false}
30
+ options = default_options.merge(options)
25
31
  start_new_translation
26
32
  text.gsub!(/^#{"\357\273\277"}/, "") #remove boom
27
33
  text.split(/$/).each_with_index do |line,index|
@@ -35,12 +41,14 @@ module GetPomo
35
41
  add_string line
36
42
  end
37
43
  end
38
- start_new_translation #instance_variable has to be overwritten or errors can occur on next add
44
+ start_new_translation(options[:parse_obsoletes]) #instance_variable has to be overwritten or errors can occur on next add
39
45
  translations
40
46
  end
41
47
 
42
- def to_text
43
- GetPomo.unique_translations(translations).map do |translation|
48
+ def to_text(options = {})
49
+ default_options = {:merge => false}
50
+ options = default_options.merge(options)
51
+ GetPomo.unique_translations(translations, options[:merge]).map do |translation|
44
52
  comment = translation.comment.to_s.split(/\n|\r\n/).map{|line|"#{line}\n"}*''
45
53
 
46
54
  msgctxt = if translation.msgctxt
@@ -61,7 +69,7 @@ module GetPomo
61
69
 
62
70
  msgids + (msgstrs*"\n")
63
71
  else
64
- %Q(msgid "#{translation.msgid}"\n)+
72
+ translation.obsolete? ? "" : %Q(msgid "#{translation.msgid}"\n)+
65
73
  %Q(msgstr "#{translation.msgstr}")
66
74
  end
67
75
 
@@ -90,7 +98,6 @@ module GetPomo
90
98
  def parse_method_call(line)
91
99
  method, string = line.match(/^\s*([a-z0-9_\[\]]+)(.*)/)[1..2]
92
100
  raise "no method found" unless method
93
-
94
101
  start_new_translation if %W(msgid msgctxt msgctxt).include? method and translation_complete?
95
102
  @last_method = method.to_sym
96
103
  add_string(string)
@@ -110,12 +117,15 @@ module GetPomo
110
117
  @current_translation.complete?
111
118
  end
112
119
 
113
- def store_translation
114
- @translations += [@current_translation] if @current_translation.complete?
120
+ def store_translation(parse_obsoletes = false)
121
+ if translation_complete? && (parse_obsoletes || !@current_translation.obsolete?)
122
+ @translations += [@current_translation]
123
+ end
124
+
115
125
  end
116
126
 
117
- def start_new_translation
118
- store_translation if translation_complete?
127
+ def start_new_translation(parse_obsoletes = false)
128
+ store_translation(parse_obsoletes) if translation_complete?
119
129
  @current_translation = Translation.new
120
130
  end
121
131
  end
@@ -13,9 +13,6 @@ module GetPomo
13
13
  self.msgstr ||= []
14
14
  msgstr[$1.to_i] = msgstr[$1.to_i].to_s + text
15
15
  elsif to.to_sym == :comment && text =~ OBSOLETE_REGEX
16
- # initialize msgid and msgstr on obsolete translations
17
- self.msgid ||= ""
18
- self.msgstr ||= ""
19
16
  send("#{to}=",send(to).to_s+text)
20
17
  else
21
18
  #simple form
@@ -33,7 +30,7 @@ module GetPomo
33
30
  end
34
31
 
35
32
  def complete?
36
- not msgid.nil? and not msgstr.nil?
33
+ (not msgid.nil? and not msgstr.nil?) or self.obsolete?
37
34
  end
38
35
 
39
36
  def fuzzy?
@@ -1,3 +1,3 @@
1
1
  module GetPomo
2
- VERSION = "0.7.1"
2
+ VERSION = "0.8.0"
3
3
  end
data/lib/get_pomo.rb CHANGED
@@ -3,10 +3,85 @@ module GetPomo
3
3
  autoload :VERSION, "get_pomo/version"
4
4
 
5
5
  extend self
6
-
7
- def self.unique_translations(translations)
8
- last_seen_at_index = {}
9
- translations.each_with_index {|translation,index|last_seen_at_index[translation.msgid]=index}
10
- last_seen_at_index.values.sort.map{|index| translations[index]}
6
+
7
+ # A message is unique if the combination of msgid and msgctxt is unique.
8
+ # An emtpy msgctxt does not mean the same thing as no msgctxt.
9
+ # If there are multiple msgid's without msgctxt we use the latest one. If at least one has a msgctxt we completly
10
+ # drop all msgids without msgctxt and do not even mark them for merging. Multiple msgid's with different msgctxt will
11
+ # be treated as unique translations each.
12
+ #
13
+ # @param merge [Bool] true merges references of non unique translations considering the rules above
14
+ def self.unique_translations(translations, merge = false)
15
+ seen_msgids = {}
16
+ # unique + merge logic
17
+ # hash with msgid's as keys.
18
+ # value is array if that msgid got no msgctxt
19
+ # value is hash if that msgid got a msgctxt
20
+ obsoletes = translations.select{|t| t.obsolete?}
21
+ translations.reject{ |t| t.obsolete?}.each_with_index do |translation, index|
22
+ key = translation.msgid
23
+ if seen_msgids.has_key?(key) # msgid not unique?
24
+ if seen_msgids[key].is_a?(Hash) # other translation with same msgid has a msgctxt?
25
+ if translation.msgctxt # this translation with same msgid has a msgctxt?, if not ignore it
26
+ if seen_msgids[key].has_key?(translation.msgctxt) # is it the same msgctxt?
27
+ seen_msgids[key][translation.msgctxt].push(index)
28
+ else
29
+ seen_msgids[key][translation.msgctxt] = [index]
30
+ end
31
+ end
32
+ else # other translation with same msgid had no msgctxt
33
+ if translation.msgctxt # if other translation has no msgctxt but this one has, just save this new one
34
+ seen_msgids[key] = {translation.msgctxt => [index]}
35
+ else # otherwise just push the new found translation for merge
36
+ seen_msgids[key].push(index)
37
+ end
38
+ end
39
+ else #msgid is unique so far
40
+ if translation.msgctxt # does the msgid got a msgctxt, if so add a nested hash with the msgctxt as key and an index_array as value
41
+ seen_msgids[key] = {translation.msgctxt => [index]}
42
+ else # if not, simply add an index array
43
+ seen_msgids[key] = [index]
44
+ end
45
+ end
46
+ end
47
+ trans_indexes = {} # maps unique translation ids as keys to mergeable ids as value array
48
+
49
+ seen_msgids.values.each do |value|
50
+ if value.is_a?(Hash)
51
+ value.values.each do |v|
52
+ id = v.pop
53
+ trans_indexes[id] = []
54
+ v.each do |index|
55
+ trans_indexes[id].push(index)
56
+ end
57
+ end
58
+ else
59
+ id = value.pop
60
+ trans_indexes[id] = []
61
+ value.each do |index|
62
+ trans_indexes[id].push(index)
63
+ end
64
+ end
65
+ end
66
+
67
+ trans = trans_indexes.keys.sort.map do |index|
68
+ if merge
69
+ t = translations[index]
70
+ trans_indexes[index].each do |id|
71
+ # sub starting \n# to \r\n as a split on \r\n should be safe
72
+ # (\n would match \\n in text, \r\n does not match \\r\\n)
73
+ # this is still monkey patching but should work fine
74
+ translations[id].comment.gsub(/\n#/, "\r\n#").split(/\r\n/).each do |com|
75
+ # prepend all references to the existing reference
76
+ t.comment.sub!("#:", com.chomp) if com.start_with?("#: ") && !t.comment.include?(com.sub("#:", ''))
77
+ end
78
+ end
79
+ t
80
+ else
81
+ translations[index]
82
+ end
83
+ end
84
+ trans.concat(obsoletes) unless obsoletes.nil?
85
+ trans
11
86
  end
12
87
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: get_pomo
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.1
4
+ version: 0.8.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michael Grosser
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-05-02 00:00:00.000000000 Z
11
+ date: 2014-08-18 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description:
14
14
  email: michael@grosser.it