# a mapping between absolute .mo file path and Translation object
def translation(domain, localedir=None, languages=None,
class_=None, fallback=False, codeset=None):
mofiles = find(domain, localedir, languages, all=True)
return NullTranslations()
raise OSError(ENOENT, 'No translation file found for domain', domain)
# Avoid opening, reading, and parsing the .mo file after it's been done
key = (class_, os.path.abspath(mofile))
t = _translations.get(key)
with open(mofile, 'rb') as fp:
t = _translations.setdefault(key, class_(fp))
# Copy the translation object to allow setting fallbacks and
# output charset. All other instance data is shared with the
t.set_output_charset(codeset)
def install(domain, localedir=None, codeset=None, names=None):
t = translation(domain, localedir, fallback=True, codeset=codeset)
# a mapping b/w domains and locale directories
# a mapping b/w domains and codesets
# current global domain, `messages' used for compatibility w/ GNU gettext
_current_domain = 'messages'
def textdomain(domain=None):
def bindtextdomain(domain, localedir=None):
if localedir is not None:
_localedirs[domain] = localedir
return _localedirs.get(domain, _default_localedir)
def bind_textdomain_codeset(domain, codeset=None):
_localecodesets[domain] = codeset
return _localecodesets.get(domain)
def dgettext(domain, message):
t = translation(domain, _localedirs.get(domain, None),
codeset=_localecodesets.get(domain))
return t.gettext(message)
def ldgettext(domain, message):
codeset = _localecodesets.get(domain)
t = translation(domain, _localedirs.get(domain, None), codeset=codeset)
return message.encode(codeset or locale.getpreferredencoding())
return t.lgettext(message)
def dngettext(domain, msgid1, msgid2, n):
t = translation(domain, _localedirs.get(domain, None),
codeset=_localecodesets.get(domain))
return t.ngettext(msgid1, msgid2, n)
def ldngettext(domain, msgid1, msgid2, n):
codeset = _localecodesets.get(domain)
t = translation(domain, _localedirs.get(domain, None), codeset=codeset)
return tmsg.encode(codeset or locale.getpreferredencoding())
return t.lngettext(msgid1, msgid2, n)
return dgettext(_current_domain, message)
return ldgettext(_current_domain, message)
def ngettext(msgid1, msgid2, n):
return dngettext(_current_domain, msgid1, msgid2, n)
def lngettext(msgid1, msgid2, n):
return ldngettext(_current_domain, msgid1, msgid2, n)
# dcgettext() has been deemed unnecessary and is not implemented.
# James Henstridge's Catalog constructor from GNOME gettext. Documented usage
# cat = gettext.Catalog(PACKAGE, localedir=LOCALEDIR)
# The resulting catalog object currently don't support access through a
# dictionary API, which was supported (but apparently unused) in GNOME