Domain#

class quart_babel.Domain(dirname: str | PathLike[str] | None = None, domain: str = 'messages')#

Localization domain. By default it will look for tranlations in the Quart application directory and “messages” domain - all message catalogs should be called messages.mo.

property as_default: None#

Set this domain as the default one for the current request.

property translations_cache: dict#

Returns a dictionary like object for translation caching.

get_translations_path(app: Quart) str | os.PathLike[str]#

Returns the translations directory path. Override if you want to implement custom behavior.

Parameters:

app – The Quart application.

property translations: Translations | NullTranslations#

Returns the correct gettext translations that should be used. This will never fail and return a dummy translation object if used outside of the app context or if a translation cannot be found.

gettext(string: str, **variables: Any) str#

Translates a string with the current locale and passes in the given keyword arguments as mapping to a string formatting string:

gettext(‘Hello World!’) gettext(‘Hello %(name)s!’, name=’World’)

Parameters:
  • string – The string to translate.

  • variables – kwargs for the translation.

ngettext(singular: str, plural: str, num: int, **variables: Any) str#

Translates a string with the current locale and passes in the given keyword arguments as mapping to a string formatting string. The num parameter is used to dispatch between singular and various plural forms of the message. It is available in the format string as %(num)d or %(num)s. The source language should be English or a similar language which only has one plural form:

ngettext(‘%(num)d Apple’, ‘%(num)d Apples’, num=len(apples))

Parameters:
  • singular – The singular string of the text.

  • plural – the plural string of the text.

  • num – The number parameter.

  • variables – kwargs variables for the translation.

pgettext(context: str, string: str, **variables: Any) str#

Like gettext() but with a context.

Gettext uses the msgctxt notation to distinguish different contexts for the same msgid

For example:

pgettext('Button label', 'Log in')

Learn more about contexts here: https://www.gnu.org/software/gettext/manual/html_node/Contexts.html

Parameters:
  • context – The context to use.

  • string – The string to use.

  • variables – Kwargs variables for the translation.

npgettext(context: str, singular: str, plural: str, num: int, **variables: Any) str#

Like ngettext() but with a context.

Parameters:
  • context – The context to use.

  • singular – The singular string of the text.

  • plural – the plural string of the text.

  • num – The number parameter.

  • variables – Kwargs variables for the translation.

lazy_gettext(string: str, **variables: Any) LazyString#

Like 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('Hello World')

@app.route('/')
async def index():
    return hello
Parameters:
  • string – The string to translate.

  • variables – kwargs for the translation.

lazy_ngettext(singular: str, plural: str, num: int, **variables: Any) LazyString#

Like ngettext() but the string returned is lazy which means it will be translated when it is used as an actual string.

Example:

a = lazy_ngettext('%(num)d Apple', '%(num)d Apples', num=len(a))

@app.route('/')
async def index():
    return a
Parameters:
  • singular – The singular string of the text.

  • plural – the plural string of the text.

  • num – The number parameter.

  • variables – kwargs variables for the translation.

lazy_pgettext(context: str, string: str, **variables: Any) LazyString#

Like pgettext() but the string returned is lazy which means it will be translated when it is used as an actual string.

Parameters:
  • context – The context to use.

  • string – The string to use.

  • variables – Kwargs variables for the translation.

quart_babel.get_domain() Domain#

Return the correct translation domain that is used for this request.

This will return the default domain.

Part of the internal API.

e.g. “messages” in <approot>/translations” if none is set for this request.

quart_babel.gettext(string: str, **variables: Any) str#

Shortcut to gettext for the default domain.

Translates a string with the current locale and passes in the given keyword arguments as mapping to a string formatting string:

gettext(‘Hello World!’) gettext(‘Hello %(name)s!’, name=’World’)

Parameters:
  • string – The string to translate.

  • variables – kwargs for the translation.

quart_babel.ngettext(singular: str, plural: str, num: int, **variables: Any) str#

Shortcut to ngettext for the default domain.

Translates a string with the current locale and passes in the given keyword arguments as mapping to a string formatting string. The num parameter is used to dispatch between singular and various plural forms of the message. It is available in the format string as %(num)d or %(num)s. The source language should be English or a similar language which only has one plural form:

ngettext(‘%(num)d Apple’, ‘%(num)d Apples’, num=len(apples))

Parameters:
  • singular – The singular string of the text.

  • plural – the plural string of the text.

  • num – The number parameter.

  • variables – kwargs variables for the translation.

quart_babel.npgettext(context: str, singular: str, plural: str, num: int, **variables: Any) str#

Shortcut to npgettext for the default domain.

Parameters:
  • context – The context to use.

  • singular – The singular string of the text.

  • plural – The plural string of the text.

  • num – The number parameter.

  • variables – Kwargs variables for the translation.

quart_babel.pgettext(context: str, string: str, **variables: Any) str#

Shortcut to pgettext for the default domain.

Like gettext() but with a context.

Gettext uses the msgctxt notation to distinguish different contexts for the same msgid

For example::

pgettext(‘Button label’, ‘Log in’)

Learn more about contexts here: https://www.gnu.org/software/gettext/manual/html_node/Contexts.html

Parameters:
  • context – The context to use.

  • string – The string to use.

  • variables – Kwargs variables for the translation.

quart_babel.lazy_gettext(string: str, **variables: Any) LazyString#

Lazy gettext shorcut for the default domain.

Like 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('Hello World')

@app.route('/')
async def index():
    return hello
Parameters:
  • string – The string to translate.

  • variables – kwargs for the translation.

quart_babel.lazy_ngettext(singular: str, plural: str, num: int, **variables: Any) LazyString#

Lazy ngettext shorcut for the default domain.

Like ngettext() but the string returned is lazy which means it will be translated when it is used as an actual string.

Example:

a = lazy_ngettext('%(num)d Apple', '%(num)d Apples', num=len(a))

@app.route('/')
async def index():
    return a
Parameters:
  • context – The context to use.

  • singular – The singular string of the text.

  • plural – The plural string of the text.

  • num – The number parameter.

  • variables – Kwargs variables for the translation.

quart_babel.lazy_pgettext(context: str, string: str, **variables: Any) LazyString#

Lazy pgettext shorcut for the default domain.

Like pgettext() but the string returned is lazy which means it will be translated when it is used as an actual string.

Parameters:
  • context – The context to use.

  • singular – The string of the text.

  • variables – Kwargs variables for the translation.