summaryrefslogtreecommitdiff
path: root/i18nfix.py
blob: 7edbcb1ed23be8008696ca0c1b63847edadd7f76 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#!/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):
    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)
        print("extracting", repr(message))
        yield lineno, funcname, message, comments

def wrap_gettext(f):
    """
    Call gettext with whitespace normalized.
    """
    def wrapper(message):
        message = normalize(message)
        print("translating", repr(message))
        return f(message)
    return wrapper