aboutsummaryrefslogtreecommitdiffstats
path: root/docs/stylesheets/transconverter.py
blob: 48b3ea63642c1f0d9a12b3022f6cbe0694c3950c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import errno, glob, polib, re, os, getopt, sys
from time import strftime

def usage():
    print '\nUsage: python %s [OPTION]' %os.path.basename(sys.argv[0])
    print '       extract existing translations from xsl file'
    print 'Options: -h, --help                              : usage'
    sys.exit(2)
try:
    opts, args = getopt.getopt(sys.argv[1:], "h:", ["help"])
except getopt.GetoptError:
    usage() # print help information and exit

directory='.'
for o in opts:
    if o in ("-h", "--help"):
        usage()

# Find all XSL files
files = []
for rootdir, dirnames, filenames in os.walk(directory):
    files.extend(glob.glob(rootdir + "/*.xsl"))

# Define Templates and po directory name
translationtemplate='l:l10n\ (.*?)<\/l:l10n'
tpattern=re.compile(translationtemplate,re.DOTALL)
entemplate='language=\"en\">\n(.*?)<\/l:l10n'
enpattern=re.compile(entemplate,re.DOTALL)
langtemplate='language=\"(.*?)\"'
lpattern=re.compile(langtemplate,re.DOTALL)
messagetemplate='<l:gentext key=\"(.*?)\"\/>'
mpattern=re.compile(messagetemplate,re.DOTALL)
podir = 'po'

pocreationtime = strftime('%Y-%m-%d %H:%M%z')

for langfile in files:
  langfiledir = langfile.replace('.xsl', '')
  langfilename = langfiledir.rpartition('/')[2]
  # Create localization directories if needed
  try:
    os.makedirs(podir)
  except OSError, e:
    if e.errno != errno.EEXIST:
        raise
  #open xsl file
  text = open(langfile,"r").read()

  # Find all English messages, determine msgctxts and msgids
  messages = {}
  for enblock in enpattern.findall(text):
    for enmessage in mpattern.findall(enblock):
      msgkey, msgid = enmessage.split('\" text=\"')
      messages[msgkey] = msgid
    
  # Parse contents and add them to PO
  for tblock in tpattern.findall(text):
    lang_code = lpattern.search(tblock).group(1)
    print lang_code
    pofilename = podir + '/' + lang_code + '.po'
    if not os.path.isfile(pofilename):
      # Create PO file
      po = polib.POFile()
      po.metadata = {
	'Project-Id-Version': 'Mageia XSL files translation',
	'Report-Msgid-Bugs-To': 'mageia-i18n@mageia.org',
	'POT-Creation-Date': pocreationtime,
	'PO-Revision-Date': pocreationtime,
	'Last-Translator': 'Duffy Duck <d_duck@nowhere.net>',
	'Language-Team': lang_code + ' <mageia-' + lang_code + '@ml.mageia.org>',
	'MIME-Version': '1.0',
	'Content-Type': 'text/plain; charset=UTF-8',
	'Content-Transfer-Encoding': '8bit',
	}
      po.save(pofilename)
    po = polib.pofile(pofilename, check_for_duplicates=True)
    if lang_code == 'en':
      for message in mpattern.findall(tblock):
	msgkey, msg_id = message.split('\" text=\"')
	poentry = polib.POEntry(
	  msgctxt = msgkey,
	  msgid = msg_id.decode('utf-8'),
	  msgstr = msg_id.decode('utf-8'),
	  occurrences=[(langfile,'')]
	)
	try:
	  po.append(poentry)
	except ValueError:
	  print 'The entry already exists, skipping it'
    else:
      for message in mpattern.findall(tblock):
	msgkey, msg_id = message.split('\" text=\"')
	poentry = polib.POEntry(
	  msgctxt = msgkey,
	  msgid = messages[msgkey].decode('utf-8'),
	  msgstr = msg_id.decode('utf-8') if msg_id != messages[msgkey] else '',
	  occurrences=[(langfile,'')]
	)
	try:
	  po.append(poentry)
	except ValueError:
	  print 'The entry already exists, skipping it'
    po.save(pofilename)