Python Glossary Русский

all Python 3gettextfunction

gettext.dngettext()

translation functions

A plural-aware translation from an explicitly named domain: it picks the form according to the number n.

Syntax

text = gettext.dngettext(domain, singular, plural, n, /)

The singular and plural strings are the English sources; how many forms the translation has and which one to use is decided by the catalog's Plural-Forms header, which gives three for Russian. If the domain is not found, the English rule applies: 0 and 2 are plural. n must be an integer — 1.5 is an error.

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
H = 'Content-Type: text/plain; charset=UTF-8\nPlural-Forms: nplurals=3; plural=(n%10==1 && n%100!=11 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2);\n'
import gettext
import os
os.makedirs('loc/ru/LC_MESSAGES', exist_ok=True)
open('loc/ru/LC_MESSAGES/app.mo', 'wb').write(mo({'': H, 'apple\0apples': 'яблоко\0яблока\0яблок'}))
os.environ['LANGUAGE'] = 'ru'
gettext.bindtextdomain('app', 'loc')
print([gettext.dngettext('app', 'apple', 'apples', n) for n in (1, 2, 5)])  # → ['яблоко', 'яблока', 'яблок']
print(gettext.dngettext('none', 'apple', 'apples', 0))  # → apples

See also

Open in the full glossarydocs.python.orgReport an inaccuracy