def lazy_pgettext(context, string, **variables): """Like :func:`pgettext` but the string returned is lazy which means it will be translated when it is used as an actual string. .. versionadded:: 0.7 """ return LazyString(pgettext, context, string, **variables)
def lazy_ngettext(self, singular, plural, num, **variables): """Like :func:`ngettext` but the string returned is lazy which means it will be translated when it is used as an actual string. Example:: apples = lazy_ngettext(u'%(num)d Apple', u'%(num)d Apples', num=len(apples)) @app.route('/') def index(): return unicode(apples) """ return LazyString(self.ngettext, singular, plural, num, **variables)
def lazy_gettext(string, **variables): """Like :func:`gettext` but the string returned is lazy which means it will be translated when it is used as an actual string. Example:: hello = lazy_gettext(u'Hello World') @app.route('/') def index(): return unicode(hello) """ return LazyString(gettext, string, **variables)
def lazy_formatter_gettext(string: str, lazy_formatter: Callable[[str], str], **variables: Any) -> LazyString: """Formats a lazy_gettext string with a custom function Example:: def custom_formatter(string: str) -> str: if current_app.config["CONDITIONAL_KEY"]: string += " . Condition key is on" return string hello = lazy_formatter_gettext(u'Hello World', custom_formatter) @app.route('/') def index(): return unicode(hello) """ return LazyString(_wrap_lazy_formatter_gettext, string, lazy_formatter, **variables)
def _l(string: str, /) -> str: """Like _() but the string returned is lazy which means it will be translated when it is used as an actual string. Positional-only arguments to simplify additional linting of localized strings.""" return LazyString(_, string)
def _l(string: str) -> str: """Like _() but the string returned is lazy which means it will be translated when it is used as an actual string.""" return LazyString(_, string)
def lazy_ngettext(*args, **kwargs): return LazyString(ngettext, *args, **kwargs)
def test_htmllib_integration(): assert escaping.escape_attribute("") == "" assert escaping.escape_text("") == "" @pytest.mark.parametrize( "inp,out", [ ('">alert(1)', "">alert(1)"), (None, ""), (1, "1"), (HTML('">alert(1)'), '">alert(1)'), (1.1, "1.1"), ("<", "<"), ("'", "'"), (LazyString(lambda: "'"), "'"), ], ) def test_escape_attribute(inp, out): assert escaping.escape_attribute(inp) == out @pytest.mark.parametrize( "inp,out", [ ("">alert(1)", '">alert(1)'), ("<", "<"), ], ) def test_unescape_attribute(inp, out): assert escaping.unescape_attributes(inp) == out