From 3af27d96037fb7cb38319132af9165ff86c79e1e Mon Sep 17 00:00:00 2001 From: TW Date: Fri, 1 Feb 2019 13:19:47 +0100 Subject: [PATCH] Add debug tools for i18n UI layout checking (#160) --- CONTRIBUTING.md | 7 +++++-- src/vorta/i18n/__init__.py | 38 ++++++++++++++++++++++++++++++++++++-- src/vorta/log.py | 1 + 3 files changed, 42 insertions(+), 4 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 0ed61a26..3e063be4 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -82,12 +82,15 @@ Translations are updated there: https://www.Transifex.com/borgbase/vorta/ - Open a new issue on Github. - Edit the language on Transifex. -### Data Flow to/from Transifex +### Using and Testing Transifex Translations -- Extract from source files: `make translations-from-source` +- Extract from source files (needed after most code changes to update line number): + `make translations-from-source` - Push to Transifex: `make translations-push` - Pull finished translations from Transifex: `make translations-pull` - Compile: `make translations-to-qm` +- Test with specific translation: `LANG=de vorta` +- Scale strings to test UI: `LANG=de TRANS_SCALE=200 vorta --foreground` ### Notes for Developers diff --git a/src/vorta/i18n/__init__.py b/src/vorta/i18n/__init__.py index bc5fb422..cfeb8e27 100644 --- a/src/vorta/i18n/__init__.py +++ b/src/vorta/i18n/__init__.py @@ -8,12 +8,46 @@ from PyQt5.QtCore import QTranslator, QLocale logger = logging.getLogger(__name__) +# To check if the UI layout copes flexibly with translated strings, an env var +# TRANS_SCALE can be used - it will result in transformed translated strings, e.g.: +# 100 = scale 100%, normal +# N>0 = scale to N% +# N<0 = additionally, do a RTL simulation at scale -N% +# usage: TRANS_SCALE=200 LANG=de_DE vorta --foreground +trans_scale = int(os.environ.get('TRANS_SCALE', '100')) + + +class VortaTranslator(QTranslator): + def translate(self, context, text, disambiguation=None, n=-1): + translated = super().translate(context, text, disambiguation=disambiguation, n=n) + scale = trans_scale + if scale == 100: # normal, production usage + return translated + + # for UI layout debugging: + has_placeholders = '%' in translated + has_html = translated.startswith('<') and translated.endswith('>') + if has_placeholders or has_html: + # not supported kinds of strings, just return normal: + return translated + if scale < 0: + # for simple RTL checking: reverse translated string + translated = translated[::-1] + scale = -scale + if 0 < scale < 100: + step = 100 // scale + scale = None + else: + step = None + scale = scale // 100 + return translated * scale if step is None else translated[::step] + def init_translations(app): global application, translator, locale # if we don't keep a reference on these, it stops working. pyqt bug? application = app - translator = QTranslator() - locale = QLocale() + translator = VortaTranslator() + locale = QLocale(os.environ.get('LANG', None)) qm_path = os.path.abspath(os.path.join(os.path.dirname(__file__), 'qm')) ui_langs = locale.uiLanguages() succeeded = translator.load(locale, 'vorta', prefix='.', directory=qm_path) # e.g. vorta/i18n/qm/vorta.de_DE.qm diff --git a/src/vorta/log.py b/src/vorta/log.py index 2bfdf8ad..bfc8b9cb 100644 --- a/src/vorta/log.py +++ b/src/vorta/log.py @@ -17,6 +17,7 @@ def init_logger(foreground=False): logger.setLevel(logging.DEBUG) logging.getLogger('peewee').setLevel(logging.INFO) logging.getLogger('apscheduler').setLevel(logging.INFO) + logging.getLogger('PyQt5').setLevel(logging.INFO) # create logging format formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')