Python Glossary Русский

all Python 3gettextfunction

gettext.bindtextdomain()

global domain

Binds a translation domain to the directory holding <lang>/LC_MESSAGES/<domain>.mo files, for the module-level functions.

Syntax

path = gettext.bindtextdomain(domain, localedir=None)

The binding is process-wide and affects gettext(), dgettext() and friends; a library is better off holding its own translation() object. Build the directory path from the program's file rather than relative to the current folder, or running from elsewhere silently returns untranslated strings. Without localedir the function only reports the current binding.

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({'Hello': 'Привет'}))
os.environ['LANGUAGE'] = 'ru'
print(gettext.bindtextdomain('app', 'loc').endswith('loc'))  # → True
print(gettext.dgettext('app', 'Hello'))  # → Привет

See also

Open in the full glossarydocs.python.orgReport an inaccuracy