2009-04-07 17:54:57 +00:00
|
|
|
#!/usr/bin/python
|
2009-07-22 23:53:30 +00:00
|
|
|
"""
|
|
|
|
Script to go through the javascript files and dynamically generate gettext.js
|
|
|
|
"""
|
2009-04-07 17:54:57 +00:00
|
|
|
import os
|
|
|
|
import re
|
|
|
|
|
2014-02-17 16:51:02 +00:00
|
|
|
output_file = "js/gettext.js"
|
2009-04-07 17:54:57 +00:00
|
|
|
string_re = re.compile('_\\(\'(.*?)\'\\)')
|
|
|
|
strings = {}
|
|
|
|
|
2012-11-23 00:53:49 +00:00
|
|
|
|
2010-01-20 20:37:14 +00:00
|
|
|
gettext_tpl = """## -*- coding: utf-8 -*-
|
|
|
|
/*
|
2009-04-07 17:54:57 +00:00
|
|
|
* Script: gettext.js
|
|
|
|
* A script file that is run through the template renderer in order for
|
|
|
|
* translated strings to be used.
|
|
|
|
*
|
|
|
|
* Copyright:
|
2010-01-20 20:37:14 +00:00
|
|
|
* (c) 2009 Damien Churchill <damoxc@gmail.com>
|
2009-04-07 17:54:57 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
GetText = {
|
|
|
|
maps: {},
|
|
|
|
add: function(string, translation) {
|
|
|
|
this.maps[string] = translation;
|
|
|
|
},
|
|
|
|
get: function(string) {
|
|
|
|
if (this.maps[string]) {
|
|
|
|
return this.maps[string];
|
|
|
|
} else {
|
|
|
|
return string;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-04-21 09:08:05 +00:00
|
|
|
function _(string) {
|
|
|
|
return GetText.get(string);
|
|
|
|
}
|
2009-04-07 17:54:57 +00:00
|
|
|
|
|
|
|
"""
|
|
|
|
|
2012-01-09 21:58:20 +00:00
|
|
|
for root, dnames, files in os.walk('js/deluge-all'):
|
|
|
|
for filename in files:
|
|
|
|
if filename.startswith('.'):
|
|
|
|
continue
|
|
|
|
if not filename.endswith('.js'):
|
|
|
|
continue
|
|
|
|
|
|
|
|
for lineno, line in enumerate(open(os.path.join(root, filename))):
|
|
|
|
for match in string_re.finditer(line):
|
|
|
|
string = match.group(1)
|
|
|
|
locations = strings.get(string, [])
|
|
|
|
locations.append((os.path.basename(filename), lineno + 1))
|
|
|
|
strings[string] = locations
|
|
|
|
|
|
|
|
|
2009-04-07 17:54:57 +00:00
|
|
|
keys = strings.keys()
|
|
|
|
keys.sort()
|
|
|
|
|
2012-11-23 00:53:49 +00:00
|
|
|
fp = open(output_file, 'w')
|
2009-04-07 17:54:57 +00:00
|
|
|
fp.write(gettext_tpl)
|
|
|
|
for key in keys:
|
|
|
|
fp.write('// %s\n' % ', '.join(map(lambda x: '%s:%s' % x, strings[key])))
|
2009-10-28 13:24:37 +00:00
|
|
|
fp.write("GetText.add('%(key)s', '${escape(_(\"%(key)s\"))}')\n\n" % locals())
|
2012-11-23 00:53:49 +00:00
|
|
|
fp.close()
|