core.i18n

Module Contents

Classes

LanguageWrapper

Wrapper-class for a multi-language value.

translate

This class is the replacement for the old translate() function provided by ViUR2. This classes __init__

TranslationExtension

Default translation extension for jinja2 render.

Functions

initializeTranslations(→ None)

Fetches all translations from the datastore and populates the systemTranslations dictionary of this module.

localizedStrfTime(→ str)

Provides correct localized names for directives like %a which don't get translated on GAE properly as we can't

Attributes

systemTranslations

localizedDateTime

localizedDate

localizedTime

localizedAbbrevDayNames

localizedDayNames

localizedAbbrevMonthNames

localizedMonthNames

core.i18n.systemTranslations
class core.i18n.LanguageWrapper(languages: List[str] | Tuple[str])

Bases: dict

Wrapper-class for a multi-language value. It’s a dictionary, allowing accessing each stored language, but can also be used as a string, in which case it tries to guess the correct language.

__str__() str

Return str(self).

__bool__() bool
resolve() str | List[str]

Causes this wrapper to evaluate to the best language available for the current request.

Returns:

A item stored inside this instance or the empty string.

class core.i18n.translate(key: str, defaultText: str = None, hint: str = None)

This class is the replacement for the old translate() function provided by ViUR2. This classes __init__ takes the unique translation key (a string usually something like “user.auth_user_password.loginfailed” which uniquely defines this text fragment), a default text that will be used if no translation for this key has been added yet (in the projects default language) and a hint (an optional text that can convey context information for the persons translating these texts - they are not shown to the end-user). This class will resolve it’s translations upfront, so the actual resolving (by casting this class to string) is fast. This resolves most translation issues with bones, which can now take an instance of this class as it’s description/hints.

__slots__ = ['key', 'defaultText', 'hint', 'translationCache']
__repr__() str

Return repr(self).

__str__() str

Return str(self).

translate(**kwargs) str
class core.i18n.TranslationExtension(environment: jinja2.environment.Environment)

Bases: jinja2.ext.Extension

Default translation extension for jinja2 render. Use like {% translate “translationKey”, “defaultText”, “translationHint”, replaceValue1=”replacedText1” %} All except translationKey is optional. translationKey is the same Key supplied to _() before. defaultText will be printed if no translation is available. translationHint is a optional hint for anyone adding a now translation how/where that translation is used.

tags
parse(parser)

If any of the tags matched this method is called with the parser as first argument. The token the parser stream is pointing at is the name token that matched. This method has to return one or a list of multiple nodes.

_translate(key, defaultText, hint, kwargs, trDict, caller) str
core.i18n.initializeTranslations() None

Fetches all translations from the datastore and populates the systemTranslations dictionary of this module. Currently, the translate-class will resolve using that dictionary; but as we expect projects to grow and accumulate translations that are no longer/not yet used, we plan to made the translation-class fetch it’s translations directly from the datastore, so we don’t have to allocate memory for unused translations.

core.i18n.localizedDateTime
core.i18n.localizedDate
core.i18n.localizedTime
core.i18n.localizedAbbrevDayNames
core.i18n.localizedDayNames
core.i18n.localizedAbbrevMonthNames
core.i18n.localizedMonthNames
core.i18n.localizedStrfTime(datetimeObj: datetime.datetime, format: str) str

Provides correct localized names for directives like %a which don’t get translated on GAE properly as we can’t set the locale (for each request). This currently replaces %a, %A, %b, %B, %c, %x and %X.

Parameters:
  • datetimeObj – Datetime-instance to call strftime on

  • format – String containing the Format to apply.

Returns:

Date and time formatted according to format with correct localization