3
4\0X @ sv d Z ddlmZmZ ddlmZmZ edddgeddged d
ddggZd
d Z dd Z
dd Zdd Ze Z
dS )z
babel.messages.checkers
~~~~~~~~~~~~~~~~~~~~~~~
Various routines that help with validation of translations.
:since: version 0.9
:copyright: (c) 2013 by the Babel Team.
:license: BSD, see LICENSE for more details.
)TranslationError
PYTHON_FORMAT)string_typesizipiduxXfFgGc C sd |j st|jtstddS | dkr*dS |j}t|ttfsD|f}t|| jkr`td| j dS )z0Verify the number of plurals in the translation.z/Found plural forms for non-pluralizable messageNz*Wrong number of plural forms (expected %d)) Zpluralizable
isinstancestringr r listtuplelennum_plurals)catalogmessagemsgstrs r /usr/lib/python3.6/checkers.pyr s r c C sl d|j krdS |j}t|ttfs(|f}|j}t|ttfsB|f}x$t||D ]\}}|rNt|| qNW dS )z9Verify the format string placeholders in the translation.z
python-formatN)flagsidr r r r r _validate_format)r r Zmsgidsr ZmsgidZmsgstrr r r
python_format. s
r c C s$ dd }dd }dd }t || |f\}}t |||f\}}|rV| rV| rVtdn||krftd|rt|t|krtd xtt||D ]4\} \\}
}\}
}|||std
| d ||f qW nTt|}
xJ|D ]B\}}||
krtd| q|||
| std
|||
| f qW dS )a Test format string `alternative` against `format`. `format` can be the
msgid of a message and `alternative` one of the `msgstr`\s. The two
arguments are not interchangeable as `alternative` may contain less
placeholders if `format` uses named placeholders.
The behavior of this function is undefined if the string does not use
string formattings.
If the string formatting of `alternative` is compatible to `format` the
function returns `None`, otherwise a `TranslationError` is raised.
Examples for compatible format strings:
>>> _validate_format('Hello %s!', 'Hallo %s!')
>>> _validate_format('Hello %i!', 'Hallo %d!')
Example for an incompatible format strings:
>>> _validate_format('Hello %(name)s!', 'Hallo %s!')
Traceback (most recent call last):
...
TranslationError: the format strings are of different kinds
This function is used by the `python_format` checker.
:param format: The original format string
:param alternative: The alternative format string that should be checked
against format
:raises TranslationError: on formatting errors
c S sN g }xDt j| D ]6}|j \}}}|dkr4|d kr4q|j|t|f qW |S )N%)r finditergroupsappendstr)r resultmatchnameformattypecharr r r _parse^ s z _validate_format.