2009-10-24 07:14:11 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# vim: autoindent shiftwidth=4 expandtab textwidth=80 tabstop=4 softtabstop=4
|
|
|
|
|
|
|
|
###############################################################################
|
|
|
|
# OpenLP - Open Source Lyrics Projection #
|
|
|
|
# --------------------------------------------------------------------------- #
|
2009-12-31 12:52:01 +00:00
|
|
|
# Copyright (c) 2008-2010 Raoul Snyman #
|
|
|
|
# Portions copyright (c) 2008-2010 Martin Thompson, Tim Bentley, Carsten #
|
2009-10-24 07:14:11 +00:00
|
|
|
# Tinggaard, Jon Tibble, Jonathan Corwin, Maikel Stuivenberg, Scott Guerrieri #
|
|
|
|
# --------------------------------------------------------------------------- #
|
|
|
|
# This program is free software; you can redistribute it and/or modify it #
|
|
|
|
# under the terms of the GNU General Public License as published by the Free #
|
|
|
|
# Software Foundation; version 2 of the License. #
|
|
|
|
# #
|
|
|
|
# This program 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., 59 #
|
|
|
|
# Temple Place, Suite 330, Boston, MA 02111-1307 USA #
|
|
|
|
###############################################################################
|
|
|
|
|
|
|
|
import os
|
2010-03-09 06:07:42 +00:00
|
|
|
from cgi import escape
|
2009-11-27 10:50:39 +00:00
|
|
|
from ast import parse, NodeVisitor, Str
|
2009-10-24 07:14:11 +00:00
|
|
|
|
|
|
|
ts_file = u"""<?xml version="1.0" encoding="utf-8"?>
|
|
|
|
<!DOCTYPE TS>
|
|
|
|
<TS version="1.1">
|
|
|
|
%s
|
|
|
|
</TS>
|
|
|
|
"""
|
|
|
|
ts_context = u""" <context>
|
|
|
|
<name>%s</name>
|
|
|
|
%s </context>
|
|
|
|
"""
|
|
|
|
ts_message = u""" <message>
|
|
|
|
<location filename="%s" line="%d"/>
|
|
|
|
<source>%s</source>
|
|
|
|
<translation type="unfinished"></translation>
|
|
|
|
</message>
|
|
|
|
"""
|
|
|
|
|
2009-11-27 10:50:39 +00:00
|
|
|
class StringExtractor(NodeVisitor):
|
|
|
|
|
2010-03-09 06:07:42 +00:00
|
|
|
def __init__(self, strings, filename, base_path):
|
|
|
|
self.base_path = base_path
|
2009-11-27 10:50:39 +00:00
|
|
|
self.filename = filename
|
2009-11-27 14:41:13 +00:00
|
|
|
self.strings = strings
|
2009-11-27 10:50:39 +00:00
|
|
|
self.classname = 'unknown'
|
|
|
|
|
|
|
|
def visit_ClassDef(self, node):
|
|
|
|
self.classname = node.name
|
|
|
|
self.generic_visit(node)
|
|
|
|
|
|
|
|
def visit_Call(self, node):
|
|
|
|
if hasattr(node.func, 'attr') and node.func.attr == 'trUtf8' and isinstance(node.args[0], Str):
|
|
|
|
string = node.args[0].s
|
|
|
|
key = '%s-%s' % (self.classname, string)
|
2010-03-09 06:07:42 +00:00
|
|
|
self.strings[key] = [self.classname, self.filename[len(self.base_path) + 1:], node.lineno, escape(string)]
|
2009-11-27 10:50:39 +00:00
|
|
|
self.generic_visit(node)
|
2009-10-24 07:14:11 +00:00
|
|
|
|
2010-03-09 06:07:42 +00:00
|
|
|
def parse_file(base_path, filename, strings):
|
2009-10-24 07:14:11 +00:00
|
|
|
file = open(filename, u'r')
|
2009-11-27 10:50:39 +00:00
|
|
|
try:
|
|
|
|
ast = parse(file.read())
|
|
|
|
except SyntaxError, e:
|
2009-11-27 10:53:39 +00:00
|
|
|
print "Unable to parse %s: %s" % (filename, e)
|
2009-11-27 10:50:39 +00:00
|
|
|
return
|
2009-10-24 07:14:11 +00:00
|
|
|
file.close()
|
|
|
|
|
2010-03-09 06:07:42 +00:00
|
|
|
StringExtractor(strings, filename, base_path).visit(ast)
|
2009-11-27 10:50:39 +00:00
|
|
|
|
2009-11-27 14:41:13 +00:00
|
|
|
def write_file(filename, strings):
|
2009-10-24 07:14:11 +00:00
|
|
|
translation_file = u''
|
|
|
|
translation_contexts = []
|
|
|
|
translation_messages = []
|
|
|
|
class_name = strings[strings.keys()[0]][0]
|
|
|
|
current_context = u''
|
|
|
|
for key, translation in strings.iteritems():
|
|
|
|
if class_name != translation[0]:
|
|
|
|
current_context = ts_context % (class_name, u''.join(translation_messages))
|
|
|
|
translation_contexts.append(current_context)
|
|
|
|
translation_messages = []
|
|
|
|
class_name = translation[0]
|
|
|
|
translation_messages.append(ts_message % (translation[1], translation[2], translation[3]))
|
|
|
|
current_context = ts_context % (class_name, u''.join(translation_messages))
|
|
|
|
translation_contexts.append(current_context)
|
|
|
|
translation_file = ts_file % (u''.join(translation_contexts))
|
|
|
|
file = open(filename, u'w')
|
2009-11-27 10:53:39 +00:00
|
|
|
file.write(translation_file.encode('utf8'))
|
2009-10-24 07:14:11 +00:00
|
|
|
file.close()
|
|
|
|
|
|
|
|
def main():
|
2009-11-27 14:41:13 +00:00
|
|
|
strings = {}
|
2010-03-09 06:07:42 +00:00
|
|
|
start_dir = os.path.abspath(u'..')
|
2009-10-24 07:14:11 +00:00
|
|
|
for root, dirs, files in os.walk(start_dir):
|
|
|
|
for file in files:
|
2010-03-15 18:40:52 +00:00
|
|
|
if file.startswith(u'hook-') or file.startswith(u'test_'):
|
|
|
|
continue
|
2009-10-24 07:14:11 +00:00
|
|
|
if file.endswith(u'.py'):
|
|
|
|
print u'Parsing "%s"' % file
|
2010-03-09 06:07:42 +00:00
|
|
|
parse_file(start_dir, os.path.join(root, file), strings)
|
2009-10-24 07:14:11 +00:00
|
|
|
print u'Generating TS file...',
|
2010-03-09 06:07:42 +00:00
|
|
|
write_file(os.path.join(start_dir, u'resources', u'i18n', u'openlp_en.ts'), strings)
|
2009-10-24 07:14:11 +00:00
|
|
|
print u'done.'
|
|
|
|
|
|
|
|
if __name__ == u'__main__':
|
2010-03-09 06:07:42 +00:00
|
|
|
if os.path.split(os.path.abspath(u'.'))[1] != u'scripts':
|
|
|
|
print u'You need to run this script from the scripts directory.'
|
|
|
|
else:
|
|
|
|
main()
|