mirror of
https://github.com/codex-storage/deluge.git
synced 2025-01-11 03:55:43 +00:00
[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.
This commit is contained in:
parent
7cc14baae3
commit
24b71a400f
File diff suppressed because it is too large
Load Diff
@ -12,61 +12,43 @@
|
|||||||
|
|
||||||
import os
|
import os
|
||||||
import re
|
import re
|
||||||
import sys
|
|
||||||
|
|
||||||
if len(sys.argv) != 2:
|
WEBUI_JS_DIR = 'deluge/ui/web/js/deluge-all'
|
||||||
WEBUI_JS_DIR = 'deluge/ui/web/js/deluge-all'
|
# Enabling Debug adds file and line number as comments to the gettext file.
|
||||||
else:
|
DEBUG = False
|
||||||
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 = {}
|
def create_gettext_js(js_dir):
|
||||||
for root, dnames, files in os.walk(WEBUI_JS_DIR):
|
string_re = re.compile('_\\(\'(.*?)\'\\)')
|
||||||
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()
|
strings = {}
|
||||||
keys.sort()
|
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
|
||||||
|
|
||||||
gettext_tpl = """/*!
|
keys = strings.keys()
|
||||||
* Script: gettext.js
|
keys.sort()
|
||||||
* 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 = {
|
gettext_tpl = """GetText={maps:{},\
|
||||||
maps: {},
|
add:function(string,translation) {this.maps[string]=translation},\
|
||||||
add: function(string, translation) {
|
get:function(string) {if (this.maps[string]) {string=this.maps[string]} return string}}
|
||||||
this.maps[string] = translation;
|
function _(string) {return GetText.get(string)}\
|
||||||
},
|
"""
|
||||||
get: function(string) {
|
|
||||||
if (this.maps[string]) {
|
|
||||||
return this.maps[string];
|
|
||||||
} else {
|
|
||||||
return string;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function _(string) {
|
gettext_file = os.path.join(os.path.dirname(js_dir), 'gettext.js')
|
||||||
return GetText.get(string);
|
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)
|
||||||
with open(OUTPUT_FILE, 'w') as fp:
|
print "Created %s" % WEBUI_JS_DIR
|
||||||
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
|
|
||||||
|
@ -16,6 +16,7 @@ import re
|
|||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from subprocess import call
|
from subprocess import call
|
||||||
|
|
||||||
|
from gen_web_gettext import create_gettext_js
|
||||||
from version import get_version
|
from version import get_version
|
||||||
|
|
||||||
# Paths to exclude
|
# Paths to exclude
|
||||||
@ -103,4 +104,7 @@ for filepath in to_translate:
|
|||||||
if filepath.endswith(".h"):
|
if filepath.endswith(".h"):
|
||||||
os.remove(filepath)
|
os.remove(filepath)
|
||||||
|
|
||||||
print "Created %s" % POT_FILEPATH
|
# Update web js gettext
|
||||||
|
create_gettext_js(WEBUI_JS_DIR)
|
||||||
|
|
||||||
|
print "Created %s and updated gettext.js" % POT_FILEPATH
|
||||||
|
Loading…
x
Reference in New Issue
Block a user