Parse a translatable tag.
(self, parser: "Parser")
| 352 | return extract_from_ast(source, gettext_functions) |
| 353 | |
| 354 | def parse(self, parser: "Parser") -> t.Union[nodes.Node, t.List[nodes.Node]]: |
| 355 | """Parse a translatable tag.""" |
| 356 | lineno = next(parser.stream).lineno |
| 357 | |
| 358 | context = None |
| 359 | context_token = parser.stream.next_if("string") |
| 360 | |
| 361 | if context_token is not None: |
| 362 | context = context_token.value |
| 363 | |
| 364 | # find all the variables referenced. Additionally a variable can be |
| 365 | # defined in the body of the trans block too, but this is checked at |
| 366 | # a later state. |
| 367 | plural_expr: t.Optional[nodes.Expr] = None |
| 368 | plural_expr_assignment: t.Optional[nodes.Assign] = None |
| 369 | num_called_num = False |
| 370 | variables: t.Dict[str, nodes.Expr] = {} |
| 371 | trimmed = None |
| 372 | while parser.stream.current.type != "block_end": |
| 373 | if variables: |
| 374 | parser.stream.expect("comma") |
| 375 | |
| 376 | # skip colon for python compatibility |
| 377 | if parser.stream.skip_if("colon"): |
| 378 | break |
| 379 | |
| 380 | token = parser.stream.expect("name") |
| 381 | if token.value in variables: |
| 382 | parser.fail( |
| 383 | f"translatable variable {token.value!r} defined twice.", |
| 384 | token.lineno, |
| 385 | exc=TemplateAssertionError, |
| 386 | ) |
| 387 | |
| 388 | # expressions |
| 389 | if parser.stream.current.type == "assign": |
| 390 | next(parser.stream) |
| 391 | variables[token.value] = var = parser.parse_expression() |
| 392 | elif trimmed is None and token.value in ("trimmed", "notrimmed"): |
| 393 | trimmed = token.value == "trimmed" |
| 394 | continue |
| 395 | else: |
| 396 | variables[token.value] = var = nodes.Name(token.value, "load") |
| 397 | |
| 398 | if plural_expr is None: |
| 399 | if isinstance(var, nodes.Call): |
| 400 | plural_expr = nodes.Name("_trans", "load") |
| 401 | variables[token.value] = plural_expr |
| 402 | plural_expr_assignment = nodes.Assign( |
| 403 | nodes.Name("_trans", "store"), var |
| 404 | ) |
| 405 | else: |
| 406 | plural_expr = var |
| 407 | num_called_num = token.value == "num" |
| 408 | |
| 409 | parser.stream.expect("block_end") |
| 410 | |
| 411 | plural = None |
nothing calls this directly
no test coverage detected