Exemplo n.º 1
0
 def test_icu_support(self):
     catalog = Catalog()
     catalog.add(
         "id",
         string=("Hello {a}, you have {g, select,"
                 "   male {{n, plural,"
                 "       =0 {no boys} one {# boy} other {# boys}"
                 "   }}"
                 "   female {{n, plural,"
                 "       =0 {no girls} one {# girl} other {# girls}"
                 "   }}"
                 "   other {{n, plural,"
                 "       =0 {no children} one {# child} other {# children}"
                 "   }}"
                 "}"),
     )
     self.assertEqual(
         MessageTranslator("en",
                           catalog).get_and_format_html_message("id",
                                                                arguments={
                                                                    "a":
                                                                    "there",
                                                                    "g":
                                                                    "male",
                                                                    "n": 17
                                                                }),
         "Hello there, you have 17 boys",
     )
     self.assertEqual(
         MessageTranslator("en", catalog).get_and_format_html_message(
             "id", arguments={
                 "a": "there",
                 "g": "female",
                 "n": 18
             }),
         "Hello there, you have 18 girls",
     )
     self.assertEqual(
         MessageTranslator("en",
                           catalog).get_and_format_html_message("id",
                                                                arguments={
                                                                    "a":
                                                                    "there",
                                                                    "g":
                                                                    "other",
                                                                    "n": 0
                                                                }),
         "Hello there, you have no children",
     )
Exemplo n.º 2
0
 def test_tags(self):
     catalog = Catalog()
     catalog.add(
         "id",
         string='<a0 id="nope">Hello</a0><b0>you</b0><div>there</div>')
     self.assertEqual(
         MessageTranslator("en", catalog).get_and_format_html_message(
             "id",
             arguments={"a": "you"},
             tags={
                 "a0": {
                     "tag": "a",
                     "attrs": {
                         "href": "/you",
                         "class": "the test",
                         "data-target": "someid",
                     },
                 },
                 "span0": {
                     "tag": "span",
                     "attrs": {
                         "id": "ignore"
                     }
                 },
             },
         ),
         '<a class="the test" data-target="someid" href="/you">Hello</a>youthere',
     )
Exemplo n.º 3
0
 def mock_get_translations(locale):
     catalog = Catalog()
     if locale == "en":
         catalog.add("id", string="Hello {a b}")
     else:
         catalog.add("id", string="Hello {a b}")
     return MessageTranslator(locale, catalog)
Exemplo n.º 4
0
 def test_format_invalid_message(self):
     self.assertEqual(
         MessageTranslator(default_locale)._process_html_message(
             "Hello {a} {0} {b}", "Hello {a} {b}", parameters={"a": "you", "b": "!"}
         ),
         "Hello you !",
     )
Exemplo n.º 5
0
 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)
Exemplo n.º 6
0
 def test_message_invalid_parameter_syntax(self):
     catalog = Catalog()
     catalog.add("id", string="Hey {a b}!")
     with self.assertRaises(ICUError):
         result = MessageTranslator("en", catalog).get_and_format_message(
             "id", arguments={
                 "a": "you",
                 "b": "!"
             })
Exemplo n.º 7
0
 def test_no_html_escape(self):
     catalog = Catalog()
     catalog.add(
         "id",
         string=
         'Hello <a href="/you?a=n&b=e">you > {param_b}</a> my & friend')
     self.assertEqual(
         MessageTranslator("en", catalog).get_and_format_message(
             "id", arguments={"param_b": "> there"}),
         'Hello <a href="/you?a=n&b=e">you > > there</a> my & friend',
     )
Exemplo n.º 8
0
 def test_message_different_arguments(self):
     catalog = Catalog()
     catalog.add("id", string="Hey {a} {0} {c}")
     self.assertEqual(
         MessageTranslator("en",
                           catalog).get_and_format_message("id",
                                                           arguments={
                                                               "a": "you",
                                                               "b": "!"
                                                           }),
         "Hey you {0} {c}",
     )
Exemplo n.º 9
0
 def test_escapes_text(self):
     catalog = Catalog()
     catalog.add("id", string="<a0>Hello &<b>&</b></a0>>")
     self.assertEqual(
         MessageTranslator("en", catalog).get_and_format_html_message(
             "id", tags={"a0": {
                 "tag": "a",
                 "attrs": {
                     "href": "/you"
                 }
             }}),
         '<a href="/you">Hello &amp;&amp;</a>&gt;',
     )
Exemplo n.º 10
0
 def test_trans_params(self):
     catalog = Catalog()
     catalog.add("id", string="Hey {a} {param_b} {c}!")
     self.assertEqual(
         MessageTranslator("en", catalog).get_and_format_html_message(
             "id",
             arguments={
                 "param_b": "there",
                 "a": "you",
                 "d": "tester"
             }),
         "Hey you there {c}!",
     )
Exemplo n.º 11
0
 def test_escapes_tag_attrs(self):
     catalog = Catalog()
     catalog.add("id", string="<a0>Hey</a0>")
     self.assertEqual(
         MessageTranslator("en", catalog).get_and_format_html_message(
             "id",
             tags={"a0": {
                 "tag": "a",
                 "attrs": {
                     "href": "/you?a=b&c=d"
                 }
             }}),
         '<a href="/you?a=b&amp;c=d">Hey</a>',
     )
