diff options
author | Calum Lind <calumlind+deluge@gmail.com> | 2015-08-22 13:04:37 +0100 |
---|---|---|
committer | Calum Lind <calumlind+deluge@gmail.com> | 2015-08-22 14:26:56 +0100 |
commit | 24b71a400f741229f0623d2cd4f81068d3809716 (patch) | |
tree | 91a2380d50299c3a9265d0a3acecab217a9362fd /gen_web_gettext.py | |
parent | 7cc14baae3f83fa7857c90b7e71714bdd2c5986b (diff) | |
download | deluge-24b71a400f741229f0623d2cd4f81068d3809716.tar.gz deluge-24b71a400f741229f0623d2cd4f81068d3809716.tar.bz2 deluge-24b71a400f741229f0623d2cd4f81068d3809716.zip |
[WebUI] Improve the gen_web_gettext script
* Create a 'minified' gettext.js by removing comments from file and simplifying js code.
* Added creating the file to generate_pot.py, so it is not forgotten about.
Diffstat (limited to 'gen_web_gettext.py')
-rwxr-xr-x | gen_web_gettext.py | 96 |
1 files changed, 39 insertions, 57 deletions
diff --git a/gen_web_gettext.py b/gen_web_gettext.py index c591b01ad..cb8d5589b 100755 --- a/gen_web_gettext.py +++ b/gen_web_gettext.py @@ -12,61 +12,43 @@ import os import re -import sys -if len(sys.argv) != 2: - WEBUI_JS_DIR = 'deluge/ui/web/js/deluge-all' -else: - WEBUI_JS_DIR = os.path.abspath(sys.argv[1]) - -OUTPUT_FILE = os.path.join(os.path.dirname(WEBUI_JS_DIR), 'gettext.js') -STRING_RE = re.compile('_\\(\'(.*?)\'\\)') - -strings = {} -for root, dnames, files in os.walk(WEBUI_JS_DIR): - for filename in files: - if os.path.splitext(filename)[1] == '.js': - 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() - -gettext_tpl = """/*! - * 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); -} - -""" - -with open(OUTPUT_FILE, 'w') as fp: - 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()) - -print "Created %s" % OUTPUT_FILE +WEBUI_JS_DIR = 'deluge/ui/web/js/deluge-all' +# Enabling Debug adds file and line number as comments to the gettext file. +DEBUG = False + + +def create_gettext_js(js_dir): + string_re = re.compile('_\\(\'(.*?)\'\\)') + + strings = {} + for root, dnames, files in os.walk(js_dir): + for filename in files: + if os.path.splitext(filename)[1] == '.js': + 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() + + gettext_tpl = """GetText={maps:{},\ + add:function(string,translation) {this.maps[string]=translation},\ + get:function(string) {if (this.maps[string]) {string=this.maps[string]} return string}} + function _(string) {return GetText.get(string)}\ + """ + + gettext_file = os.path.join(os.path.dirname(js_dir), 'gettext.js') + with open(gettext_file, 'w') as fp: + fp.write(gettext_tpl) + for key in keys: + if DEBUG: + fp.write('\n// %s\n' % ', '.join(map(lambda x: '%s:%s' % x, strings[key]))) + fp.write("GetText.add('%(key)s','${escape(_(\"%(key)s\"))}')\n" % locals()) + +if __name__ == '__main__': + create_gettext_js(WEBUI_JS_DIR) + print "Created %s" % WEBUI_JS_DIR |