module documentation

Extension API for adding custom tags and behavior.
Class ​Extension No summary
Constant GETTEXT​_FUNCTIONS Undocumented
Class _​Comment​Finder No summary
Class _​Translations​Basic Undocumented
Class _​Translations​Context Undocumented
Class ​Auto​Escape​Extension Undocumented
Class ​Debug​Extension A {% debug %} tag that dumps the available variables, filters, and tests.
Class ​Expr​Stmt​Extension Adds a do tag to Jinja that works like the print statement just that it doesn't print the return value.
Class ​Internationalization​Extension This extension adds gettext support to Jinja.
Class ​Loop​Control​Extension Adds break and continue to the template engine.
Class ​With​Extension Undocumented
Function ​_gettext​_alias Undocumented
Function ​_make​_new​_gettext Undocumented
Function ​_make​_new​_ngettext Undocumented
Function ​_make​_new​_npgettext Undocumented
Function ​_make​_new​_pgettext Undocumented
Function babel​_extract Babel extraction method for Jinja templates.
Function extract​_from​_ast No summary
Variable _​Supported​Translations Undocumented
Variable ​_ws​_re Undocumented
GETTEXT_FUNCTIONS: t.Tuple[str, ...] =

Undocumented

Value
('_', 'gettext', 'ngettext', 'pgettext', 'npgettext')
@pass_context
def _gettext_alias(__context, *args, **kwargs):

Undocumented

Parameters
__context:ContextUndocumented
*args:t.AnyUndocumented
**kwargs:t.AnyUndocumented
Returns
t.Union[t.Any, Undefined]Undocumented
def _make_new_gettext(func):

Undocumented

Parameters
func:t.Callable[[str], str]Undocumented
Returns
t.Callable[..., str]Undocumented
def _make_new_ngettext(func):

Undocumented

Parameters
func:t.Callable[[str, str, int], str]Undocumented
Returns
t.Callable[..., str]Undocumented
def _make_new_npgettext(func):

Undocumented

Parameters
func:t.Callable[[str, str, str, int], str]Undocumented
Returns
t.Callable[..., str]Undocumented
def _make_new_pgettext(func):

Undocumented

Parameters
func:t.Callable[[str, str], str]Undocumented
Returns
t.Callable[..., str]Undocumented
def babel_extract(fileobj, keywords, comment_tags, options):

Babel extraction method for Jinja templates.

Changed in version 2.3: Basic support for translation comments was added. If comment_tags is now set to a list of keywords for extraction, the extractor will try to find the best preceding comment that begins with one of the keywords. For best results, make sure to not have more than one gettext call in one line of code and the matching comment in the same line or the line before.
Changed in version 2.5.1: The newstyle_gettext flag can be set to True to enable newstyle gettext calls.
Changed in version 2.7: A silent option can now be provided. If set to False template syntax errors are propagated instead of being ignored.
Parameters
fileobj:t.BinaryIOthe file-like object the messages should be extracted from
keywords:t.Sequence[str]a list of keywords (i.e. function names) that should be recognized as translation functions
comment​_tags:t.Sequence[str]a list of translator tags to search for and include in the results.
options:t.Dict[str, t.Any]a dictionary of additional options (optional)
Returns
t.Iterator[t.Tuple[int, str, t.Union[t.Optional[str], t.Tuple[t.Optional[str], ...]], t.List[str]]]an iterator over (lineno, funcname, message, comments) tuples. (comments will be empty currently)
def extract_from_ast(ast, gettext_functions=GETTEXT_FUNCTIONS, babel_style=True):

Extract localizable strings from the given template node. Per default this function returns matches in babel style that means non string parameters as well as keyword arguments are returned as None. This allows Babel to figure out what you really meant if you are using gettext functions that allow keyword arguments for placeholder expansion. If you don't want that behavior set the babel_style parameter to False which causes only strings to be returned and parameters are always stored in tuples. As a consequence invalid gettext calls (calls without a single string parameter or string parameters after non-string parameters) are skipped.

This example explains the behavior:

>>> from jinja2 import Environment
>>> env = Environment()
>>> node = env.parse('{{ (_("foo"), _(), ngettext("foo", "bar", 42)) }}')
>>> list(extract_from_ast(node))
[(1, '_', 'foo'), (1, '_', ()), (1, 'ngettext', ('foo', 'bar', None))]
>>> list(extract_from_ast(node, babel_style=False))
[(1, '_', ('foo',)), (1, 'ngettext', ('foo', 'bar'))]

For every string found this function yields a (lineno, function, message) tuple, where:

  • lineno is the number of the line on which the string was found,
  • function is the name of the gettext function used (if the string was extracted from embedded Python code), and
  • message is the string, or a tuple of strings for functions
    with multiple string arguments.

This extraction function operates on the AST and is because of that unable to extract any comments. For comment support you have to use the babel extraction interface or extract comments yourself.

Parameters
ast:nodes.TemplateUndocumented
gettext​_functions:t.Sequence[str]Undocumented
babel​_style:boolUndocumented
Returns
t.Iterator[t.Tuple[int, str, t.Union[t.Optional[str], t.Tuple[t.Optional[str], ...]]]]Undocumented
_SupportedTranslations =

Undocumented

_ws_re =

Undocumented