module documentation

Translation helper functions.
Constant CONTEXT​_SEPARATOR Undocumented
Variable accept​_language​_re Undocumented
Variable language​_code​_prefix​_re Undocumented
Variable language​_code​_re Undocumented
Class ​Django​Translation Set up the GNUTranslations context with regard to output charset.
Class ​Translation​Catalog 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 No summary
Function get​_languages Cache of settings.LANGUAGES in a dictionary for easy lookups by key.
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 No summary
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 lang_string, which is the body of an HTTP Accept-Language header, and return a tuple of (lang, q-value), ordered by 'q' values.
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.
Variable ​_active Undocumented
Variable ​_default Undocumented
Variable ​_translations Undocumented
CONTEXT_SEPARATOR: str =

Undocumented

Value
'\x04'
accept_language_re =

Undocumented

language_code_prefix_re =

Undocumented

language_code_re =

Undocumented

def activate(language):
Fetch the translation object for a given language and install it as the current translation object for the current thread.
def all_locale_paths():
Return a list of paths to user-provides languages files.
def 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.
@functools.lru_cache(maxsize=1000)
def check_for_language(lang_code):

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():
Uninstall the active translation object so that further _() calls resolve to the default translation object.
def 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.
def do_ntranslate(singular, plural, number, translation_function):

Undocumented

def get_language():
Return the currently selected language.
def get_language_bidi():

Return selected language's BiDi layout.

  • False = left-to-right layout
  • True = right-to-left layout
def get_language_from_path(path, strict=False):

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):

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():
Cache of settings.LANGUAGES in a dictionary for easy lookups by key.
@functools.lru_cache(maxsize=1000)
def get_supported_language_variant(lang_code, strict=False):

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):
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):
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):
Return a string of the translation of either the singular or plural, based on the number.
def npgettext(context, singular, plural, number):

Undocumented

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

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.

def pgettext(context, message):

Undocumented

@receiver(setting_changed)
def reset_cache(**kwargs):
Reset global state when LANGUAGES setting has been changed, as some languages should no longer be accepted.
def translation(language):
Return a translation object in the default 'django' domain.
_active =

Undocumented

_default =

Undocumented

_translations: dict =

Undocumented