From 2e2851dc13d73352530dd4495c7e05603b2e520d Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Wed, 10 Apr 2024 23:38:38 +0200 Subject: Adding upstream version 2.1.2~dev0+20240219. Signed-off-by: Daniel Baumann --- gen_web_gettext.py | 119 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 119 insertions(+) create mode 100755 gen_web_gettext.py (limited to 'gen_web_gettext.py') diff --git a/gen_web_gettext.py b/gen_web_gettext.py new file mode 100755 index 0000000..80186e9 --- /dev/null +++ b/gen_web_gettext.py @@ -0,0 +1,119 @@ +#!/usr/bin/env python +# +# Copyright (C) 2009-2012 Damien Churchill +# +# This file is part of Deluge and is licensed under GNU General Public License 3.0, or later, with +# the additional special exception to link portions of this program with the OpenSSL library. +# See LICENSE for more details. +# + +"""Script to parse javascript files for translation strings and generate gettext.js""" + +import os +import re + +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 check_missing_markup(js_dir): + """Search js to check for missed translation markup.""" + + # A list of common extjs attributes that are usually marked for translation. + attr_list = [ + "text: '", + "msg: '", + "title: '", + "fieldLabel: '", + "boxLabel: '", + "tooltip: '", + "header: '", + "defaultText: '", + "unit: '", + r"setText\('", + r"addButton\('", + ] + + # Don't match against any of these chars at start of string value. + except_chars = "' &#" + + # A list of strings that should be skipped shuold the match contain them. + skip = ['HTTP:'] + + # Create a list of the matching strings to search for with the except_chars appended to each one. + string_re = re.compile( + '(' + + ')|('.join(['%s[^' + except_chars + "].*'"] * len(attr_list)) + % tuple(attr_list) + + ')' + ) + + strings = {} + for root, dnames, files in os.walk(js_dir): + for filename in files: + if os.path.splitext(filename)[1] != '.js': + continue + for lineno, line in enumerate(open(os.path.join(root, filename))): + for match in string_re.finditer(line): + for string in match.groups(): + # Ignore string that contains only digits or specificied strings in skip. + if ( + not string + or string.split('\'')[1].isdigit() + or any(x in string for x in skip) + ): + continue + locations = strings.get(string, []) + locations.append( + (os.path.join(root, filename), str(lineno + 1)) + ) + strings[string] = locations + return strings + + +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_SUBST_TPL = "GetText.add('{key}','${{escape(_(\"{key}\"))}}')\n" + + +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 filename.endswith('.js'): + filepath = os.path.join(root, filename) + with open(os.path.join(root, filename)) as _file: + for lineno, line in enumerate(_file, 1): + for match in string_re.finditer(line): + string = match.group(1) + locations = strings.get(string, []) + locations.append((filepath, lineno)) + strings[string] = locations + + 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 sorted(strings): + if DEBUG: + fp.write( + '\n//: %s' % '//: '.join(['%s:%s\n' % x for x in strings[key]]) + ) + fp.write(GETTEXT_SUBST_TPL.format(key=key)) + return gettext_file + + +if __name__ == '__main__': + gettext_fname = create_gettext_js(WEBUI_JS_DIR) + print('Created: %s' % gettext_fname) + missed_markup = check_missing_markup(WEBUI_JS_DIR) + if missed_markup: + print('Possible missed text for translation markup:') + for text, filenames in missed_markup.iteritems(): + for filename_lineno in filenames: + print('{:<58} {}'.format(':'.join(filename_lineno), text)) -- cgit v1.2.3