Python Glossary Русский

Python 3.8+gettextfunction

gettext.dpgettext()

contextual translation

A contextual translation from an explicitly named domain — dgettext() and pgettext() combined.

Syntax

text = gettext.dpgettext(domain, context, message, /)

The argument order is domain, context, message, and swapping the context and message is easy: there is no error, you just get untranslated text back. As with dgettext(), the domain must be bound with bindtextdomain(). Useful in a library that must not change the global textdomain().

Examples

import io, struct
def mo(cat):  # собрать каталог .mo в памяти
    cat = {'': 'Content-Type: text/plain; charset=UTF-8\n', **cat}
    ks = sorted(cat)
    bs = [k.encode() for k in ks] + [cat[k].encode() for k in ks]
    n, data, tab = len(ks), b'', b''
    for b in bs:
        tab += struct.pack('<2I', len(b), 28 + 16 * n + len(data))
        data += b + b'\0'
    return struct.pack('<7I', 0x950412de, 0, n, 28, 28 + 8 * n, 0, 0) + tab + data
import gettext
import os
os.makedirs('loc/ru/LC_MESSAGES', exist_ok=True)
open('loc/ru/LC_MESSAGES/app.mo', 'wb').write(mo({'menu\x04File': 'Файл'}))
os.environ['LANGUAGE'] = 'ru'
gettext.bindtextdomain('app', 'loc')
print(gettext.dpgettext('app', 'menu', 'File'))  # → Файл
print(gettext.dpgettext('app', 'File', 'menu'))  # → menu — аргументы перепутаны

See also

Open in the full glossarydocs.python.orgReport an inaccuracy