module documentation

Translation helper functions.

Class DjangoTranslation Set up the GNUTranslations context with regard to output charset.
Class TranslationCatalog Simulate a dict for DjangoTranslation._catalog so as multiple catalogs with different plural equations are kept separate.
Function activate Fetch the translation object for a given language and install it as the current translation object for the current thread.
Function all_locale_paths Return a list of paths to user-provides languages files.
Function catalog Return the current active catalog for further processing. This can be used if you need to modify the catalog or want to access the whole message catalog instead of just translating one string.
Function check_for_language Check whether there is a global language file for the given language code. This is used to decide whether a user-provided language is available.
Function deactivate Uninstall the active translation object so that further _() calls resolve to the default translation object.
Function deactivate_all Make the active translation object a NullTranslations() instance. This is useful when we want delayed translations to appear as the original string for some reason.
Function do_ntranslate Undocumented
Function get_language Return the currently selected language.
Function get_language_bidi Return selected language's BiDi layout.
Function get_language_from_path Return the language code if there's a valid language code found in `path`.
Function get_language_from_request Analyze the request to find what language the user wants the system to show. Only languages listed in settings.LANGUAGES are taken into account. If the user requests a sublanguage where we have a main language, we send out the main language.
Function get_languages Cache of settings.LANGUAGES in a dictionary for easy lookups by key. Convert keys to lowercase as they should be treated as case-insensitive.
Function get_supported_language_variant Return the language code that's listed in supported languages, possibly selecting a more generic variant. Raise LookupError if nothing is found.
Function gettext Translate the 'message' string. It uses the current thread to find the translation object to use. If no current translation is activated, the message will be run through the default translation object.
Function gettext_noop Mark strings for translation but don't translate them now. This can be used to store strings in global variables that should stay in the base language (because they might be used externally) and will be translated later.
Function ngettext Return a string of the translation of either the singular or plural, based on the number.
Function npgettext Undocumented
Function parse_accept_lang_header Parse the value of the Accept-Language header up to a maximum length.
Function pgettext Undocumented
Function reset_cache Reset global state when LANGUAGES setting has been changed, as some languages should no longer be accepted.
Function translation Return a translation object in the default 'django' domain.
Constant ACCEPT_LANGUAGE_HEADER_MAX_LENGTH Undocumented
Constant CONTEXT_SEPARATOR Undocumented
Variable accept_language_re Undocumented
Variable language_code_prefix_re Undocumented
Variable language_code_re Undocumented
Function _parse_accept_lang_header Parse the lang_string, which is the body of an HTTP Accept-Language header, and return a tuple of (lang, q-value), ordered by 'q' values.
Variable _active Undocumented
Variable _default Undocumented
Variable _translations Undocumented
def activate(language): (source)

Fetch the translation object for a given language and install it as the current translation object for the current thread.

def all_locale_paths(): (source)

Return a list of paths to user-provides languages files.

def catalog(): (source)

Return the current active catalog for further processing. This can be used if you need to modify the catalog or want to access the whole message catalog instead of just translating one string.

@functools.lru_cache(maxsize=1000)
def check_for_language(lang_code): (source)

Check whether there is a global language file for the given language code. This is used to decide whether a user-provided language is available. lru_cache should have a maxsize to prevent from memory exhaustion attacks, as the provided language codes are taken from the HTTP request. See also <https://www.djangoproject.com/weblog/2007/oct/26/security-fix/>.

def deactivate(): (source)

Uninstall the active translation object so that further _() calls resolve to the default translation object.

def deactivate_all(): (source)

Make the active translation object a NullTranslations() instance. This is useful when we want delayed translations to appear as the original string for some reason.

def do_ntranslate(singular, plural, number, translation_function): (source)

Undocumented

def get_language(): (source)

Return the currently selected language.

def get_language_bidi(): (source)

Return selected language's BiDi layout. * False = left-to-right layout * True = right-to-left layout

def get_language_from_path(path, strict=False): (source)

Return the language code if there's a valid language code found in `path`. If `strict` is False (the default), look for a country-specific variant when neither the language code nor its generic variant is found.

def get_language_from_request(request, check_path=False): (source)

Analyze the request to find what language the user wants the system to show. Only languages listed in settings.LANGUAGES are taken into account. If the user requests a sublanguage where we have a main language, we send out the main language. If check_path is True, the URL path prefix will be checked for a language code, otherwise this is skipped for backwards compatibility.

@functools.lru_cache
def get_languages(): (source)

Cache of settings.LANGUAGES in a dictionary for easy lookups by key. Convert keys to lowercase as they should be treated as case-insensitive.

@functools.lru_cache(maxsize=1000)
def get_supported_language_variant(lang_code, strict=False): (source)

Return the language code that's listed in supported languages, possibly selecting a more generic variant. Raise LookupError if nothing is found. If `strict` is False (the default), look for a country-specific variant when neither the language code nor its generic variant is found. lru_cache should have a maxsize to prevent from memory exhaustion attacks, as the provided language codes are taken from the HTTP request. See also <https://www.djangoproject.com/weblog/2007/oct/26/security-fix/>.

def gettext(message): (source)

Translate the 'message' string. It uses the current thread to find the translation object to use. If no current translation is activated, the message will be run through the default translation object.

def gettext_noop(message): (source)

Mark strings for translation but don't translate them now. This can be used to store strings in global variables that should stay in the base language (because they might be used externally) and will be translated later.

def ngettext(singular, plural, number): (source)

Return a string of the translation of either the singular or plural, based on the number.

def npgettext(context, singular, plural, number): (source)

Undocumented

def parse_accept_lang_header(lang_string): (source)

Parse the value of the Accept-Language header up to a maximum length. The value of the header is truncated to a maximum length to avoid potential denial of service and memory exhaustion attacks. Excessive memory could be used if the raw value is very large as it would be cached due to the use of functools.lru_cache() to avoid repetitive parsing of common header values.

def pgettext(context, message): (source)

Undocumented

@receiver(setting_changed)
def reset_cache(*, setting, **kwargs): (source)

Reset global state when LANGUAGES setting has been changed, as some languages should no longer be accepted.

def translation(language): (source)

Return a translation object in the default 'django' domain.

ACCEPT_LANGUAGE_HEADER_MAX_LENGTH: int = (source)

Undocumented

Value
500
CONTEXT_SEPARATOR: str = (source)

Undocumented

Value
'\x04'
accept_language_re = (source)

Undocumented

language_code_prefix_re = (source)

Undocumented

language_code_re = (source)

Undocumented

@functools.lru_cache(maxsize=1000)
def _parse_accept_lang_header(lang_string): (source)

Parse the lang_string, which is the body of an HTTP Accept-Language header, and return a tuple of (lang, q-value), ordered by 'q' values. Return an empty tuple if there are any format errors in lang_string.

Undocumented

_default = (source)

Undocumented

_translations: dict = (source)

Undocumented