Python Glossary Русский

all Python 3gettextfunction

gettext.dgettext()

translation functions

Translates a string from an explicitly named domain rather than the current global one. Useful when a program has several catalogs.

Syntax

text = gettext.dgettext(domain, message, /)

The domain must be bound beforehand with bindtextdomain(), otherwise the search goes to the system locale directory and the string comes back untranslated — with no error. The language is chosen from the LANGUAGE, LC_ALL, LC_MESSAGES and LANG environment variables on each call, not from locale.setlocale().

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({'Save': 'Сохранить'}))
os.environ['LANGUAGE'] = 'ru'
gettext.bindtextdomain('app', 'loc')
print(gettext.dgettext('app', 'Save'))  # → Сохранить
print(gettext.dgettext('other', 'Save'))  # → Save

See also

Open in the full glossarydocs.python.orgReport an inaccuracy