export lang as po file: bugfix: msgid must be unique

bzr revid: christophe@tinyerp.com-20080822093914-bk6hahvstngwkgxj
This commit is contained in:
Christophe Simonis 2008-08-22 11:39:14 +02:00
parent 1f36721791
commit 78fce194ca
1 changed files with 63 additions and 36 deletions

View File

@ -80,6 +80,7 @@ class TinyPoFile(object):
self.buffer.seek(0)
self.lines = self.buffer.readlines()
self.first = True
self.tnrs= []
return self
def next(self):
@ -88,41 +89,51 @@ class TinyPoFile(object):
.replace('\\"', "\"")
type = name = res_id = source = trad = None
line = None
while not line:
if 0 == len(self.lines):
raise StopIteration()
line = self.lines.pop(0).strip()
while line.startswith('#'):
if line.startswith('#:'):
type, name, res_id = line[2:].strip().split(':')
line = self.lines.pop(0).strip()
if not line.startswith('msgid'):
raise Exception("malformed file")
source = line[7:-1]
line = self.lines.pop(0).strip()
if not source and self.first:
# if the source is "" and it's the first msgid, it's the special
# msgstr with the informations about the traduction and the
# traductor; we skip it
while line:
if self.tnrs:
type, name, res_id, source, trad = self.tnrs.pop(0)
else:
tmp_tnrs = []
line = None
while not line:
if 0 == len(self.lines):
raise StopIteration()
line = self.lines.pop(0).strip()
return next()
while not line.startswith('msgstr'):
if not line:
raise Exception('malformed file')
source += unquote(line)
while line.startswith('#'):
if line.startswith('#:'):
tmp_tnrs.append( line[2:].strip().split(':') )
line = self.lines.pop(0).strip()
if not line.startswith('msgid'):
raise Exception("malformed file")
source = line[7:-1]
line = self.lines.pop(0).strip()
if not source and self.first:
# if the source is "" and it's the first msgid, it's the special
# msgstr with the informations about the traduction and the
# traductor; we skip it
self.tnrs = []
while line:
line = self.lines.pop(0).strip()
return next()
while not line.startswith('msgstr'):
if not line:
raise Exception('malformed file')
source += unquote(line)
line = self.lines.pop(0).strip()
trad = line[8:-1]
line = self.lines.pop(0).strip()
while line:
trad += unquote(line)
trad = line[8:-1]
line = self.lines.pop(0).strip()
while line:
trad += unquote(line)
line = self.lines.pop(0).strip()
if tmp_tnrs:
type, name, res_id = tmp_tnrs.pop(0)
for t, n, r in tmp_tnrs:
self.tnrs.append((t, n, r, source, trad))
self.first = False
return type, name, res_id, source, trad
@ -154,18 +165,23 @@ class TinyPoFile(object):
}
)
def write(self, module, type, name, res_id, source, trad):
def write(self, modules, tnrs, source, trad):
def quote(str):
return '"%s"' % str.replace('"','\\"') \
.replace('\n', '\\n"\n"')
self.buffer.write("#. module: %s\n" \
plurial = len(modules) > 1 and 's' or ''
self.buffer.write("#. module%s: %s\n" \
"#, python-format\n" \
"#: %s:%s:%s\n" \
"msgid %s\n" \
% (plurial, ', '.join(modules)))
for type, name, res_id in tnrs:
self.buffer.write("#: %s:%s:%s\n" % (type, name, str(res_id)))
self.buffer.write("msgid %s\n" \
"msgstr %s\n\n" \
% (module, type, name, str(res_id), quote(source), quote(trad))
)
% (quote(source), quote(trad))
)
@ -182,8 +198,18 @@ def trans_export(lang, modules, buffer, format, dbname=None):
rows.pop(0)
writer = tools.TinyPoFile(buffer)
writer.write_infos(modules)
# we now group the translations by source. That means one translation per source.
grouped_rows = {}
for module, type, name, res_id, src, trad in rows:
writer.write(module, type, name, res_id, src, trad)
row = grouped_rows.setdefault(src, {})
row.setdefault('modules', set()).add(module)
row.setdefault('translation', trad)
row.setdefault('tnrs', []).append((type, name, res_id))
for src, row in grouped_rows.items():
writer.write(row['modules'], row['tnrs'], src, row['translation'])
elif format == 'tgz':
rows.pop(0)
rows_by_module = {}
@ -210,6 +236,7 @@ def trans_export(lang, modules, buffer, format, dbname=None):
if newlang:
lang = 'en_US'
trans = trans_generate(lang, modules, dbname)
modules = set([t[0] for t in trans[1:]])
_process(format, modules, trans, buffer, lang, newlang)
del trans