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", )
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', )
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)
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 !", )
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)
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": "!" })
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', )
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}", )
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 &&</a>>', )
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}!", )
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&c=d">Hey</a>', )
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 !", )
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>!', )
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 <b>you</b></a><b>there</b>', )
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">&</span> <a href="/you">you</a> < <a class="red big" href="/there?a=b&c=d">there<</a>!', )
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>', )
def mock_get_translations(locale): catalog = Catalog() if locale == "en": catalog.add("id", "Hello") return MessageTranslator(locale, catalog)
def mock_get_translations(locale): return MessageTranslator(locale, Catalog())
def mock_get_translations(locale): catalog = Catalog() catalog.add("id", string="Hello {a} {0} {b}") return MessageTranslator(locale, catalog)
def mock_get_translations(locale): catalog = Catalog() catalog.add("id", string="Show the message") return MessageTranslator(locale, catalog)
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"]})
def mock_get_translations(locale): catalog = Catalog() catalog.add("id", string="Hello <b0>{param_b}</b0>!") return MessageTranslator(locale, catalog)
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"))
def test_missing_message(self): catalog = Catalog() with self.assertRaises(KeyError): MessageTranslator("en", catalog).get_and_format_html_message("id")