~ubuntu-branches/ubuntu/natty/moin/natty-updates

« back to all changes in this revision

Viewing changes to MoinMoin/i18n/build_lang_py

  • Committer: Bazaar Package Importer
  • Author(s): Jonas Smedegaard
  • Date: 2008-06-22 21:17:13 UTC
  • mfrom: (0.9.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20080622211713-fpo2zrq3s5dfecxg
Tags: 1.7.0-3
Simplify /etc/moin/wikilist format: "USER URL" (drop unneeded middle
CONFIG_DIR that was wrongly advertised as DATA_DIR).  Make
moin-mass-migrate handle both formats and warn about deprecation of
the old one.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/usr/bin/env python
2
 
 
3
 
#
4
 
#  Copyright (C) 2000, 2003 Free Software Foundation.
5
 
#  Copyright (C) 2000, 2001 Eazel, Inc
6
 
#  Copyright (C) 2004 Bastian Blank <bblank@thinkmo.de>
7
 
#
8
 
#  Intltool is free software; you can redistribute it and/or
9
 
#  modify it under the terms of the GNU General Public License 
10
 
#  version 2 published by the Free Software Foundation.
11
 
#
12
 
#  Intltool is distributed in the hope that it will be useful,
13
 
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
 
#  General Public License for more details.
16
 
#
17
 
#  You should have received a copy of the GNU General Public License
18
 
#  along with this program; if not, write to the Free Software
19
 
#  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20
 
#
21
 
 
22
 
import os, re, string, sys, warnings
23
 
 
24
 
def print_translation(lang, file, a, b):
25
 
  if lang == 'en' and not b:
26
 
    b = a # no translation means use orig string
27
 
  if a and b:
28
 
    a = re.sub("'", r"\'", a)
29
 
    b = re.sub("'", r"\'", b)
30
 
    file.write("'''%s''':\n" % a)
31
 
    file.write("'''%s''',\n" % b)
32
 
 
33
 
def unescape_one_sequence(match):
34
 
  sequence = match.group(1)
35
 
  if sequence == r'\\':
36
 
    return r'\\'
37
 
  if sequence == r'\"':
38
 
    return r'"'
39
 
  if sequence == r'\n':
40
 
    return '\n'
41
 
 
42
 
  return sequence;
43
 
 
44
 
def unescape_po_string(string):
45
 
  return re.sub(r'(\\.)', unescape_one_sequence, string)
46
 
 
47
 
def do(lang, infile, outfile):
48
 
  nextfuzzy = False
49
 
  inmsgid = False
50
 
  inmsgstr = False
51
 
  msgid = ""
52
 
  msgstr = ""
53
 
 
54
 
  header = {}
55
 
 
56
 
  for i in infile.readlines():
57
 
    i = string.rstrip(i)
58
 
 
59
 
    if re.match(r'^msgstr', i):
60
 
      inmsgstr = True
61
 
 
62
 
    if inmsgstr:
63
 
      match = re.match(r'"(\S+):\s+(.*)\\n"', i)
64
 
      if match:
65
 
        header[match.group(1)] = match.group(2)
66
 
      if re.match('^\s*$', i):
67
 
        break
68
 
 
69
 
  inmsgstr = False
70
 
 
71
 
  match = re.match(r'.*charset=([-a-z0-9]+)', header['Content-Type'])
72
 
  if not match:
73
 
    raise ValueError, "Please make the charset lowercase."
74
 
  encoding = match.group(1)
75
 
 
76
 
  try:
77
 
    direction = header['X-Direction']
78
 
  except:
79
 
    warnings.warn("%s.po don't provide information about the direction, use default ('ltr')" % lang, stacklevel=2)
80
 
    direction = 'ltr'
81
 
 
82
 
  temp = {
83
 
    'lang': lang,
84
 
    'language': header['X-Language'],
85
 
    'elanguage': header['X-Language-in-English'],
86
 
    'encoding': encoding,
87
 
    'maintainer': header['Last-Translator'],
88
 
    'direction': direction,
89
 
    'wikimarkup': header.get('X-HasWikiMarkup', False),
90
 
  }
91
 
 
92
 
  outfile.write('''# -*- coding: %(encoding)s -*-
93
 
# Text translations for %(language)s (%(lang)s).
94
 
# Automatically generated - DO NOT EDIT, edit %(lang)s.po instead!
95
 
meta = {
96
 
  'language': """%(language)s""",
97
 
  'elanguage': """%(elanguage)s""",
98
 
  'maintainer': """%(maintainer)s""",
99
 
  'encoding': '%(encoding)s',
100
 
  'direction': '%(direction)s',
101
 
  'wikimarkup': %(wikimarkup)s,
102
 
}
103
 
text = {
104
 
''' % temp)
105
 
 
106
 
  infile.seek(0)
107
 
 
108
 
  for i in infile.readlines():
109
 
    if re.match(r'^#, fuzzy', i):
110
 
      nextfuzzy = True
111
 
 
112
 
    match = re.match(r'^msgid "((\\.|[^\\])*)"', i)
113
 
    if match:
114
 
      if inmsgstr:
115
 
        print_translation(lang, outfile, msgid, msgstr)
116
 
      msgid = "";
117
 
      msgstr = "";
118
 
 
119
 
      if nextfuzzy:
120
 
        inmsgid = False
121
 
      else:
122
 
        msgid = unescape_po_string(match.group(1));
123
 
        inmsgid = True
124
 
      inmsgstr = False
125
 
      nextfuzzy = False
126
 
 
127
 
    match = re.match(r'^msgstr "((\\.|[^\\])*)"', i)
128
 
    if match:
129
 
      msgstr = unescape_po_string(match.group(1));
130
 
      inmsgstr = True
131
 
      inmsgid = False
132
 
 
133
 
    match = re.match(r'^"((\\.|[^\\])*)"', i)
134
 
    if match:
135
 
      if inmsgid:
136
 
        msgid += unescape_po_string(match.group(1))
137
 
      if inmsgstr:
138
 
        msgstr += unescape_po_string(match.group(1))
139
 
 
140
 
  if inmsgstr:
141
 
    print_translation(lang, outfile, msgid, msgstr)
142
 
 
143
 
  outfile.write('}\n')
144
 
 
145
 
def run():
146
 
    for i in sys.argv[1:]:
147
 
      lang = i
148
 
      infilename = "%s.po" % lang
149
 
      outfilename = "%s.py" % lang
150
 
      infile = file(infilename, "r")
151
 
      outfile = file(outfilename, "w")
152
 
      try:
153
 
        do(lang, infile, outfile)
154
 
      except:
155
 
        os.unlink(outfilename)
156
 
        raise
157
 
 
158
 
if __name__ == "__main__":
159
 
    run()
160