all Python 3gettextfunction
gettext.textdomain()
global domain
Sets or returns the current global domain from which the module-level gettext() and ngettext() take translations.
Syntax
name = gettext.textdomain(domain=None)
The default domain is called 'messages', so without textdomain gettext() looks for messages.mo rather than your program's catalog. The domain is shared by the whole process: changing it in one module changes translations everywhere. dgettext() picks a domain explicitly for a single call.
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.textdomain()) # → messages
gettext.bindtextdomain('app', 'loc')
gettext.textdomain('app')
print(gettext.gettext('Hello')) # → Привет
See also
Open in the full glossarydocs.python.orgReport an inaccuracy