123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539 |
- """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
- internationalized, to the local language and cultural habits.
- """
- import locale, copy, io, os, re, struct, sys
- from errno import ENOENT
- __all__ = ['NullTranslations', 'GNUTranslations', 'Catalog',
- 'find', 'translation', 'install', 'textdomain', 'bindtextdomain',
- 'bind_textdomain_codeset',
- 'dgettext', 'dngettext', 'gettext', 'lgettext', 'ldgettext',
- 'ldngettext', 'lngettext', 'ngettext',
- ]
- _default_localedir = os.path.join(sys.base_prefix, 'share', 'locale')
- 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.
- """
-
- import token, tokenize
- tokens = tokenize.generate_tokens(io.StringIO(plural).readline)
- 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')
-
- plural = plural.replace('&&', ' and ')
- plural = plural.replace('||', ' or ')
- expr = re.compile(r'\!([^=])')
- plural = expr.sub(' not \\1', plural)
-
-
- expr = re.compile(r'(.*?)\?(.*?):(.*)')
- def repl(x):
- return "(%s if %s else %s)" % (x.group(2), x.group(1),
- expr.sub(repl, x.group(3)))
-
- stack = ['']
- for c in plural:
- if c == '(':
- stack.append('')
- elif c == ')':
- if len(stack) == 1:
-
-
-
- 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)
- def _expand_lang(loc):
- loc = locale.normalize(loc)
- COMPONENT_CODESET = 1 << 0
- COMPONENT_TERRITORY = 1 << 1
- COMPONENT_MODIFIER = 1 << 2
-
- mask = 0
- pos = loc.find('@')
- if pos >= 0:
- modifier = loc[pos:]
- loc = loc[:pos]
- mask |= COMPONENT_MODIFIER
- else:
- modifier = ''
- pos = loc.find('.')
- if pos >= 0:
- codeset = loc[pos:]
- loc = loc[:pos]
- mask |= COMPONENT_CODESET
- else:
- codeset = ''
- pos = loc.find('_')
- if pos >= 0:
- territory = loc[pos:]
- loc = loc[:pos]
- mask |= COMPONENT_TERRITORY
- else:
- territory = ''
- language = loc
- ret = []
- for i in range(mask+1):
- if not (i & ~mask):
- 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
- class NullTranslations:
- def __init__(self, fp=None):
- self._info = {}
- self._charset = None
- self._output_charset = None
- self._fallback = None
- if fp is not None:
- self._parse(fp)
- def _parse(self, fp):
- pass
- def add_fallback(self, fallback):
- if self._fallback:
- self._fallback.add_fallback(fallback)
- else:
- self._fallback = fallback
- def gettext(self, message):
- if self._fallback:
- return self._fallback.gettext(message)
- return message
- def lgettext(self, message):
- if self._fallback:
- return self._fallback.lgettext(message)
- return message
- def ngettext(self, msgid1, msgid2, n):
- if self._fallback:
- return self._fallback.ngettext(msgid1, msgid2, n)
- if n == 1:
- return msgid1
- else:
- return msgid2
- def lngettext(self, msgid1, msgid2, n):
- if self._fallback:
- return self._fallback.lngettext(msgid1, msgid2, n)
- if n == 1:
- return msgid1
- else:
- return msgid2
- def info(self):
- return self._info
- def charset(self):
- return self._charset
- def output_charset(self):
- return self._output_charset
- def set_output_charset(self, charset):
- self._output_charset = charset
- def install(self, names=None):
- import builtins
- builtins.__dict__['_'] = self.gettext
- if hasattr(names, "__contains__"):
- if "gettext" in names:
- builtins.__dict__['gettext'] = builtins.__dict__['_']
- if "ngettext" in names:
- builtins.__dict__['ngettext'] = self.ngettext
- if "lgettext" in names:
- builtins.__dict__['lgettext'] = self.lgettext
- if "lngettext" in names:
- builtins.__dict__['lngettext'] = self.lngettext
- class GNUTranslations(NullTranslations):
-
- LE_MAGIC = 0x950412de
- BE_MAGIC = 0xde120495
-
- VERSIONS = (0, 1)
- def _get_versions(self, version):
- """Returns a tuple of major version, minor version"""
- return (version >> 16, version & 0xffff)
- def _parse(self, fp):
- """Override this method to support alternative .mo formats."""
- unpack = struct.unpack
- filename = getattr(fp, 'name', '')
-
-
- self._catalog = catalog = {}
- self.plural = lambda n: int(n != 1)
- buf = fp.read()
- buflen = len(buf)
-
- magic = unpack('<I', buf[:4])[0]
- if magic == self.LE_MAGIC:
- version, msgcount, masteridx, transidx = unpack('<4I', buf[4:20])
- ii = '<II'
- elif magic == self.BE_MAGIC:
- version, msgcount, masteridx, transidx = unpack('>4I', buf[4:20])
- ii = '>II'
- else:
- raise OSError(0, 'Bad magic number', filename)
- major_version, minor_version = self._get_versions(version)
- if major_version not in self.VERSIONS:
- raise OSError(0, 'Bad version number ' + str(major_version), filename)
-
-
- for i in range(0, msgcount):
- mlen, moff = unpack(ii, buf[masteridx:masteridx+8])
- mend = moff + mlen
- tlen, toff = unpack(ii, buf[transidx:transidx+8])
- tend = toff + tlen
- if mend < buflen and tend < buflen:
- msg = buf[moff:mend]
- tmsg = buf[toff:tend]
- else:
- raise OSError(0, 'File is corrupt', filename)
-
- if mlen == 0:
-
- lastk = None
- for b_item in tmsg.split('\n'.encode("ascii")):
- item = b_item.decode().strip()
- if not item:
- continue
- k = v = None
- if ':' in item:
- k, v = item.split(':', 1)
- k = k.strip().lower()
- v = v.strip()
- self._info[k] = v
- lastk = k
- elif lastk:
- self._info[lastk] += '\n' + item
- if k == 'content-type':
- self._charset = v.split('charset=')[1]
- elif k == 'plural-forms':
- v = v.split(';')
- plural = v[1].split('plural=')[1]
- self.plural = c2py(plural)
-
-
-
-
-
-
-
-
-
- charset = self._charset or 'ascii'
- if b'\x00' in msg:
-
- msgid1, msgid2 = msg.split(b'\x00')
- tmsg = tmsg.split(b'\x00')
- msgid1 = str(msgid1, charset)
- for i, x in enumerate(tmsg):
- catalog[(msgid1, i)] = str(x, charset)
- else:
- catalog[str(msg, charset)] = str(tmsg, charset)
-
- masteridx += 8
- transidx += 8
- def lgettext(self, message):
- missing = object()
- tmsg = self._catalog.get(message, missing)
- if tmsg is missing:
- if self._fallback:
- return self._fallback.lgettext(message)
- return message
- if self._output_charset:
- return tmsg.encode(self._output_charset)
- return tmsg.encode(locale.getpreferredencoding())
- def lngettext(self, msgid1, msgid2, n):
- try:
- tmsg = self._catalog[(msgid1, self.plural(n))]
- if self._output_charset:
- return tmsg.encode(self._output_charset)
- return tmsg.encode(locale.getpreferredencoding())
- except KeyError:
- if self._fallback:
- return self._fallback.lngettext(msgid1, msgid2, n)
- if n == 1:
- return msgid1
- else:
- return msgid2
- def gettext(self, message):
- missing = object()
- tmsg = self._catalog.get(message, missing)
- if tmsg is missing:
- if self._fallback:
- return self._fallback.gettext(message)
- return message
- return tmsg
- def ngettext(self, msgid1, msgid2, n):
- try:
- tmsg = self._catalog[(msgid1, self.plural(n))]
- except KeyError:
- if self._fallback:
- return self._fallback.ngettext(msgid1, msgid2, n)
- if n == 1:
- tmsg = msgid1
- else:
- tmsg = msgid2
- return tmsg
- def find(domain, localedir=None, languages=None, all=False):
-
- if localedir is None:
- localedir = _default_localedir
- 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')
-
- nelangs = []
- for lang in languages:
- for nelang in _expand_lang(lang):
- if nelang not in nelangs:
- nelangs.append(nelang)
-
- if all:
- result = []
- else:
- result = None
- for lang in nelangs:
- if lang == 'C':
- break
- mofile = os.path.join(localedir, lang, 'LC_MESSAGES', '%s.mo' % domain)
- if os.path.exists(mofile):
- if all:
- result.append(mofile)
- else:
- return mofile
- return result
- _translations = {}
- def translation(domain, localedir=None, languages=None,
- class_=None, fallback=False, codeset=None):
- if class_ is None:
- class_ = GNUTranslations
- mofiles = find(domain, localedir, languages, all=True)
- if not mofiles:
- if fallback:
- return NullTranslations()
- raise OSError(ENOENT, 'No translation file found for domain', domain)
-
-
- result = None
- for mofile in mofiles:
- key = (class_, os.path.abspath(mofile))
- t = _translations.get(key)
- if t is None:
- with open(mofile, 'rb') as fp:
- t = _translations.setdefault(key, class_(fp))
-
-
-
- t = copy.copy(t)
- if codeset:
- t.set_output_charset(codeset)
- if result is None:
- result = t
- else:
- result.add_fallback(t)
- return result
- def install(domain, localedir=None, codeset=None, names=None):
- t = translation(domain, localedir, fallback=True, codeset=codeset)
- t.install(names)
- _localedirs = {}
- _localecodesets = {}
- _current_domain = 'messages'
- def textdomain(domain=None):
- global _current_domain
- if domain is not None:
- _current_domain = domain
- return _current_domain
- def bindtextdomain(domain, localedir=None):
- global _localedirs
- if localedir is not None:
- _localedirs[domain] = localedir
- return _localedirs.get(domain, _default_localedir)
- def bind_textdomain_codeset(domain, codeset=None):
- global _localecodesets
- if codeset is not None:
- _localecodesets[domain] = codeset
- return _localecodesets.get(domain)
- def dgettext(domain, message):
- try:
- t = translation(domain, _localedirs.get(domain, None),
- codeset=_localecodesets.get(domain))
- except OSError:
- return message
- return t.gettext(message)
- def ldgettext(domain, message):
- try:
- t = translation(domain, _localedirs.get(domain, None),
- codeset=_localecodesets.get(domain))
- except OSError:
- return message
- return t.lgettext(message)
- def dngettext(domain, msgid1, msgid2, n):
- try:
- t = translation(domain, _localedirs.get(domain, None),
- codeset=_localecodesets.get(domain))
- except OSError:
- if n == 1:
- return msgid1
- else:
- return msgid2
- return t.ngettext(msgid1, msgid2, n)
- def ldngettext(domain, msgid1, msgid2, n):
- try:
- t = translation(domain, _localedirs.get(domain, None),
- codeset=_localecodesets.get(domain))
- except OSError:
- if n == 1:
- return msgid1
- else:
- return msgid2
- return t.lngettext(msgid1, msgid2, n)
- def gettext(message):
- return dgettext(_current_domain, message)
- def lgettext(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)
- Catalog = translation
|