Exemplo n.º 12
0
 def test_message_numeric_parameter(self):
     catalog = Catalog()
     catalog.add("id", string="Hey {a} {0} {b}")
     self.assertEqual(
         MessageTranslator("en", catalog).get_and_format_message("id",
                                                                 arguments={
                                                                     "a":
                                                                     "you",
                                                                     "b":
                                                                     "!",
                                                                     "0":
                                                                     "there"
                                                                 }),
         "Hey you there !",
     )
Exemplo n.º 13
0
 def test_params_in_tags(self):
     catalog = Catalog()
     catalog.add("id", string="<a0>Hello {name}</a0>{test}")
     self.assertEqual(
         MessageTranslator("en", catalog).get_and_format_html_message(
             "id",
             arguments={
                 "name": "you",
                 "test": "!"
             },
             tags={"a0": {
                 "tag": "a",
                 "attrs": {
                     "href": "/you"
                 }
             }},
         ),
         '<a href="/you">Hello you</a>!',
     )
Exemplo n.º 14
0
 def test_escapes_params(self):
     catalog = Catalog()
     catalog.add("id", string="<a0>Hey {name}</a0>{test}")
     self.assertEqual(
         MessageTranslator("en", catalog).get_and_format_html_message(
             "id",
             arguments={
                 "name": "<b>you</b>",
                 "test": "<b>there</b>"
             },
             tags={"a0": {
                 "tag": "a",
                 "attrs": {
                     "href": "/you"
                 }
             }},
         ),
         '<a href="/you">Hey &lt;b&gt;you&lt;/b&gt;</a>&lt;b&gt;there&lt;/b&gt;',
     )
Exemplo n.º 15
0
 def test_tag_placeholders(self):
     catalog = Catalog()
     catalog.add(
         "id",
         string=
         '<span0 class="nope">Hey {first}</span0><span1></span1> <span0>{second}</span0> <a0>{a}<b></b></a0> < <a1>there<</a1>!<br /><script type="text/javascript" src="mybadscript.js"></script>',
     )
     self.assertEqual(
         MessageTranslator("en", catalog).get_and_format_html_message(
             "id",
             arguments={
                 "a": "you",
                 "first": "hello",
                 "second": "&"
             },
             tags={
                 "a0": {
                     "tag": "a",
                     "attrs": {
                         "href": "/you"
                     }
                 },
                 "a1": {
                     "tag": "a",
                     "attrs": {
                         "href": "/there?a=b&c=d",
                         "class": "red big"
                     },
                 },
                 "span0": {
                     "tag": "span",
                     "attrs": {
                         "id": "hi"
                     }
                 },
             },
         ),
         '<span id="hi">Hey hello</span> <span id="hi">&amp;</span> <a href="/you">you</a> &lt; <a class="red big" href="/there?a=b&amp;c=d">there&lt;</a>!',
     )
Exemplo n.º 16
0
 def test_nested_tags(self):
     catalog = Catalog()
     catalog.add("id", string="<a0>Hello<b0>you</b0><div>there</div></a0>")
     self.assertEqual(
         MessageTranslator("en", catalog).get_and_format_html_message(
             "id",
             arguments={"a": "you"},
             tags={
                 "a0": {
                     "tag": "a",
                     "attrs": {
                         "href": "/you"
                     }
                 },
                 "b0": {
                     "tag": "b",
                     "attrs": {
                         "id": "hi"
                     }
                 },
             },
         ),
         '<a href="/you">Helloyouthere</a>',
     )
Exemplo n.º 17
0
 def mock_get_translations(locale):
     catalog = Catalog()
     if locale == "en":
         catalog.add("id", "Hello")
     return MessageTranslator(locale, catalog)
Exemplo n.º 18
0
 def mock_get_translations(locale):
     return MessageTranslator(locale, Catalog())
Exemplo n.º 19
0
 def mock_get_translations(locale):
     catalog = Catalog()
     catalog.add("id", string="Hello {a} {0} {b}")
     return MessageTranslator(locale, catalog)
Exemplo n.º 20
0
 def mock_get_translations(locale):
     catalog = Catalog()
     catalog.add("id", string="Show the message")
     return MessageTranslator(locale, catalog)
Exemplo n.º 21
0
 def test_parameter_list(self):
     catalog = Catalog()
     catalog.add("id", string="Hey {0} {1}!")
     with self.assertRaises(InvalidArgsError):
         MessageTranslator("en", catalog).get_and_format_message(
             "id", arguments={"0": ["you ", "there"]})
Exemplo n.º 22
0
 def mock_get_translations(locale):
     catalog = Catalog()
     catalog.add("id", string="Hello <b0>{param_b}</b0>!")
     return MessageTranslator(locale, catalog)
Exemplo n.º 23
0
 def test_arguments_tuple(self):
     catalog = Catalog()
     catalog.add("id", string="Hey {0} {1}!")
     with self.assertRaises(AttributeError):
         MessageTranslator("en", catalog).get_and_format_message(
             "id", arguments=("you", "there"))
Exemplo n.º 24
0
 def test_missing_message(self):
     catalog = Catalog()
     with self.assertRaises(KeyError):
         MessageTranslator("en", catalog).get_and_format_html_message("id")