From c288ba14fd475a03ef148769ffb4146669a34de9 Mon Sep 17 00:00:00 2001 From: Christian Grothoff Date: Sat, 20 Apr 2019 13:57:39 +0200 Subject: fixing #5063, also adding i18n, with help by Torsten --- i18nfix.py | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 i18nfix.py (limited to 'i18nfix.py') diff --git a/i18nfix.py b/i18nfix.py new file mode 100644 index 0000000..9e4b26b --- /dev/null +++ b/i18nfix.py @@ -0,0 +1,34 @@ +#!/usr/bin/env python3 +# This file is in the public domain. + +""" +Extract translations from a Jinja2 template, stripping leading newlines. + +@author Florian Dold +""" + +import jinja2.ext +import re + +def normalize(message): + if isinstance(message, str): + message = message.strip() + # collapse whitespaces (including newlines) into one space. + message = re.sub("\s+", " ", message) + return message + + +def babel_extract(fileobj, keywords, comment_tags, options): + res = jinja2.ext.babel_extract(fileobj, keywords, comment_tags, options) + for lineno, funcname, message, comments in res: + message = normalize(message) + yield lineno, funcname, message, comments + +def wrap_gettext(f): + """ + Call gettext with whitespace normalized. + """ + def wrapper(message): + message = normalize(message) + return f(message) + return wrapper -- cgit v1.2.3