]> pere.pagekite.me Git - text-madewithcc.git/blob - fixup-migrate-gettext.rb
Start on script to unfuzzy po files after migration to XML as po4a source.
[text-madewithcc.git] / fixup-migrate-gettext.rb
1 #!/usr/bin/ruby
2 class PoStr
3 Sections = %w(Headers Source Translated)
4 attr_accessor :status, :src, :dest, :type, :location
5 def initialize(text)
6 section = 0
7 @status = ''
8 @src = []
9 @dest = []
10
11 text.split(/\n/).each do |lin|
12 # Header
13 if section == 0
14 if lin =~ /^#(.) ([^:]*)(?::(.*))?/
15 type = $1
16 field = $2
17 value = $3
18
19 @status = 'fuzzy' if type == ',' and field == 'fuzzy'
20 @type = value if type == '.' and field == 'type'
21 @location = {:file => field, :line => value} if type == ':'
22 elsif lin =~ /^msgid (".*")/
23 section += 1
24 @src << $1
25 else
26 boom(section, lin, text)
27 end
28
29 # Source text
30 elsif section == 1
31 if lin =~ /^".*"$/
32 @src << lin
33 elsif lin =~ /^msgstr (".*")/
34 section += 1
35 @dest << $1
36 else
37 boom(section, lin, text)
38 end
39
40 # Translated text
41 else
42 if lin =~ /^".*"$/
43 @dest << lin
44 else
45 boom(section, lin, text)
46 end
47 end
48 end
49 end
50
51 def boom(section, line, text)
52 raise RuntimeError, "Unexpected string in %s:\n %s\n\nFull text: %s" %
53 [Sections[section], line, text]
54 end
55
56 def fuzzy?
57 @status == 'fuzzy'
58 end
59 end
60
61 file = 'po/es/mwcc.po'
62 strings = File.open(file,'r').read.split(/\n\n/)[1..-1].map {|str| PoStr.new(str)}