view MoinMoin/i18n/build_lang_py @ 0:77665d8e2254

tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0 (automatically generated log message) imported from: moin--main--1.5--base-0
author Thomas Waldmann <tw-public@gmx.de>
date Thu, 22 Sep 2005 15:09:50 +0000
parents
children 77499985c79f
line wrap: on
line source
#!/usr/bin/env python

#
#  Copyright (C) 2000, 2003 Free Software Foundation.
#  Copyright (C) 2000, 2001 Eazel, Inc
#  Copyright (C) 2004 Bastian Blank <bblank@thinkmo.de>
#
#  Intltool is free software; you can redistribute it and/or
#  modify it under the terms of the GNU General Public License 
#  version 2 published by the Free Software Foundation.
#
#  Intltool is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
#  General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#

import os, re, string, sys, warnings

def print_translation(lang, file, a, b):
  if lang == 'en' and not b:
    b = a # no translation means use orig string
  if a and b:
    a = re.sub("'", r"\'", a)
    b = re.sub("'", r"\'", b)
    file.write("'''%s''':\n" % a)
    file.write("'''%s''',\n" % b)

def unescape_one_sequence(match):
  sequence = match.group(1)
  if sequence == r'\\':
    return r'\\'
  if sequence == r'\"':
    return r'"'
  if sequence == r'\n':
    return '\n'

  return sequence;

def unescape_po_string(string):
  return re.sub(r'(\\.)', unescape_one_sequence, string)

def do(lang, infile, outfile):
  nextfuzzy = False
  inmsgid = False
  inmsgstr = False
  msgid = ""
  msgstr = ""

  header = {}

  for i in infile.readlines():
    i = string.rstrip(i)

    if re.match(r'^msgstr', i):
      inmsgstr = True

    if inmsgstr:
      match = re.match(r'"(\S+):\s+(.*)\\n"', i)
      if match:
        header[match.group(1)] = match.group(2)
      if re.match('^\s*$', i):
        break

  inmsgstr = False

  match = re.match(r'.*charset=([-a-z0-9]+)', header['Content-Type'])
  if not match:
    raise ValueError, "Please make the charset lowercase."
  encoding = match.group(1)

  try:
    direction = header['X-Direction']
  except:
    warnings.warn("%s.po don't provide information about the direction, use default ('ltr')" % lang, stacklevel=2)
    direction = 'ltr'

  temp = {
    'lang': lang,
    'language': header['X-Language'],
    'elanguage': header['X-Language-in-English'],
    'encoding': encoding,
    'maintainer': header['Last-Translator'],
    'direction': direction,
    'wikimarkup': header.get('X-HasWikiMarkup', False),
  }

  outfile.write('''# -*- coding: %(encoding)s -*-
# Text translations for %(language)s (%(lang)s).
# Automatically generated - DO NOT EDIT, edit %(lang)s.po instead!
meta = {
  'language': '%(language)s',
  'elanguage': '%(elanguage)s',
  'maintainer': '%(maintainer)s',
  'encoding': '%(encoding)s',
  'direction': '%(direction)s',
  'wikimarkup': %(wikimarkup)s,
}
text = {
''' % temp)

  infile.seek(0)

  for i in infile.readlines():
    if re.match(r'^#, fuzzy', i):
      nextfuzzy = True

    match = re.match(r'^msgid "((\\.|[^\\])*)"', i)
    if match:
      if inmsgstr:
        print_translation(lang, outfile, msgid, msgstr)
      msgid = "";
      msgstr = "";

      if nextfuzzy:
        inmsgid = False
      else:
        msgid = unescape_po_string(match.group(1));
        inmsgid = True
      inmsgstr = False
      nextfuzzy = False

    match = re.match(r'^msgstr "((\\.|[^\\])*)"', i)
    if match:
      msgstr = unescape_po_string(match.group(1));
      inmsgstr = True
      inmsgid = False

    match = re.match(r'^"((\\.|[^\\])*)"', i)
    if match:
      if inmsgid:
        msgid += unescape_po_string(match.group(1))
      if inmsgstr:
        msgstr += unescape_po_string(match.group(1))

  if inmsgstr:
    print_translation(lang, outfile, msgid, msgstr)

  outfile.write('}\n')

for i in sys.argv[1:]:
  lang = i
  infilename = "%s.po" % lang
  outfilename = "%s.py" % lang
  infile = file(infilename, "r")
  outfile = file(outfilename, "w")
  try:
    do(lang, infile, outfile)
  except:
    os.unlink(outfilename)
    raise