示例#1
0
    def test_get_localized_message(self):
        non_message = 'Non-translatable Message'
        en_message = 'A message in the default locale'
        es_translation = 'A message in Spanish'
        zh_translation = 'A message in Chinese'
        message = gettextutils.Message(en_message, 'test_domain')

        # In the Message class the translation ultimately occurs when the
        # message is turned into a string, and that is what we mock here
        def _mock_translation_and_unicode(self):
            if self.locale == 'es':
                return es_translation
            if self.locale == 'zh':
                return zh_translation
            return self.data

        self.stubs.Set(gettextutils.Message, '__unicode__',
                       _mock_translation_and_unicode)

        self.assertEqual(es_translation,
                         gettextutils.get_localized_message(message, 'es'))
        self.assertEqual(zh_translation,
                         gettextutils.get_localized_message(message, 'zh'))
        self.assertEqual(en_message,
                         gettextutils.get_localized_message(message, 'en'))
        self.assertEqual(en_message,
                         gettextutils.get_localized_message(message, 'XX'))
        self.assertEqual(en_message,
                         gettextutils.get_localized_message(message, None))
        self.assertEqual(non_message,
                         gettextutils.get_localized_message(non_message, 'A'))
示例#2
0
    def test_get_localized_message_with_named_params(self, mock_translation):
        message_with_params = 'A message with params: %(param)s'
        es_translation = 'A message with params in Spanish: %(param)s'
        message_param = 'A Message param'
        es_param_translation = 'A message param in Spanish'

        translations = {
            message_with_params: es_translation,
            message_param: es_param_translation
        }
        translator = TestTranslations.translator(translations, 'es')
        mock_translation.side_effect = translator

        msg = gettextutils.Message(message_with_params, 'test_domain')
        msg_param = gettextutils.Message(message_param, 'test_domain')
        msg = msg % {'param': msg_param}

        expected_translation = es_translation % {'param': es_param_translation}
        self._assert_translations(msg, 'es', expected_translation)
示例#3
0
 def test_message_logging(self):
     # NOTE(luisg): Logging message objects with unicode objects
     # may cause trouble by the logging mechanism trying to coerce
     # the Message object, with a wrong encoding. This test case
     # tests that problem does not occur.
     ctxt = _fake_context()
     ctxt.request_id = six.text_type('99')
     message = gettextutils.Message('test ' + unichr(128), 'test')
     self.log.info(message, context=ctxt)
     expected = "HAS CONTEXT [%s]: %s\n" % (ctxt.request_id,
                                            six.text_type(message))
     self.assertEqual(expected, self.stream.getvalue())
示例#4
0
    def test_get_localized_message_with_param(self, mock_translation):
        message_with_params = 'A message: %s'
        es_translation = 'A message: %s'
        param = 'A Message param'

        translations = {message_with_params: es_translation}
        translator = TestTranslations.translator(translations, 'es')
        mock_translation.side_effect = translator

        msg = gettextutils.Message(message_with_params, 'test_domain')
        msg = msg % param

        expected_translation = es_translation % param
        self._assert_translations(msg, 'es', expected_translation)
示例#5
0
    def test_get_localized_message_with_message_params(self, mock_translation):
        message_with_params = 'A message with params: %s %s'
        es_translation = 'A message with params in Spanish: %s %s'
        message_param = 'A message param'
        es_param_translation = 'A message param in Spanish'
        another_message_param = 'Another message param'
        another_es_param_translation = 'Another message param in Spanish'

        translations = {
            message_with_params: es_translation,
            message_param: es_param_translation,
            another_message_param: another_es_param_translation
        }
        translator = TestTranslations.translator(translations, 'es')
        mock_translation.side_effect = translator

        msg = gettextutils.Message(message_with_params, 'test_domain')
        param_1 = gettextutils.Message(message_param, 'test_domain')
        param_2 = gettextutils.Message(another_message_param, 'test_domain')
        msg = msg % (param_1, param_2)

        expected_translation = es_translation % (es_param_translation,
                                                 another_es_param_translation)
        self._assert_translations(msg, 'es', expected_translation)
示例#6
0
 def _message_with_domain(msg):
     return gettextutils.Message(msg, 'oslo')
示例#7
0
 def test_message_with_named_param(self):
     message_with_params = 'A message with params: %(param)s'
     msg = gettextutils.Message(message_with_params, 'test_domain')
     msg = msg % {'param': 'hello'}
     ret = jsonutils.to_primitive(msg)
     self.assertEqual(msg, ret)
示例#8
0
 def test_message_with_param(self):
     message_with_params = 'A message with param: %s'
     msg = gettextutils.Message(message_with_params, 'test_domain')
     msg = msg % 'test_domain'
     ret = jsonutils.to_primitive(msg)
     self.assertEqual(msg, ret)
示例#9
0
 def _lazy_gettext(msg):
     return gettextutils.Message(msg, 'oslo')