def test_translation_bool(): def translation(s): return Translation(localized_string=s) assert bool(translation('text')) is True assert bool(translation(' ')) is False assert bool(translation('')) is False assert bool(translation(None)) is False
def get_regex(self, lang=None): lang = lang or get_language() # Only attempt to get the translation of the regex if the regex string # is not empty. The empty string is handled as a special case by # Django's gettext. It's where it stores its metadata. if self._raw_regex != '': regex_in_lang = translation(lang).ugettext(self._raw_regex) else: regex_in_lang = self._raw_regex return self._regex_dict.setdefault(lang, re.compile(regex_in_lang, re.UNICODE))
def trans_in_lang(string, lang): """ Translate a string into a specific language (which can be different than the set language). Usage: {{ var|trans_in_lang:"fr" }} """ if check_for_language(lang): return translation(lang).ugettext(string) return string
def test_maclines(self): """ Translations on files with mac or dos end of lines will be converted to unix eof in .po catalogs, and they have to match when retrieved """ from django.utils.translation.trans_real import translation ca_translation = translation('ca') ca_translation._catalog[u'Mac\nEOF\n'] = u'Catalan Mac\nEOF\n' ca_translation._catalog[u'Win\nEOF\n'] = u'Catalan Win\nEOF\n' activate('ca') try: self.assertEqual(u'Catalan Mac\nEOF\n', ugettext(u'Mac\rEOF\r')) self.assertEqual(u'Catalan Win\nEOF\n', ugettext(u'Win\r\nEOF\r\n')) finally: deactivate()
def test_esperanto_translations_in_student_view(self): """ Checks if the template and its context are both correctly translated. """ xblock = make_xblock('freetextresponse', FreeTextResponse, {}) current_translation = translation(get_language()) with django.utils.translation.override('eo'), \ patch.object(current_translation, 'gettext') as gettext, \ patch.object(current_translation, 'ngettext') as ngettext: gettext.side_effect = mock_gettext ngettext.side_effect = mock_gettext student_view = xblock.student_view() student_view_html = student_view.content self.assertIn('Süßmït', student_view_html) english_text = 'Your response must be between' translated_text = 'Ýöür réspönsé müst ßé ßétwéén' self.assertNotIn(english_text, student_view_html) self.assertIn(translated_text, student_view_html)
def test_translation_unicode(): def translation(s): return Translation(localized_string=s) assert six.text_type(translation('hello')) == 'hello' assert six.text_type(translation(None)) == ''
def get_regex(self, lang=None): lang = lang or get_language() return self._regex_dict.setdefault(lang, re.compile(translation(lang).ugettext(self._raw_regex), re.UNICODE))