def test_missing_message(self):
        def mock_get_translations(locale):
            return MessageTranslator(locale, Catalog())

        with patch("cjworkbench.i18n.trans._get_translations",
                   mock_get_translations):
            with self.assertRaises(KeyError):
                trans_html(mock_context(), "id", default="Hello")
    def test_trans_html_with_missing_context_i18n(self):
        # context[i18n] needs to be managed by the caller. And sometimes, the
        # caller has a bug. (Seen 2019-08-2019-12-06 01:57:19.327 GMT.) We want
        # Django's exception-handling code to be able to call trans_html().
        #
        # Calling trans_html without a context[i18n] is always a bug. So let's
        # test that it's logged.
        def mock_get_translations(locale):
            catalog = Catalog()
            catalog.add("id", string="Show the message")
            return MessageTranslator(locale, catalog)

        with patch("cjworkbench.i18n.trans._get_translations",
                   mock_get_translations):
            with self.assertLogs(level=logging.ERROR) as cm:
                result = trans_html({"invalid-context": "yup"},
                                    "id",
                                    default="Show the message")
            self.assertEqual(result, "Show the message")
            self.assertRegex(
                cm.output[0],
                re.escape(
                    "ERROR:server.templatetags.i18n_icu:"
                    "Missing context['i18n']['locale_id'] translating message_id id"
                ),
            )
示例#3
0
 def test_trans_tag_placeholders(self):
     catalog = Catalog()
     catalog.add(
         "id",
         string=
         '<em0>Hello</em0> <span0 class="nope">{first}</span0><span1></span1> {second} <a0>{a}<b></b></a0> < <a1>there<</a1>!<br /><script type="text/javascript" src="mybadscript.js"></script>',
     )
     with mock_app_catalogs({"en": catalog}):
         self.assertEqual(
             trans_html(
                 mock_context(),
                 "id",
                 default=
                 '<em0>Hello</em0> <span0 class="nope">{first}</span0><span1></span1> {second} <a0>{a}<b></b></a0> < <a1>there<</a1>!<br /><script type="text/javascript" src="mybadscript.js"></script>',
                 arg_a="you",
                 tag_a0_href="/you",
                 tag_a1_href="/there?a=b&c=d",
                 tag_a1_class="red big",
                 tag_span0_id="hi",
                 tag_em0="",
                 tag_div0_class="red big",
                 arg_first="hello",
                 arg_second="&",
             ),
             '<em>Hello</em> <span id="hi">hello</span> &amp; <a href="/you">you</a> &lt; <a class="red big" href="/there?a=b&amp;c=d">there&lt;</a>!',
         )
    def test_trans_tag_placeholders(self):
        def mock_get_translations(locale):
            catalog = Catalog()
            catalog.add(
                "id",
                string=
                '<em0>Hello</em0> <span0 class="nope">{first}</span0><span1></span1> {second} <a0>{a}<b></b></a0> < <a1>there<</a1>!<br /><script type="text/javascript" src="mybadscript.js"></script>',
            )
            return MessageTranslator(locale, catalog)

        with patch("cjworkbench.i18n.trans._get_translations",
                   mock_get_translations):
            self.assertEqual(
                trans_html(
                    mock_context(),
                    "id",
                    default=
                    '<em0>Hello</em0> <span0 class="nope">{first}</span0><span1></span1> {second} <a0>{a}<b></b></a0> < <a1>there<</a1>!<br /><script type="text/javascript" src="mybadscript.js"></script>',
                    arg_a="you",
                    tag_a0_href="/you",
                    tag_a1_href="/there?a=b&c=d",
                    tag_a1_class="red big",
                    tag_span0_id="hi",
                    tag_em0="",
                    tag_div0_class="red big",
                    arg_first="hello",
                    arg_second="&",
                ),
                '<em>Hello</em> <span id="hi">hello</span> &amp; <a href="/you">you</a> &lt; <a class="red big" href="/there?a=b&amp;c=d">there&lt;</a>!',
            )
示例#5
0
 def test_default_locale(self):
     catalog = Catalog()
     catalog.add("id", "Hello")
     with mock_app_catalogs({"en": catalog, "el": Catalog()}):
         self.assertEqual(
             trans_html(mock_context(locale_id="el"), "id",
                        default="Hello"), "Hello")
示例#6
0
 def test_trans_noop(self):
     catalog = Catalog()
     catalog.add("id", string="Hello {a} {b}!")
     with mock_app_catalogs({"en": catalog}):
         self.assertIsNone(
             trans_html(mock_context(),
                        "id",
                        noop=True,
                        default="Hello {a} {b}!"))
    def test_default_is_ignored(self):
        def mock_get_translations(locale):
            catalog = Catalog()
            catalog.add("id", string="Hello")
            return MessageTranslator(locale, catalog)

        with patch("cjworkbench.i18n.trans._get_translations",
                   mock_get_translations):
            self.assertEqual(
                trans_html(mock_context(), "id", default="Nothing"), "Hello")
    def test_default_locale(self):
        def mock_get_translations(locale):
            catalog = Catalog()
            if locale == "en":
                catalog.add("id", "Hello")
            return MessageTranslator(locale, catalog)

        with patch("cjworkbench.i18n.trans._get_translations",
                   mock_get_translations):
            self.assertEqual(
                trans_html(mock_context(locale_id="el"), "id",
                           default="Hello"), "Hello")
