summaryrefslogtreecommitdiff
path: root/src/zope/i18n/tests/test_zcml.py
blob: cd3d2b4ecc5319ce1fdf987d23b97988bb959050 (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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
##############################################################################
#
# 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.
#
##############################################################################
"""Test the gts ZCML namespace directives.
"""
import doctest
import os
import shutil
import stat
import unittest

from zope.component import getUtility
from zope.component import queryUtility
from zope.component.testing import PlacelessSetup
from zope.configuration import xmlconfig

import zope.i18n.tests
from zope.i18n._compat import text_type
from zope.i18n.interfaces import ITranslationDomain
from zope.i18n import config


template = """\
<configure
    xmlns='http://namespaces.zope.org/zope'
    xmlns:i18n='http://namespaces.zope.org/i18n'>
  %s
</configure>"""


class DirectivesTest(PlacelessSetup, unittest.TestCase):

    # This test suite needs the [zcml] and [compile] extra dependencies

    def setUp(self):
        super(DirectivesTest, self).setUp()
        self.context = xmlconfig.file('meta.zcml', zope.i18n)
        self.allowed = config.ALLOWED_LANGUAGES
        config.ALLOWED_LANGUAGES = None

    def tearDown(self):
        super(DirectivesTest, self).tearDown()
        config.ALLOWED_LANGUAGES = self.allowed

    def testRegisterTranslations(self):
        self.assertTrue(queryUtility(ITranslationDomain) is None)
        xmlconfig.string(
            template % '''
            <configure package="zope.i18n.tests">
            <i18n:registerTranslations directory="locale" />
            </configure>
            ''', self.context)
        path = os.path.join(os.path.dirname(zope.i18n.tests.__file__),
                            'locale', 'en', 'LC_MESSAGES', 'zope-i18n.mo')
        util = getUtility(ITranslationDomain, 'zope-i18n')
        self.assertEqual(util._catalogs.get('test'), ['test'])
        self.assertEqual(util._catalogs.get('en'), [text_type(path)])

    def testAllowedTranslations(self):
        self.assertTrue(queryUtility(ITranslationDomain) is None)
        config.ALLOWED_LANGUAGES = ('de', 'fr')
        xmlconfig.string(
            template % '''
            <configure package="zope.i18n.tests">
            <i18n:registerTranslations directory="locale" />
            </configure>
            ''', self.context)
        path = os.path.join(os.path.dirname(zope.i18n.tests.__file__),
                            'locale', 'de', 'LC_MESSAGES', 'zope-i18n.mo')
        util = getUtility(ITranslationDomain, 'zope-i18n')
        self.assertEqual(util._catalogs,
                         {'test': ['test'], 'de': [text_type(path)]})

    def testRegisterDistributedTranslations(self):
        self.assertTrue(queryUtility(ITranslationDomain, 'zope-i18n') is None)
        xmlconfig.string(
            template % '''
            <configure package="zope.i18n.tests">
            <i18n:registerTranslations directory="locale" />
            </configure>
            ''', self.context)
        xmlconfig.string(
            template % '''
            <configure package="zope.i18n.tests">
            <i18n:registerTranslations directory="locale2" />
            </configure>
            ''', self.context)
        path1 = os.path.join(os.path.dirname(zope.i18n.tests.__file__),
                             'locale', 'en', 'LC_MESSAGES', 'zope-i18n.mo')
        path2 = os.path.join(os.path.dirname(zope.i18n.tests.__file__),
                             'locale2', 'en', 'LC_MESSAGES', 'zope-i18n.mo')
        util = getUtility(ITranslationDomain, 'zope-i18n')
        self.assertEqual(util._catalogs.get('test'), ['test', 'test'])
        self.assertEqual(util._catalogs.get('en'),
                         [text_type(path1), text_type(path2)])

        msg = util.translate(u"Additional message", target_language='en')
        self.assertEqual(msg, u"Additional message translated")

        msg = util.translate(u"New Domain", target_language='en')
        self.assertEqual(msg, u"New Domain translated")

        msg = util.translate(u"New Language", target_language='en')
        self.assertEqual(msg, u"New Language translated")

    def testRegisterAndCompileTranslations(self):
        config.COMPILE_MO_FILES = True
        self.assertTrue(queryUtility(ITranslationDomain) is None)

        # Copy an old and outdated file over, so we can test if the
        # newer file check works
        testpath = os.path.join(os.path.dirname(zope.i18n.tests.__file__))
        basepath = os.path.join(testpath, 'locale3', 'en', 'LC_MESSAGES')
        in_ = os.path.join(basepath, 'zope-i18n.in')
        path = os.path.join(basepath, 'zope-i18n.mo')
        shutil.copy2(in_, path)

        # Make sure the older mo file always has an older time stamp
        # than the po file
        path_atime = os.stat(path)[stat.ST_ATIME]
        path_mtime = os.stat(path)[stat.ST_MTIME]
        os.utime(path, (path_atime, path_mtime - 6000))

        xmlconfig.string(
            template % '''
            <configure package="zope.i18n.tests">
            <i18n:registerTranslations directory="locale3" />
            </configure>
            ''', self.context)
        util = getUtility(ITranslationDomain, 'zope-i18n')
        self.assertEqual(util._catalogs,
                         {'test': ['test'], 'en': [text_type(path)]})

        msg = util.translate(u"I'm a newer file", target_language='en')
        self.assertEqual(msg, u"I'm a newer file translated")

        util = getUtility(ITranslationDomain, 'zope-i18n2')
        msg = util.translate(u"I'm a new file", target_language='en')
        self.assertEqual(msg, u"I'm a new file translated")

        # Reset the mtime of the mo file
        os.utime(path, (path_atime, path_mtime))

    def testRegisterTranslationsForDomain(self):
        self.assertTrue(queryUtility(ITranslationDomain, 'zope-i18n') is None)
        self.assertTrue(queryUtility(ITranslationDomain, 'zope-i18n2') is None)
        xmlconfig.string(
            template % '''
            <configure package="zope.i18n.tests">
            <i18n:registerTranslations directory="locale3"
                domain="zope-i18n" />
            </configure>
            ''', self.context)
        path = os.path.join(os.path.dirname(zope.i18n.tests.__file__),
                            'locale3', 'en', 'LC_MESSAGES', 'zope-i18n.mo')
        util = getUtility(ITranslationDomain, 'zope-i18n')
        self.assertEqual(util._catalogs,
                         {'test': ['test'], 'en': [text_type(path)]})

        self.assertTrue(queryUtility(ITranslationDomain, 'zope-i18n2') is None)


def test_suite():
    return unittest.TestSuite((
        unittest.defaultTestLoader.loadTestsFromName(__name__),
        doctest.DocFileSuite('configure.txt'),
    ))