deluge/gen_web_gettext.py

67 lines
1.6 KiB
Python
Raw Normal View History

#!/usr/bin/python
2009-07-22 23:53:30 +00:00
"""
Script to go through the javascript files and dynamically generate gettext.js
"""
import os
import re
2014-02-17 16:51:02 +00:00
output_file = "js/gettext.js"
string_re = re.compile('_\\(\'(.*?)\'\\)')
strings = {}
gettext_tpl = """## -*- coding: utf-8 -*-
/*
* Script: gettext.js
* A script file that is run through the template renderer in order for
* translated strings to be used.
*
* Copyright:
* (c) 2009 Damien Churchill <damoxc@gmail.com>
*/
GetText = {
maps: {},
add: function(string, translation) {
this.maps[string] = translation;
},
get: function(string) {
if (this.maps[string]) {
return this.maps[string];
} else {
return string;
}
}
}
function _(string) {
return GetText.get(string);
}
"""
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
keys = strings.keys()
keys.sort()
fp = open(output_file, 'w')
fp.write(gettext_tpl)
for key in keys:
fp.write('// %s\n' % ', '.join(map(lambda x: '%s:%s' % x, strings[key])))
fp.write("GetText.add('%(key)s', '${escape(_(\"%(key)s\"))}')\n\n" % locals())
fp.close()