d49cfaac72
Blueprint remove-namespace-packages Depends-on: I5bd1cdc35d2ecaf98a0802fff589d8c37aaa8028 for openstack/django_openstack_auth Depends-on: I5da7ca7a8a909eb12a4417e7d5f5b6bd33da4de8 for openstack/horizon Depends-on: Ib17a74ada3122ce45c52fdd7dcf8d27fafbb1d70 for openstack/openstack-doc-tools Depends-on: I1f539cf9f5d6f48e10cac381c13eeb5fa314abd4 for openstack/python-ceilometerclient Depends-on: Ic21066211a071591854280a93a53bb2255cb8ad3 for openstack/python-ironicclient Depends-on: I387a7a1a817058a4daca313fe6df60612cb84864 for openstack/python-keystoneclient Depends-on: I32767615b2d678b6ce545722b6b9d1704cb1ffb4 for openstack/python-keystoneclient-saml2 Depends-on: Id26264ad0b002ab21e60431a2fd39fcecf76c490 for openstack/python-magnumclient Depends-on: Ib86c36f81f6fdbee64d88dd0b1d126bcc16649ac for openstack/python-manilaclient Depends-on: If5e8257085b5fd0a26a34705505fc0077adbabb2 for openstack/python-novaclient Depends-on: I37ce99d8c9a2ba5b97a12a0c2666f6340dbc7f42 for openstack/python-saharaclient Depends-on: I2a8caa859830b3416bfe54e4261dd3415ac5a76a for openstack/python-troveclient Depends-on: I324a3bd0c468a3e84f633497ad5c0d59c5ccc455 for openstack/python-tuskarclient Depends-on: Id54b2381f00d9905c4bb07821f54c5aaaa48d970 for openstack/python-zaqarclient Depends-on: I9d3e80d94795060d375aae30ce249513aae3fd97 for openstack/trove Change-Id: I1b100a426f84b91f9c5a9a857f420e36c209cbb4
166 lines
5.5 KiB
Python
166 lines
5.5 KiB
Python
# 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
|
|
import six
|
|
|
|
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 six.text_type(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):
|
|
super(PrefixLazyTranslation, self).__init__()
|
|
self.languages = languages or [PrefixLazyTranslation._DEFAULT_LANG]
|
|
|
|
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(
|
|
'oslo_i18n.get_available_languages',
|
|
lambda *x, **y: self.languages))
|
|
self.useFixture(fixtures.MonkeyPatch('gettext.translation',
|
|
_prefix_translations))
|