summaryrefslogtreecommitdiff
path: root/oslo_i18n/fixture.py
blob: 5c5fddc350fcdb2d8ce66eb1869a030cdab4680e (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
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.
"""Test fixtures for working with oslo_i18n.

"""

import gettext

import fixtures

from oslo_i18n import _lazy
from oslo_i18n import _message


class Translation(fixtures.Fixture):
    """Fixture for managing translatable strings.

    This class provides methods for creating translatable strings
    using both lazy translation and immediate translation. It can be
    used to generate the different types of messages returned from
    oslo_i18n to test code that may need to know about the type to
    handle them differently (for example, error handling in WSGI apps,
    or logging).

    Use this class to generate messages instead of toggling the global
    lazy flag and using the regular translation factory.

    """

    def __init__(self, domain='test-domain'):
        """Initialize the fixture.

        :param domain: The translation domain. This is not expected to
                       coincide with an actual set of message
                       catalogs, but it can.
        :type domain: str
        """
        self.domain = domain

    def lazy(self, msg):
        """Return a lazily translated message.

        :param msg: Input message string. May optionally include
                    positional or named string interpolation markers.
        :type msg: str or unicode

        """
        return _message.Message(msg, domain=self.domain)

    def immediate(self, msg):
        """Return a string as though it had been translated immediately.

        :param msg: Input message string. May optionally include
                    positional or named string interpolation markers.
        :type msg: str or unicode

        """
        return str(msg)


class ToggleLazy(fixtures.Fixture):
    """Fixture to toggle lazy translation on or off for a test."""

    def __init__(self, enabled):
        """Force lazy translation on or off.

        :param enabled: Flag controlling whether to enable or disable
            lazy translation, passed to :func:`~oslo_i18n.enable_lazy`.
        :type enabled: bool
        """
        super(ToggleLazy, self).__init__()
        self._enabled = enabled
        self._original_value = _lazy.USE_LAZY

    def setUp(self):
        super(ToggleLazy, self).setUp()
        self.addCleanup(self._restore_original)
        _lazy.enable_lazy(self._enabled)

    def _restore_original(self):
        _lazy.enable_lazy(self._original_value)


class _PrefixTranslator(gettext.NullTranslations):
    """Translator that adds prefix to message ids

    NOTE: gettext.NullTranslations is an old style class

    :parm prefix: prefix to add to message id.  If not specified (None)
                  then 'noprefix' is used.
    :type prefix: string

    """

    def __init__(self, fp=None, prefix='noprefix'):
        gettext.NullTranslations.__init__(self, fp)
        self.prefix = prefix

    def gettext(self, message):
        msg = gettext.NullTranslations.gettext(self, message)
        return self.prefix + msg

    def ugettext(self, message):
        msg = gettext.NullTranslations.ugettext(self, message)
        return self.prefix + msg


def _prefix_translations(*x, **y):
    """Use message id prefixed with domain and language as translation

    """
    return _PrefixTranslator(prefix=x[0] + '/' + y['languages'][0] + ': ')


class PrefixLazyTranslation(fixtures.Fixture):
    """Fixture to prefix lazy translation enabled messages

    Use of this fixture will cause messages supporting lazy translation to
    be replaced with the message id prefixed with 'domain/language:'.
    For example, 'oslo/en_US: message about something'.  It will also
    override the available languages returned from
    oslo_18n.get_available_languages to the specified languages.

    This will enable tests to ensure that messages were translated lazily
    with the specified language and not immediately with the default language.

    NOTE that this does not work unless lazy translation is enabled, so it
    uses the ToggleLazy fixture to enable lazy translation.

    :param languages: list of languages to support.  If not specified (None)
                      then ['en_US'] is used.
    :type languages: list of strings

    """

    _DEFAULT_LANG = 'en_US'

    def __init__(self, languages=None, locale=None):
        super(PrefixLazyTranslation, self).__init__()
        self.languages = languages or [PrefixLazyTranslation._DEFAULT_LANG]
        self.locale = locale

    def setUp(self):
        super(PrefixLazyTranslation, self).setUp()
        self.useFixture(ToggleLazy(True))
        self.useFixture(fixtures.MonkeyPatch(
            'oslo_i18n._gettextutils.get_available_languages',
            lambda *x, **y: self.languages))
        self.useFixture(fixtures.MonkeyPatch(
            'oslo_i18n.get_available_languages',
            lambda *x, **y: self.languages))
        self.useFixture(fixtures.MonkeyPatch('gettext.translation',
                                             _prefix_translations))
        self.useFixture(fixtures.MonkeyPatch('locale.getdefaultlocale',
                                             lambda *x, **y: self.locale))