Minor updates to the translation scripts

* General cleanup of code.
 * Add commandline folder option to js gettext script.
 * Include webui render html files to pot template creation.
This commit is contained in:
Calum Lind 2015-08-20 18:51:09 +01:00
commit 90db2b4c5c
3 changed files with 56 additions and 49 deletions

View file

@ -23,9 +23,11 @@ from deluge.common import path_join, utf8_encoded
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
# Dummy tranlation dict so Torrent states text is available for Translators # Dummy translation dicts so Torrent and Tracker states are available for Translators.
# All entries in deluge.common.TORRENT_STATE should be here. It does not need importing #
# as the string matches the translation text so using the _() function is enough. # All entries in deluge.common.TORRENT_STATE should be added here.
#
# No need to import these, just simply use the `_()` function around a status variable.
def _(message): def _(message):
return message return message
STATE_TRANSLATION = { STATE_TRANSLATION = {

View file

@ -12,20 +12,35 @@
import os import os
import re 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('_\\(\'(.*?)\'\\)')
output_file = "js/gettext.js"
string_re = re.compile('_\\(\'(.*?)\'\\)')
strings = {} 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 = """## -*- coding: utf-8 -*- gettext_tpl = """/*!
/*
* Script: gettext.js * Script: gettext.js
* A script file that is run through the template renderer in order for * A script file that is run through the template renderer in order for translated strings to be used.
* translated strings to be used.
* *
* Copyright: * Copyright (c) 2009 Damien Churchill <damoxc@gmail.com>
* (c) 2009 Damien Churchill <damoxc@gmail.com>
*/ */
GetText = { GetText = {
@ -48,27 +63,10 @@ function _(string) {
""" """
for root, dnames, files in os.walk('js/deluge-all'): with open(OUTPUT_FILE, 'w') as fp:
for filename in files: fp.write(gettext_tpl)
if filename.startswith('.'): for key in keys:
continue fp.write('// %s\n' % ', '.join(map(lambda x: '%s:%s' % x, strings[key])))
if not filename.endswith('.js'): fp.write("GetText.add('%(key)s', '${escape(_(\"%(key)s\"))}')\n\n" % locals())
continue
for lineno, line in enumerate(open(os.path.join(root, filename))): print "Created %s" % OUTPUT_FILE
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()

View file

@ -24,18 +24,19 @@ EXCLUSIONS = [
"deluge/i18n", "deluge/i18n",
"deluge/tests", "deluge/tests",
] ]
webui_js_dir = "deluge/ui/web/js/deluge-all" WEBUI_JS_DIR = "deluge/ui/web/js/deluge-all"
infiles_list = "infiles.list" WEBUI_RENDER_DIR = "deluge/ui/web/render"
pot_filepath = os.path.join("deluge", "i18n", "deluge.pot") INFILES_LIST = "infiles.list"
POT_FILEPATH = os.path.join("deluge", "i18n", "deluge.pot")
re_exc_plugin_build = re.compile("deluge\/plugins\/.*\/build") RE_EXC_PLUGIN_BUILD = re.compile("deluge\/plugins\/.*\/build")
xgettext_cmd = [ xgettext_cmd = [
"xgettext", "xgettext",
"--from-code=UTF-8", "--from-code=UTF-8",
"-kN_:1", "-kN_:1",
"-f%s" % infiles_list, "-f%s" % INFILES_LIST,
"-o%s" % pot_filepath, "-o%s" % POT_FILEPATH,
"--package-name=%s" % "Deluge", "--package-name=%s" % "Deluge",
"--copyright-holder=%s" % "Deluge Team", "--copyright-holder=%s" % "Deluge Team",
"--package-version=%s" % get_version(prefix='deluge-', suffix='.dev0'), "--package-version=%s" % get_version(prefix='deluge-', suffix='.dev0'),
@ -45,7 +46,7 @@ xgettext_cmd = [
to_translate = [] to_translate = []
for (dirpath, dirnames, filenames) in os.walk("deluge"): for (dirpath, dirnames, filenames) in os.walk("deluge"):
for filename in filenames: for filename in filenames:
if dirpath not in EXCLUSIONS and not re_exc_plugin_build.match(dirpath): if dirpath not in EXCLUSIONS and not RE_EXC_PLUGIN_BUILD.match(dirpath):
filepath = os.path.join(dirpath, filename) filepath = os.path.join(dirpath, filename)
if os.path.splitext(filepath)[1] in (".py", ".glade"): if os.path.splitext(filepath)[1] in (".py", ".glade"):
to_translate.append(filepath) to_translate.append(filepath)
@ -56,7 +57,7 @@ for (dirpath, dirnames, filenames) in os.walk("deluge"):
call(["intltool-extract", "--quiet", "--type=gettext/glade", filepath]) call(["intltool-extract", "--quiet", "--type=gettext/glade", filepath])
to_translate.append(filepath + ".h") to_translate.append(filepath + ".h")
with open(infiles_list, "wb") as f: with open(INFILES_LIST, "wb") as f:
for line in to_translate: for line in to_translate:
f.write(line + "\n") f.write(line + "\n")
@ -65,24 +66,30 @@ call(xgettext_cmd)
# find javascript files # find javascript files
js_to_translate = [] js_to_translate = []
for (dirpath, dirnames, filenames) in os.walk(webui_js_dir): for (dirpath, dirnames, filenames) in os.walk(WEBUI_JS_DIR):
for filename in filenames: for filename in filenames:
if os.path.splitext(filename)[1] == ".js": if os.path.splitext(filename)[1] == ".js":
js_to_translate.append(os.path.join(dirpath, filename)) js_to_translate.append(os.path.join(dirpath, filename))
with open(infiles_list, "wb") as f: # find render html files
for (dirpath, dirnames, filenames) in os.walk(WEBUI_RENDER_DIR):
for filename in filenames:
if os.path.splitext(filename)[1] == ".html":
js_to_translate.append(os.path.join(dirpath, filename))
with open(INFILES_LIST, "wb") as f:
for line in js_to_translate: for line in js_to_translate:
f.write(line + "\n") f.write(line + "\n")
# Force xgettext language to parse javascript and update pot file # Force xgettext language to parse javascript and update pot file
# Note: For javascript files xgettext will parse comments, so single apostrophes or quotes are # Note: For javascript files xgettext will parse comments, so single apostrophes or quotes are
# flagged as a 'warning: untermined string'. Either ignore warning or edit javascript comment. # flagged as a 'warning: untermined string'. Either ignore warning or edit javascript comment.
output = call(xgettext_cmd + ["--language=Python", "-j"]) call(xgettext_cmd + ["--language=Python", "-j"])
# Replace YEAR and PACKAGE in the copyright message # Replace YEAR and PACKAGE in the copyright message
with open(pot_filepath, "r") as f: with open(POT_FILEPATH, "r") as f:
lines = f.readlines() lines = f.readlines()
with open(pot_filepath, "w") as f: with open(POT_FILEPATH, "w") as f:
for line in lines: for line in lines:
if "YEAR" in line: if "YEAR" in line:
line = line.replace("YEAR", str(datetime.now().year)) line = line.replace("YEAR", str(datetime.now().year))
@ -91,9 +98,9 @@ with open(pot_filepath, "w") as f:
f.write(line) f.write(line)
# Clean up temp files # Clean up temp files
os.remove(infiles_list) os.remove(INFILES_LIST)
for filepath in to_translate: for filepath in to_translate:
if filepath.endswith(".h"): if filepath.endswith(".h"):
os.remove(filepath) os.remove(filepath)
print "Created %s" % pot_filepath print "Created %s" % POT_FILEPATH