示例#9
0
    def test_trans_params(self):
        self.assertEqual(
            trans_html(
                mock_context(),
                mock_message_id,
                default="Hello {a} {param_b} {c}!",
                arg_param_b="there",
                arg_a="you",
                arg_d="tester",
            ),
            "Hello you there {c}!",
        )

        with self.assertRaises(Exception):
            trans_html(
                mock_context(),
                mock_message_id,
                default="Hello {a} {0} {b}",
                arg_a="you",
                arg_0="!",
                arg_b="2",
            ),
    def test_trans_noop(self):
        def mock_get_translations(locale):
            catalog = Catalog()
            catalog.add("id", string="Hello {a} {b}!")
            return MessageTranslator(locale, catalog)

        with patch("cjworkbench.i18n.trans._get_translations",
                   mock_get_translations):
            self.assertIsNone(
                trans_html(mock_context(),
                           "id",
                           noop=True,
                           default="Hello {a} {b}!"))
示例#11
0
 def test_trans_tag_without_attributes(self):
     catalog = Catalog()
     catalog.add("id", string="Hello <b0>{param_b}</b0>!")
     with mock_app_catalogs({"en": catalog}):
         self.assertEqual(
             trans_html(
                 mock_context(),
                 "id",
                 default="Hello <b0>{param_b}</b0>!",
                 arg_param_b="there",
                 tag_b0="",
             ),
             "Hello <b>there</b>!",
         )
示例#12
0
 def test_trans_params(self):
     catalog = Catalog()
     catalog.add("id", string="Hello {a} {0} {b}")
     with mock_app_catalogs({"en": catalog}):
         self.assertEqual(
             trans_html(
                 mock_context(),
                 "id",
                 default="Hello {a} {0} {b}",
                 arg_b="2",
                 arg_a="you",
                 arg_0="!",
             ),
             "Hello you ! 2",
         )
示例#13
0
 def test_trans_tag_placeholders(self):
     self.assertEqual(
         trans_html(
             mock_context(),
             mock_message_id,
             default='<span0 class="nope">Hello {first}</span0><span1></span1> {second} <a0>{a}<b></b></a0> < <a1>there<</a1>!<br /><script type="text/javascript" src="mybadscript.js"></script>',
             arg_a="you",
             tag_a0_href="/you",
             tag_a1_href="/there?a=b&c=d",
             tag_a1_class="red big",
             tag_span0_id="hi",
             tag_div0_class="red big",
             arg_first="hello",
             arg_second="&",
         ),
         '<span id="hi">Hello hello</span> &amp; <a href="/you">you</a> &lt; <a class="red big" href="/there?a=b&amp;c=d">there&lt;</a>!',
     )
    def test_trans_tag_without_attributes(self):
        def mock_get_translations(locale):
            catalog = Catalog()
            catalog.add("id", string="Hello <b0>{param_b}</b0>!")
            return MessageTranslator(locale, catalog)

        with patch("cjworkbench.i18n.trans._get_translations",
                   mock_get_translations):
            self.assertEqual(
                trans_html(
                    mock_context(),
                    "id",
                    default="Hello <b0>{param_b}</b0>!",
                    arg_param_b="there",
                    tag_b0="",
                ),
                "Hello <b>there</b>!",
            )
    def test_trans_params(self):
        def mock_get_translations(locale):
            catalog = Catalog()
            catalog.add("id", string="Hello {a} {0} {b}")
            return MessageTranslator(locale, catalog)

        with patch("cjworkbench.i18n.trans._get_translations",
                   mock_get_translations):
            self.assertEqual(
                trans_html(
                    mock_context(),
                    "id",
                    default="Hello {a} {0} {b}",
                    arg_b="2",
                    arg_a="you",
                    arg_0="!",
                ),
                "Hello you ! 2",
            )
示例#16
0
 def test_missing_message(self):
     with mock_app_catalogs({"en": Catalog()}):
         with self.assertRaises(KeyError):
             trans_html(mock_context(), "id", default="Hello")
示例#17
0
 def test_default_is_ignored(self):
     catalog = Catalog()
     catalog.add("id", string="Hello")
     with mock_app_catalogs({"en": catalog}):
         self.assertEqual(
             trans_html(mock_context(), "id", default="Nothing"), "Hello")
示例#18
0
 def test_trans_noop(self):
     self.assertIsNone(
         trans_html(
             mock_context(), mock_message_id, noop=True, default="Hello {a} {b}!"
         )
     )