~chromium-team/chromium-browser/translations-pump

1 by Chad Miller
Checkin.
1
"""Common functions to all gettext file formats"""
2
3
 
4
from io import StringIO
2 by Chad Miller
Fix exporting gettext placeholders.
5
import re
6
import logging
1 by Chad Miller
Checkin.
7
8
def gettext_file_sorting_function(item):
9
    r"""Used to sort .items of txln_info pairs so gettext output files are
10
    consistently ordered.
11
5 by Chad MILLER
.
12
    >>> gettext_file_sorting_function(("s", {"langs":{"l":["1","2"]}, "refs":[{"grdfile":"f1"},{"grdfile":"f2"}]}))
1 by Chad Miller
Checkin.
13
    (('f1', 'f2'), 's')
5 by Chad MILLER
.
14
    >>> gettext_file_sorting_function(("s", {"refs":[{"grdfile":"f1"},{"grdfile":"f2"}]}))
1 by Chad Miller
Checkin.
15
    (('f1', 'f2'), 's')
16
    >>> gettext_file_sorting_function(("s", {"langs":{"l":["1","2"]}, "refs":[]}))
17
    ((), 's')
18
    >>> gettext_file_sorting_function(("s", {"langs":{"l":["1","2"]},}))
19
    ((), 's')
20
    """
5 by Chad MILLER
.
21
    return tuple([tuple(sorted(r["grdfile"] for r in item[1].get("refs", []))), item[0]])
1 by Chad Miller
Checkin.
22
23
24
def grd_msg_write_in_po(header, text, outfile):
25
    r"""Rewrite a message into several lines in a gettext file.  Outside,
26
    string literals are concatenated together. We cater to that by making each
27
    line into a "-delimited line with the newline encoded into the string.
28
2 by Chad Miller
Fix exporting gettext placeholders.
29
    >>> f = StringIO(); grd_msg_write_in_po("asdf", "12<ph name=\"AB\" />3\n4<ph name=\"C\"/>56", f).getvalue()
30
    'asdf ""\n"12%{AB}3\\n"\n"4%{C}56"\n'
1 by Chad Miller
Checkin.
31
    """
2 by Chad Miller
Fix exporting gettext placeholders.
32
33
    if "%{" in text:
34
        logging.warn("Text from GRD, %r, looks like it has percent-openbrace already, which would be confusing later.", text)
35
36
    text = re.sub(r'<ph\s+name="([^"]+)"\s*/>', r"%{\1}", text)
1 by Chad Miller
Checkin.
37
    outfile.write('{0} ""\n'.format(header))
38
    outfile.write('"' + '\\n"\n"'.join(line.replace("\\", "\\\\") for line in text.split("\n")) + '"\n')
39
40
    return outfile