From 5db3b214371c1a871f9cce361c61a266f0b4b260 Mon Sep 17 00:00:00 2001 From: Vinyl Darkscratch Date: Mon, 6 Nov 2017 01:40:03 -0800 Subject: [PATCH 1/2] Add update_languages script --- scripts/update_languages.py | 82 +++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 scripts/update_languages.py diff --git a/scripts/update_languages.py b/scripts/update_languages.py new file mode 100644 index 00000000..3e94cb36 --- /dev/null +++ b/scripts/update_languages.py @@ -0,0 +1,82 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# Written by Vinyl Darkscratch, www.queengoob.org + +# TODO: add comments + +import os +import ast +import sys +import inspect + +currentdir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe()))) +parentdir = os.path.dirname(currentdir) +sys.path.insert(0, parentdir) + +from gluon.cfs import getcfs +from gluon.utf8 import Utf8 +from gluon._compat import copyreg, PY2, maketrans, iterkeys, unicodeT, to_unicode, to_bytes, iteritems, to_native, pjoin +from gluon.languages import findT + +# This script can be run with no arguments (which sets the language folder to the current working directory, and default language to English), one argument (which sets the default language), or two arguments (language folder path and default language). +# When run, it will update the default language, as well as strip all of the strings found in the non-default languages but not in the default language, and add the strings found in the default language to the non-default languages it is not, making sure translators don't do additional work that will never be used. + +def read_dict_aux(filename): + lang_text = open(filename, 'r').read().replace(b'\r\n', b'\n') + try: + return safe_eval(to_native(lang_text)) or {} + except Exception: + e = sys.exc_info()[1] + status = 'Syntax error in %s (%s)' % (filename, e) + return {'__corrupted__': status} + +def read_dict(filename): + return getcfs('lang:' + filename, filename, lambda: read_dict_aux(filename)) + +def safe_eval(text): + if text.strip(): + try: + return ast.literal_eval(text) + except ImportError: + return eval(text, {}, {}) + return None + +def sort_function(x, y): + return cmp(unicode(x, 'utf-8').lower(), unicode(y, 'utf-8').lower()) + +def write_file(file, contents): + file.write('# -*- coding: utf-8 -*-\n{\n') + for key in sorted(contents, sort_function): + file.write('%s: %s,\n' % (repr(Utf8(key)), + repr(Utf8(contents[key])))) + file.write('}\n') + file.close() + +def update_languages(cwd, default_lang): + defaultfp = os.path.join(cwd, '%s.py' %default_lang) + findT(cwd, default_lang) + default = read_dict(defaultfp) + + for x in os.listdir(cwd): + if x == default_lang or x.startswith("plural-"): continue + + i18n = read_dict(os.path.join(cwd, x)) + if i18n: + nd = default + for k_d1 in i18n: + if k_d1 in default: + nd[k_d1] = i18n[k_d1] + write_file(open(x, 'w'), nd) + print x + +if __name__ == "__main__": + cwd = os.getcwd() + default_lang = 'en' + + if len(sys.argv) > 2: + cwd = sys.argv[1] + default_lang = sys.argv[2] + elif len(sys.argv) > 1: + default_lang = sys.argv[1] + + update_languages(cwd, default_lang) From 3ab91b9fe9b8d760b4e355beccbaa58095d58dbf Mon Sep 17 00:00:00 2001 From: Vinyl Darkscratch Date: Mon, 6 Nov 2017 01:42:25 -0800 Subject: [PATCH 2/2] Add check_lang_progress script --- scripts/check_lang_progress.py | 38 ++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 scripts/check_lang_progress.py diff --git a/scripts/check_lang_progress.py b/scripts/check_lang_progress.py new file mode 100644 index 00000000..6c53bbd7 --- /dev/null +++ b/scripts/check_lang_progress.py @@ -0,0 +1,38 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# Written by Vinyl Darkscratch, www.queengoob.org + +import sys +import os + +# TODO: add comments + +# This script can be run with no arguments (which sets the language folder to the current working directory, and default language to English), one argument (which sets the default language), or two arguments (language folder path and default language). +# When run, this script will compare the original language's strings to their assigned values and determine if they match, just like web2py's interface. While some phrases may be the exact same between both languages, this should provide a good idea of how far along translations are. + +def check_lang_progress(cwd, default_lang): + for x in os.listdir(cwd): + if x == default_lang or x.startswith("plural-"): continue + + data = eval(open(os.path.join(cwd, x)).read()) + + total = 0 + translated = 0 + + for key in data: + total += 1 + if key.replace('@markmin\x01', '') != data[key]: translated += 1 + + print "Translations for %s (%s): %d/%d Translated (%d Untranslated)" %(data['!langname!'], data['!langcode!'], translated, total, total-translated) + +if __name__ == "__main__": + cwd = os.getcwd() + default_lang = 'en' + + if len(sys.argv) > 2: + cwd = sys.argv[1] + default_lang = sys.argv[2] + elif len(sys.argv) > 1: + default_lang = sys.argv[1] + + check_lang_progress(cwd, default_lang) \ No newline at end of file