2000-08-26 03:13:37 +08:00
|
|
|
"""Internationalization and localization support.
|
|
|
|
|
|
|
|
This module provides internationalization (I18N) and localization (L10N)
|
|
|
|
support for your Python programs by providing an interface to the GNU gettext
|
|
|
|
message catalog library.
|
|
|
|
|
|
|
|
I18N refers to the operation by which a program is made aware of multiple
|
|
|
|
languages. L10N refers to the adaptation of your program, once
|
2000-08-30 11:29:58 +08:00
|
|
|
internationalized, to the local language and cultural habits.
|
2000-08-26 03:13:37 +08:00
|
|
|
|
|
|
|
"""
|
|
|
|
|
2000-08-26 04:26:43 +08:00
|
|
|
# This module represents the integration of work, contributions, feedback, and
|
|
|
|
# suggestions from the following people:
|
2000-08-26 03:13:37 +08:00
|
|
|
#
|
|
|
|
# Martin von Loewis, who wrote the initial implementation of the underlying
|
|
|
|
# C-based libintlmodule (later renamed _gettext), along with a skeletal
|
|
|
|
# gettext.py implementation.
|
|
|
|
#
|
|
|
|
# Peter Funk, who wrote fintl.py, a fairly complete wrapper around intlmodule,
|
|
|
|
# which also included a pure-Python implementation to read .mo files if
|
|
|
|
# intlmodule wasn't available.
|
|
|
|
#
|
|
|
|
# James Henstridge, who also wrote a gettext.py module, which has some
|
|
|
|
# interesting, but currently unsupported experimental features: the notion of
|
|
|
|
# a Catalog class and instances, and the ability to add to a catalog file via
|
|
|
|
# a Python API.
|
|
|
|
#
|
|
|
|
# Barry Warsaw integrated these modules, wrote the .install() API and code,
|
|
|
|
# and conformed all C and Python code to Python's coding standards.
|
2000-08-30 11:29:58 +08:00
|
|
|
#
|
|
|
|
# Francois Pinard and Marc-Andre Lemburg also contributed valuably to this
|
|
|
|
# module.
|
|
|
|
#
|
2003-03-11 00:01:43 +08:00
|
|
|
# J. David Ibanez implemented plural forms. Bruno Haible fixed some bugs.
|
2002-11-22 05:45:32 +08:00
|
|
|
#
|
2000-08-30 11:29:58 +08:00
|
|
|
# TODO:
|
|
|
|
# - Lazy loading of .mo files. Currently the entire catalog is loaded into
|
|
|
|
# memory, but that's probably bad for large translated programs. Instead,
|
|
|
|
# the lexical sort of original strings in GNU .mo files should be exploited
|
|
|
|
# to do binary searches and lazy initializations. Or you might want to use
|
|
|
|
# the undocumented double-hash algorithm for .mo files with hash tables, but
|
|
|
|
# you'll need to study the GNU gettext code to do this.
|
|
|
|
#
|
|
|
|
# - Support Solaris .mo file formats. Unfortunately, we've been unable to
|
|
|
|
# find this format documented anywhere.
|
2000-08-26 03:13:37 +08:00
|
|
|
|
2002-11-22 05:45:32 +08:00
|
|
|
|
|
|
|
import copy, os, re, struct, sys
|
2000-08-30 11:29:58 +08:00
|
|
|
from errno import ENOENT
|
2000-08-26 03:13:37 +08:00
|
|
|
|
2002-11-22 05:45:32 +08:00
|
|
|
|
2001-01-23 23:35:05 +08:00
|
|
|
__all__ = ["bindtextdomain","textdomain","gettext","dgettext",
|
|
|
|
"find","translation","install","Catalog"]
|
|
|
|
|
2000-08-30 11:29:58 +08:00
|
|
|
_default_localedir = os.path.join(sys.prefix, 'share', 'locale')
|
2000-08-26 03:13:37 +08:00
|
|
|
|
|
|
|
|
2002-11-22 05:45:32 +08:00
|
|
|
def test(condition, true, false):
|
|
|
|
"""
|
|
|
|
Implements the C expression:
|
|
|
|
|
|
|
|
condition ? true : false
|
|
|
|
|
|
|
|
Required to correctly interpret plural forms.
|
|
|
|
"""
|
|
|
|
if condition:
|
|
|
|
return true
|
|
|
|
else:
|
|
|
|
return false
|
|
|
|
|
|
|
|
|
|
|
|
def c2py(plural):
|
|
|
|
"""
|
|
|
|
Gets a C expression as used in PO files for plural forms and
|
|
|
|
returns a Python lambda function that implements an equivalent
|
|
|
|
expression.
|
|
|
|
"""
|
|
|
|
# Security check, allow only the "n" identifier
|
|
|
|
from StringIO import StringIO
|
|
|
|
import token, tokenize
|
|
|
|
tokens = tokenize.generate_tokens(StringIO(plural).readline)
|
2003-03-11 00:01:43 +08:00
|
|
|
try:
|
|
|
|
danger = [ x for x in tokens if x[0] == token.NAME and x[1] != 'n' ]
|
|
|
|
except tokenize.TokenError:
|
|
|
|
raise ValueError, \
|
|
|
|
'plural forms expression error, maybe unbalanced parenthesis'
|
|
|
|
else:
|
|
|
|
if danger:
|
|
|
|
raise ValueError, 'plural forms expression could be dangerous'
|
2002-11-22 05:45:32 +08:00
|
|
|
|
|
|
|
# Replace some C operators by their Python equivalents
|
|
|
|
plural = plural.replace('&&', ' and ')
|
|
|
|
plural = plural.replace('||', ' or ')
|
|
|
|
|
2003-03-11 00:01:43 +08:00
|
|
|
expr = re.compile(r'\!([^=])')
|
|
|
|
plural = expr.sub(' not \\1', plural)
|
2002-11-22 05:45:32 +08:00
|
|
|
|
|
|
|
# Regular expression and replacement function used to transform
|
|
|
|
# "a?b:c" to "test(a,b,c)".
|
|
|
|
expr = re.compile(r'(.*?)\?(.*?):(.*)')
|
|
|
|
def repl(x):
|
|
|
|
return "test(%s, %s, %s)" % (x.group(1), x.group(2),
|
|
|
|
expr.sub(repl, x.group(3)))
|
|
|
|
|
|
|
|
# Code to transform the plural expression, taking care of parentheses
|
|
|
|
stack = ['']
|
|
|
|
for c in plural:
|
|
|
|
if c == '(':
|
|
|
|
stack.append('')
|
|
|
|
elif c == ')':
|
2003-03-11 00:01:43 +08:00
|
|
|
if len(stack) == 1:
|
|
|
|
# Actually, we never reach this code, because unbalanced
|
|
|
|
# parentheses get caught in the security check at the
|
|
|
|
# beginning.
|
2002-11-22 05:45:32 +08:00
|
|
|
raise ValueError, 'unbalanced parenthesis in plural form'
|
|
|
|
s = expr.sub(repl, stack.pop())
|
|
|
|
stack[-1] += '(%s)' % s
|
|
|
|
else:
|
|
|
|
stack[-1] += c
|
|
|
|
plural = expr.sub(repl, stack.pop())
|
|
|
|
|
|
|
|
return eval('lambda n: int(%s)' % plural)
|
|
|
|
|
|
|
|
|
2001-01-15 07:47:14 +08:00
|
|
|
|
2000-08-26 04:26:43 +08:00
|
|
|
def _expand_lang(locale):
|
|
|
|
from locale import normalize
|
|
|
|
locale = normalize(locale)
|
|
|
|
COMPONENT_CODESET = 1 << 0
|
|
|
|
COMPONENT_TERRITORY = 1 << 1
|
|
|
|
COMPONENT_MODIFIER = 1 << 2
|
|
|
|
# split up the locale into its base components
|
|
|
|
mask = 0
|
|
|
|
pos = locale.find('@')
|
|
|
|
if pos >= 0:
|
|
|
|
modifier = locale[pos:]
|
|
|
|
locale = locale[:pos]
|
|
|
|
mask |= COMPONENT_MODIFIER
|
|
|
|
else:
|
|
|
|
modifier = ''
|
|
|
|
pos = locale.find('.')
|
|
|
|
if pos >= 0:
|
|
|
|
codeset = locale[pos:]
|
|
|
|
locale = locale[:pos]
|
|
|
|
mask |= COMPONENT_CODESET
|
|
|
|
else:
|
|
|
|
codeset = ''
|
|
|
|
pos = locale.find('_')
|
|
|
|
if pos >= 0:
|
|
|
|
territory = locale[pos:]
|
|
|
|
locale = locale[:pos]
|
|
|
|
mask |= COMPONENT_TERRITORY
|
|
|
|
else:
|
|
|
|
territory = ''
|
|
|
|
language = locale
|
|
|
|
ret = []
|
|
|
|
for i in range(mask+1):
|
|
|
|
if not (i & ~mask): # if all components for this combo exist ...
|
|
|
|
val = language
|
|
|
|
if i & COMPONENT_TERRITORY: val += territory
|
|
|
|
if i & COMPONENT_CODESET: val += codeset
|
|
|
|
if i & COMPONENT_MODIFIER: val += modifier
|
|
|
|
ret.append(val)
|
|
|
|
ret.reverse()
|
|
|
|
return ret
|
|
|
|
|
|
|
|
|
2001-01-15 07:47:14 +08:00
|
|
|
|
2000-08-30 11:29:58 +08:00
|
|
|
class NullTranslations:
|
|
|
|
def __init__(self, fp=None):
|
|
|
|
self._info = {}
|
|
|
|
self._charset = None
|
2002-01-11 14:58:49 +08:00
|
|
|
self._fallback = None
|
2002-06-01 09:29:16 +08:00
|
|
|
if fp is not None:
|
2000-08-30 11:29:58 +08:00
|
|
|
self._parse(fp)
|
2000-08-26 03:13:37 +08:00
|
|
|
|
2000-08-30 11:29:58 +08:00
|
|
|
def _parse(self, fp):
|
|
|
|
pass
|
|
|
|
|
2002-01-11 14:58:49 +08:00
|
|
|
def add_fallback(self, fallback):
|
|
|
|
if self._fallback:
|
|
|
|
self._fallback.add_fallback(fallback)
|
|
|
|
else:
|
|
|
|
self._fallback = fallback
|
|
|
|
|
2000-08-30 11:29:58 +08:00
|
|
|
def gettext(self, message):
|
2002-01-11 14:58:49 +08:00
|
|
|
if self._fallback:
|
|
|
|
return self._fallback.gettext(message)
|
2000-08-30 11:29:58 +08:00
|
|
|
return message
|
|
|
|
|
2002-11-22 05:45:32 +08:00
|
|
|
def ngettext(self, msgid1, msgid2, n):
|
|
|
|
if self._fallback:
|
|
|
|
return self._fallback.ngettext(msgid1, msgid2, n)
|
|
|
|
if n == 1:
|
|
|
|
return msgid1
|
|
|
|
else:
|
|
|
|
return msgid2
|
|
|
|
|
2000-08-30 11:29:58 +08:00
|
|
|
def ugettext(self, message):
|
2002-01-11 14:58:49 +08:00
|
|
|
if self._fallback:
|
|
|
|
return self._fallback.ugettext(message)
|
2000-08-30 11:29:58 +08:00
|
|
|
return unicode(message)
|
|
|
|
|
2002-11-22 05:45:32 +08:00
|
|
|
def ungettext(self, msgid1, msgid2, n):
|
|
|
|
if self._fallback:
|
|
|
|
return self._fallback.ungettext(msgid1, msgid2, n)
|
|
|
|
if n == 1:
|
|
|
|
return unicode(msgid1)
|
|
|
|
else:
|
|
|
|
return unicode(msgid2)
|
|
|
|
|
2000-08-30 11:29:58 +08:00
|
|
|
def info(self):
|
|
|
|
return self._info
|
|
|
|
|
|
|
|
def charset(self):
|
|
|
|
return self._charset
|
|
|
|
|
|
|
|
def install(self, unicode=0):
|
|
|
|
import __builtin__
|
|
|
|
__builtin__.__dict__['_'] = unicode and self.ugettext or self.gettext
|
|
|
|
|
|
|
|
|
|
|
|
class GNUTranslations(NullTranslations):
|
|
|
|
# Magic number of .mo files
|
2002-08-14 23:09:12 +08:00
|
|
|
LE_MAGIC = 0x950412deL
|
|
|
|
BE_MAGIC = 0xde120495L
|
2000-08-26 03:13:37 +08:00
|
|
|
|
|
|
|
def _parse(self, fp):
|
|
|
|
"""Override this method to support alternative .mo formats."""
|
|
|
|
unpack = struct.unpack
|
|
|
|
filename = getattr(fp, 'name', '')
|
|
|
|
# Parse the .mo file header, which consists of 5 little endian 32
|
|
|
|
# bit words.
|
2000-08-30 11:29:58 +08:00
|
|
|
self._catalog = catalog = {}
|
2003-03-11 00:01:43 +08:00
|
|
|
self.plural = lambda n: int(n != 1) # germanic plural by default
|
2000-08-26 03:13:37 +08:00
|
|
|
buf = fp.read()
|
2000-09-01 07:28:52 +08:00
|
|
|
buflen = len(buf)
|
2000-08-30 11:29:58 +08:00
|
|
|
# Are we big endian or little endian?
|
2002-08-14 23:09:12 +08:00
|
|
|
magic = unpack('<I', buf[:4])[0]
|
2000-08-30 11:29:58 +08:00
|
|
|
if magic == self.LE_MAGIC:
|
2002-08-14 23:09:12 +08:00
|
|
|
version, msgcount, masteridx, transidx = unpack('<4I', buf[4:20])
|
|
|
|
ii = '<II'
|
2000-08-30 11:29:58 +08:00
|
|
|
elif magic == self.BE_MAGIC:
|
2002-08-14 23:09:12 +08:00
|
|
|
version, msgcount, masteridx, transidx = unpack('>4I', buf[4:20])
|
|
|
|
ii = '>II'
|
2000-08-30 11:29:58 +08:00
|
|
|
else:
|
2000-08-26 03:13:37 +08:00
|
|
|
raise IOError(0, 'Bad magic number', filename)
|
|
|
|
# Now put all messages from the .mo file buffer into the catalog
|
|
|
|
# dictionary.
|
|
|
|
for i in xrange(0, msgcount):
|
2000-08-30 11:29:58 +08:00
|
|
|
mlen, moff = unpack(ii, buf[masteridx:masteridx+8])
|
2002-08-14 23:09:12 +08:00
|
|
|
mend = moff + mlen
|
2000-08-30 11:29:58 +08:00
|
|
|
tlen, toff = unpack(ii, buf[transidx:transidx+8])
|
2002-08-14 23:09:12 +08:00
|
|
|
tend = toff + tlen
|
2000-09-01 07:28:52 +08:00
|
|
|
if mend < buflen and tend < buflen:
|
2002-11-22 05:45:32 +08:00
|
|
|
msg = buf[moff:mend]
|
2000-08-30 11:29:58 +08:00
|
|
|
tmsg = buf[toff:tend]
|
2002-11-22 05:45:32 +08:00
|
|
|
if msg.find('\x00') >= 0:
|
|
|
|
# Plural forms
|
|
|
|
msgid1, msgid2 = msg.split('\x00')
|
|
|
|
tmsg = tmsg.split('\x00')
|
|
|
|
for i in range(len(tmsg)):
|
|
|
|
catalog[(msgid1, i)] = tmsg[i]
|
|
|
|
else:
|
|
|
|
catalog[msg] = tmsg
|
2000-08-26 03:13:37 +08:00
|
|
|
else:
|
|
|
|
raise IOError(0, 'File is corrupt', filename)
|
2000-08-30 11:29:58 +08:00
|
|
|
# See if we're looking at GNU .mo conventions for metadata
|
2003-03-11 00:01:43 +08:00
|
|
|
if mlen == 0:
|
2000-08-30 11:29:58 +08:00
|
|
|
# Catalog description
|
|
|
|
for item in tmsg.split('\n'):
|
|
|
|
item = item.strip()
|
|
|
|
if not item:
|
|
|
|
continue
|
|
|
|
k, v = item.split(':', 1)
|
|
|
|
k = k.strip().lower()
|
|
|
|
v = v.strip()
|
|
|
|
self._info[k] = v
|
|
|
|
if k == 'content-type':
|
|
|
|
self._charset = v.split('charset=')[1]
|
2002-11-22 05:45:32 +08:00
|
|
|
elif k == 'plural-forms':
|
|
|
|
v = v.split(';')
|
|
|
|
## nplurals = v[0].split('nplurals=')[1]
|
|
|
|
## nplurals = int(nplurals.strip())
|
|
|
|
plural = v[1].split('plural=')[1]
|
|
|
|
self.plural = c2py(plural)
|
2000-08-26 03:13:37 +08:00
|
|
|
# advance to next entry in the seek tables
|
2000-08-26 04:26:43 +08:00
|
|
|
masteridx += 8
|
|
|
|
transidx += 8
|
2000-08-26 03:13:37 +08:00
|
|
|
|
2000-08-30 11:29:58 +08:00
|
|
|
def gettext(self, message):
|
2002-01-11 14:58:49 +08:00
|
|
|
try:
|
|
|
|
return self._catalog[message]
|
|
|
|
except KeyError:
|
|
|
|
if self._fallback:
|
|
|
|
return self._fallback.gettext(message)
|
|
|
|
return message
|
2000-08-30 11:29:58 +08:00
|
|
|
|
2002-11-22 05:45:32 +08:00
|
|
|
|
|
|
|
def ngettext(self, msgid1, msgid2, n):
|
|
|
|
try:
|
|
|
|
return self._catalog[(msgid1, self.plural(n))]
|
|
|
|
except KeyError:
|
|
|
|
if self._fallback:
|
|
|
|
return self._fallback.ngettext(msgid1, msgid2, n)
|
|
|
|
if n == 1:
|
|
|
|
return msgid1
|
|
|
|
else:
|
|
|
|
return msgid2
|
|
|
|
|
|
|
|
|
2000-08-30 11:29:58 +08:00
|
|
|
def ugettext(self, message):
|
2002-01-11 14:58:49 +08:00
|
|
|
try:
|
|
|
|
tmsg = self._catalog[message]
|
|
|
|
except KeyError:
|
|
|
|
if self._fallback:
|
|
|
|
return self._fallback.ugettext(message)
|
|
|
|
tmsg = message
|
2000-08-30 11:29:58 +08:00
|
|
|
return unicode(tmsg, self._charset)
|
2000-08-26 03:13:37 +08:00
|
|
|
|
|
|
|
|
2002-11-22 05:45:32 +08:00
|
|
|
def ungettext(self, msgid1, msgid2, n):
|
|
|
|
try:
|
|
|
|
tmsg = self._catalog[(msgid1, self.plural(n))]
|
|
|
|
except KeyError:
|
|
|
|
if self._fallback:
|
|
|
|
return self._fallback.ungettext(msgid1, msgid2, n)
|
|
|
|
if n == 1:
|
|
|
|
tmsg = msgid1
|
|
|
|
else:
|
|
|
|
tmsg = msgid2
|
|
|
|
return unicode(tmsg, self._charset)
|
|
|
|
|
2001-01-15 07:47:14 +08:00
|
|
|
|
2000-08-26 03:13:37 +08:00
|
|
|
# Locate a .mo file using the gettext strategy
|
2002-01-11 14:58:49 +08:00
|
|
|
def find(domain, localedir=None, languages=None, all=0):
|
2000-08-26 03:13:37 +08:00
|
|
|
# Get some reasonable defaults for arguments that were not supplied
|
|
|
|
if localedir is None:
|
2000-08-30 11:29:58 +08:00
|
|
|
localedir = _default_localedir
|
2000-08-26 03:13:37 +08:00
|
|
|
if languages is None:
|
|
|
|
languages = []
|
|
|
|
for envar in ('LANGUAGE', 'LC_ALL', 'LC_MESSAGES', 'LANG'):
|
|
|
|
val = os.environ.get(envar)
|
|
|
|
if val:
|
|
|
|
languages = val.split(':')
|
|
|
|
break
|
|
|
|
if 'C' not in languages:
|
|
|
|
languages.append('C')
|
2000-08-26 04:26:43 +08:00
|
|
|
# now normalize and expand the languages
|
2000-10-16 23:47:50 +08:00
|
|
|
nelangs = []
|
2000-08-26 04:26:43 +08:00
|
|
|
for lang in languages:
|
|
|
|
for nelang in _expand_lang(lang):
|
2000-10-16 23:47:50 +08:00
|
|
|
if nelang not in nelangs:
|
|
|
|
nelangs.append(nelang)
|
2000-08-26 03:13:37 +08:00
|
|
|
# select a language
|
2002-01-11 14:58:49 +08:00
|
|
|
if all:
|
|
|
|
result = []
|
|
|
|
else:
|
|
|
|
result = None
|
2000-10-16 23:47:50 +08:00
|
|
|
for lang in nelangs:
|
2000-08-26 03:13:37 +08:00
|
|
|
if lang == 'C':
|
|
|
|
break
|
2000-08-26 03:53:17 +08:00
|
|
|
mofile = os.path.join(localedir, lang, 'LC_MESSAGES', '%s.mo' % domain)
|
2000-08-30 11:29:58 +08:00
|
|
|
if os.path.exists(mofile):
|
2002-01-11 14:58:49 +08:00
|
|
|
if all:
|
|
|
|
result.append(mofile)
|
|
|
|
else:
|
|
|
|
return mofile
|
|
|
|
return result
|
2000-08-26 03:13:37 +08:00
|
|
|
|
|
|
|
|
2001-01-15 07:47:14 +08:00
|
|
|
|
2000-08-30 11:29:58 +08:00
|
|
|
# a mapping between absolute .mo file path and Translation object
|
|
|
|
_translations = {}
|
|
|
|
|
2002-01-11 14:33:28 +08:00
|
|
|
def translation(domain, localedir=None, languages=None,
|
|
|
|
class_=None, fallback=0):
|
2000-08-30 11:29:58 +08:00
|
|
|
if class_ is None:
|
|
|
|
class_ = GNUTranslations
|
2002-01-11 14:58:49 +08:00
|
|
|
mofiles = find(domain, localedir, languages, all=1)
|
|
|
|
if len(mofiles)==0:
|
2002-01-11 14:33:28 +08:00
|
|
|
if fallback:
|
|
|
|
return NullTranslations()
|
2000-08-30 11:29:58 +08:00
|
|
|
raise IOError(ENOENT, 'No translation file found for domain', domain)
|
|
|
|
# TBD: do we need to worry about the file pointer getting collected?
|
2000-10-06 02:48:12 +08:00
|
|
|
# Avoid opening, reading, and parsing the .mo file after it's been done
|
|
|
|
# once.
|
2002-01-11 14:58:49 +08:00
|
|
|
result = None
|
|
|
|
for mofile in mofiles:
|
|
|
|
key = os.path.abspath(mofile)
|
|
|
|
t = _translations.get(key)
|
|
|
|
if t is None:
|
|
|
|
t = _translations.setdefault(key, class_(open(mofile, 'rb')))
|
|
|
|
# Copy the translation object to allow setting fallbacks.
|
|
|
|
# All other instance data is shared with the cached object.
|
|
|
|
t = copy.copy(t)
|
|
|
|
if result is None:
|
|
|
|
result = t
|
|
|
|
else:
|
|
|
|
result.add_fallback(t)
|
|
|
|
return result
|
2000-08-30 11:29:58 +08:00
|
|
|
|
2001-01-15 07:47:14 +08:00
|
|
|
|
2000-08-30 11:29:58 +08:00
|
|
|
def install(domain, localedir=None, unicode=0):
|
2002-01-11 14:33:28 +08:00
|
|
|
translation(domain, localedir, fallback=1).install(unicode)
|
2000-08-30 11:29:58 +08:00
|
|
|
|
|
|
|
|
2001-01-15 07:47:14 +08:00
|
|
|
|
2000-08-30 11:29:58 +08:00
|
|
|
# a mapping b/w domains and locale directories
|
|
|
|
_localedirs = {}
|
|
|
|
# current global domain, `messages' used for compatibility w/ GNU gettext
|
|
|
|
_current_domain = 'messages'
|
2000-08-26 03:13:37 +08:00
|
|
|
|
|
|
|
|
|
|
|
def textdomain(domain=None):
|
|
|
|
global _current_domain
|
2000-08-30 11:29:58 +08:00
|
|
|
if domain is not None:
|
2000-08-26 03:13:37 +08:00
|
|
|
_current_domain = domain
|
2000-08-30 11:29:58 +08:00
|
|
|
return _current_domain
|
2000-08-26 03:13:37 +08:00
|
|
|
|
|
|
|
|
2000-08-30 11:29:58 +08:00
|
|
|
def bindtextdomain(domain, localedir=None):
|
|
|
|
global _localedirs
|
|
|
|
if localedir is not None:
|
|
|
|
_localedirs[domain] = localedir
|
|
|
|
return _localedirs.get(domain, _default_localedir)
|
2000-08-26 03:13:37 +08:00
|
|
|
|
|
|
|
|
|
|
|
def dgettext(domain, message):
|
2000-08-30 11:29:58 +08:00
|
|
|
try:
|
|
|
|
t = translation(domain, _localedirs.get(domain, None))
|
|
|
|
except IOError:
|
|
|
|
return message
|
|
|
|
return t.gettext(message)
|
2001-01-15 07:47:14 +08:00
|
|
|
|
2000-08-26 03:13:37 +08:00
|
|
|
|
2002-11-22 05:45:32 +08:00
|
|
|
def dngettext(domain, msgid1, msgid2, n):
|
|
|
|
try:
|
|
|
|
t = translation(domain, _localedirs.get(domain, None))
|
|
|
|
except IOError:
|
|
|
|
if n == 1:
|
|
|
|
return msgid1
|
|
|
|
else:
|
|
|
|
return msgid2
|
|
|
|
return t.ngettext(msgid1, msgid2, n)
|
|
|
|
|
|
|
|
|
2000-08-30 11:29:58 +08:00
|
|
|
def gettext(message):
|
|
|
|
return dgettext(_current_domain, message)
|
2000-08-26 03:13:37 +08:00
|
|
|
|
|
|
|
|
2002-11-22 05:45:32 +08:00
|
|
|
def ngettext(msgid1, msgid2, n):
|
|
|
|
return dngettext(_current_domain, msgid1, msgid2, n)
|
|
|
|
|
|
|
|
|
2000-08-30 11:29:58 +08:00
|
|
|
# dcgettext() has been deemed unnecessary and is not implemented.
|
2000-08-26 03:13:37 +08:00
|
|
|
|
2000-08-30 11:29:58 +08:00
|
|
|
# James Henstridge's Catalog constructor from GNOME gettext. Documented usage
|
|
|
|
# was:
|
|
|
|
#
|
|
|
|
# import gettext
|
|
|
|
# cat = gettext.Catalog(PACKAGE, localedir=LOCALEDIR)
|
|
|
|
# _ = cat.gettext
|
|
|
|
# print _('Hello World')
|
2000-08-26 03:13:37 +08:00
|
|
|
|
2000-08-30 11:29:58 +08:00
|
|
|
# The resulting catalog object currently don't support access through a
|
|
|
|
# dictionary API, which was supported (but apparently unused) in GNOME
|
|
|
|
# gettext.
|
2000-08-26 03:13:37 +08:00
|
|
|
|
2000-08-30 11:29:58 +08:00
|
|
|
Catalog = translation
|