summaryrefslogtreecommitdiff
path: root/src/zope/tal/tests/test_talgettext.py
blob: 5d0a0f18f22f49c029b0f7b6f6e8d39f158550c2 (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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
##############################################################################
#
# Copyright (c) 2001, 2002 Zope Foundation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
"""Tests for the talgettext utility.
"""

from __future__ import print_function

import sys
import tempfile
import unittest
import warnings

try:
    # Python 2.x
    from StringIO import StringIO
except ImportError:
    # Python 3.x
    from io import StringIO

from zope.tal.htmltalparser import HTMLTALParser
from zope.tal.talgettext import POTALInterpreter
from zope.tal.talgettext import POEngine
from zope.tal.tests import utils

class test_POEngine(unittest.TestCase):
    """Test the PO engine functionality, which simply adds items to a catalog
    as .translate is called
    """

    def test_translate(self):
        test_keys = ['foo', 'bar', 'blarf', 'washington']

        engine = POEngine()
        engine.file = 'foo.pt'
        for key in test_keys:
            engine.translate(key, 'domain')

        for key in test_keys:
            self.assertTrue(key in engine.catalog['domain'],
                        "POEngine catalog does not properly store message ids"
                        )

    def test_translate_existing(self):
        engine = POEngine()
        # This tries to reproduce a big surfacing in a template of
        # PloneSoftwareCenter when using the i18ndude package to
        # extract translatable strings, which uses zope.tal.  The
        # relevant html snippet is this:
        #
        # <a href="#" title="Read more&hellip;"
        #    i18n:attributes="title label_read_more"
        #    tal:attributes="href release/absolute_url">
        #    <span i18n:translate="label_read_more">Read more&hellip;</span>
        # </a>
        #
        # Due to the different ways that i18n:attributes and
        # i18n:translate are handled, the attribute gets passed to the
        # translate method with the html entity interpreted as a
        # unicode, and the i18n:translate gets passed as a simple
        # string with the html entity intact.  That may need a fix
        # elsewhere, but at the moment it gives a warning.  The very
        # least we can do is make sure that this does not give a
        # UnicodeDecodeError, which is what we test here.
        engine.file = 'psc_release_listing.pt'
        # position is position in file.
        engine.translate('foo', 'domain',
                         default=u'Read more\u2026', position=7)
        # Adding the same key with the same default is fine.
        engine.translate('foo', 'domain',
                         default=u'Read more\u2026', position=13)
        # Adding the same key with a different default is bad and
        # triggers a warning.
        with warnings.catch_warnings(record=True) as log:
            warnings.simplefilter("always")
            engine.translate('foo', 'domain',
                             default='Read still more&hellip;', position=42)
            self.assertEqual(len(log), 1)
            message = log[0].message
            with tempfile.TemporaryFile('w+') as printfile:
                print(message, file=printfile)
                printfile.seek(0)
                self.assertTrue("already exists with a different default"
                                in printfile.read())

    def test_dynamic_msgids(self):
        sample_source = """
            <p i18n:translate="">
              Some
              <span tal:replace="string:strange">dynamic</span>
              text.
            </p>
            <p i18n:translate="">
              A <a tal:attributes="href path:dynamic">link</a>.
            </p>
        """
        p = HTMLTALParser()
        p.parseString(sample_source)
        program, macros = p.getCode()
        engine = POEngine()
        engine.file = 'sample_source'
        POTALInterpreter(program, macros, engine, stream=StringIO(),
                         metal=False)()
        msgids = []
        for domain in engine.catalog.values():
            msgids += list(domain)
        msgids.sort()
        self.assertEqual(msgids,
            ['A <a href="${DYNAMIC_CONTENT}">link</a>.',
            'Some ${DYNAMIC_CONTENT} text.'])


def test_suite():
    suite = unittest.makeSuite(test_POEngine)
    return suite

if __name__ == "__main__":
    errs = utils.run_suite(test_suite())
    sys.exit(errs and 1 or 0)