Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/AnonR/anonr.TX.../opt/imh-pyth.../lib/python2....
File: gettext.py
"""Internationalization and localization support.
[0] Fix | Delete
[1] Fix | Delete
This module provides internationalization (I18N) and localization (L10N)
[2] Fix | Delete
support for your Python programs by providing an interface to the GNU gettext
[3] Fix | Delete
message catalog library.
[4] Fix | Delete
[5] Fix | Delete
I18N refers to the operation by which a program is made aware of multiple
[6] Fix | Delete
languages. L10N refers to the adaptation of your program, once
[7] Fix | Delete
internationalized, to the local language and cultural habits.
[8] Fix | Delete
[9] Fix | Delete
"""
[10] Fix | Delete
[11] Fix | Delete
# This module represents the integration of work, contributions, feedback, and
[12] Fix | Delete
# suggestions from the following people:
[13] Fix | Delete
#
[14] Fix | Delete
# Martin von Loewis, who wrote the initial implementation of the underlying
[15] Fix | Delete
# C-based libintlmodule (later renamed _gettext), along with a skeletal
[16] Fix | Delete
# gettext.py implementation.
[17] Fix | Delete
#
[18] Fix | Delete
# Peter Funk, who wrote fintl.py, a fairly complete wrapper around intlmodule,
[19] Fix | Delete
# which also included a pure-Python implementation to read .mo files if
[20] Fix | Delete
# intlmodule wasn't available.
[21] Fix | Delete
#
[22] Fix | Delete
# James Henstridge, who also wrote a gettext.py module, which has some
[23] Fix | Delete
# interesting, but currently unsupported experimental features: the notion of
[24] Fix | Delete
# a Catalog class and instances, and the ability to add to a catalog file via
[25] Fix | Delete
# a Python API.
[26] Fix | Delete
#
[27] Fix | Delete
# Barry Warsaw integrated these modules, wrote the .install() API and code,
[28] Fix | Delete
# and conformed all C and Python code to Python's coding standards.
[29] Fix | Delete
#
[30] Fix | Delete
# Francois Pinard and Marc-Andre Lemburg also contributed valuably to this
[31] Fix | Delete
# module.
[32] Fix | Delete
#
[33] Fix | Delete
# J. David Ibanez implemented plural forms. Bruno Haible fixed some bugs.
[34] Fix | Delete
#
[35] Fix | Delete
# TODO:
[36] Fix | Delete
# - Lazy loading of .mo files. Currently the entire catalog is loaded into
[37] Fix | Delete
# memory, but that's probably bad for large translated programs. Instead,
[38] Fix | Delete
# the lexical sort of original strings in GNU .mo files should be exploited
[39] Fix | Delete
# to do binary searches and lazy initializations. Or you might want to use
[40] Fix | Delete
# the undocumented double-hash algorithm for .mo files with hash tables, but
[41] Fix | Delete
# you'll need to study the GNU gettext code to do this.
[42] Fix | Delete
#
[43] Fix | Delete
# - Support Solaris .mo file formats. Unfortunately, we've been unable to
[44] Fix | Delete
# find this format documented anywhere.
[45] Fix | Delete
[46] Fix | Delete
[47] Fix | Delete
import locale, copy, os, re, struct, sys
[48] Fix | Delete
from errno import ENOENT
[49] Fix | Delete
[50] Fix | Delete
[51] Fix | Delete
__all__ = ['NullTranslations', 'GNUTranslations', 'Catalog',
[52] Fix | Delete
'find', 'translation', 'install', 'textdomain', 'bindtextdomain',
[53] Fix | Delete
'bind_textdomain_codeset',
[54] Fix | Delete
'dgettext', 'dngettext', 'gettext', 'lgettext', 'ldgettext',
[55] Fix | Delete
'ldngettext', 'lngettext', 'ngettext',
[56] Fix | Delete
]
[57] Fix | Delete
[58] Fix | Delete
_default_localedir = os.path.join(sys.prefix, 'share', 'locale')
[59] Fix | Delete
[60] Fix | Delete
[61] Fix | Delete
def test(condition, true, false):
[62] Fix | Delete
"""
[63] Fix | Delete
Implements the C expression:
[64] Fix | Delete
[65] Fix | Delete
condition ? true : false
[66] Fix | Delete
[67] Fix | Delete
Required to correctly interpret plural forms.
[68] Fix | Delete
"""
[69] Fix | Delete
if condition:
[70] Fix | Delete
return true
[71] Fix | Delete
else:
[72] Fix | Delete
return false
[73] Fix | Delete
[74] Fix | Delete
[75] Fix | Delete
def c2py(plural):
[76] Fix | Delete
"""Gets a C expression as used in PO files for plural forms and returns a
[77] Fix | Delete
Python lambda function that implements an equivalent expression.
[78] Fix | Delete
"""
[79] Fix | Delete
# Security check, allow only the "n" identifier
[80] Fix | Delete
try:
[81] Fix | Delete
from cStringIO import StringIO
[82] Fix | Delete
except ImportError:
[83] Fix | Delete
from StringIO import StringIO
[84] Fix | Delete
import token, tokenize
[85] Fix | Delete
tokens = tokenize.generate_tokens(StringIO(plural).readline)
[86] Fix | Delete
try:
[87] Fix | Delete
danger = [x for x in tokens if x[0] == token.NAME and x[1] != 'n']
[88] Fix | Delete
except tokenize.TokenError:
[89] Fix | Delete
raise ValueError, \
[90] Fix | Delete
'plural forms expression error, maybe unbalanced parenthesis'
[91] Fix | Delete
else:
[92] Fix | Delete
if danger:
[93] Fix | Delete
raise ValueError, 'plural forms expression could be dangerous'
[94] Fix | Delete
[95] Fix | Delete
# Replace some C operators by their Python equivalents
[96] Fix | Delete
plural = plural.replace('&&', ' and ')
[97] Fix | Delete
plural = plural.replace('||', ' or ')
[98] Fix | Delete
[99] Fix | Delete
expr = re.compile(r'\!([^=])')
[100] Fix | Delete
plural = expr.sub(' not \\1', plural)
[101] Fix | Delete
[102] Fix | Delete
# Regular expression and replacement function used to transform
[103] Fix | Delete
# "a?b:c" to "test(a,b,c)".
[104] Fix | Delete
expr = re.compile(r'(.*?)\?(.*?):(.*)')
[105] Fix | Delete
def repl(x):
[106] Fix | Delete
return "test(%s, %s, %s)" % (x.group(1), x.group(2),
[107] Fix | Delete
expr.sub(repl, x.group(3)))
[108] Fix | Delete
[109] Fix | Delete
# Code to transform the plural expression, taking care of parentheses
[110] Fix | Delete
stack = ['']
[111] Fix | Delete
for c in plural:
[112] Fix | Delete
if c == '(':
[113] Fix | Delete
stack.append('')
[114] Fix | Delete
elif c == ')':
[115] Fix | Delete
if len(stack) == 1:
[116] Fix | Delete
# Actually, we never reach this code, because unbalanced
[117] Fix | Delete
# parentheses get caught in the security check at the
[118] Fix | Delete
# beginning.
[119] Fix | Delete
raise ValueError, 'unbalanced parenthesis in plural form'
[120] Fix | Delete
s = expr.sub(repl, stack.pop())
[121] Fix | Delete
stack[-1] += '(%s)' % s
[122] Fix | Delete
else:
[123] Fix | Delete
stack[-1] += c
[124] Fix | Delete
plural = expr.sub(repl, stack.pop())
[125] Fix | Delete
[126] Fix | Delete
return eval('lambda n: int(%s)' % plural)
[127] Fix | Delete
[128] Fix | Delete
[129] Fix | Delete
[130] Fix | Delete
def _expand_lang(locale):
[131] Fix | Delete
from locale import normalize
[132] Fix | Delete
locale = normalize(locale)
[133] Fix | Delete
COMPONENT_CODESET = 1 << 0
[134] Fix | Delete
COMPONENT_TERRITORY = 1 << 1
[135] Fix | Delete
COMPONENT_MODIFIER = 1 << 2
[136] Fix | Delete
# split up the locale into its base components
[137] Fix | Delete
mask = 0
[138] Fix | Delete
pos = locale.find('@')
[139] Fix | Delete
if pos >= 0:
[140] Fix | Delete
modifier = locale[pos:]
[141] Fix | Delete
locale = locale[:pos]
[142] Fix | Delete
mask |= COMPONENT_MODIFIER
[143] Fix | Delete
else:
[144] Fix | Delete
modifier = ''
[145] Fix | Delete
pos = locale.find('.')
[146] Fix | Delete
if pos >= 0:
[147] Fix | Delete
codeset = locale[pos:]
[148] Fix | Delete
locale = locale[:pos]
[149] Fix | Delete
mask |= COMPONENT_CODESET
[150] Fix | Delete
else:
[151] Fix | Delete
codeset = ''
[152] Fix | Delete
pos = locale.find('_')
[153] Fix | Delete
if pos >= 0:
[154] Fix | Delete
territory = locale[pos:]
[155] Fix | Delete
locale = locale[:pos]
[156] Fix | Delete
mask |= COMPONENT_TERRITORY
[157] Fix | Delete
else:
[158] Fix | Delete
territory = ''
[159] Fix | Delete
language = locale
[160] Fix | Delete
ret = []
[161] Fix | Delete
for i in range(mask+1):
[162] Fix | Delete
if not (i & ~mask): # if all components for this combo exist ...
[163] Fix | Delete
val = language
[164] Fix | Delete
if i & COMPONENT_TERRITORY: val += territory
[165] Fix | Delete
if i & COMPONENT_CODESET: val += codeset
[166] Fix | Delete
if i & COMPONENT_MODIFIER: val += modifier
[167] Fix | Delete
ret.append(val)
[168] Fix | Delete
ret.reverse()
[169] Fix | Delete
return ret
[170] Fix | Delete
[171] Fix | Delete
[172] Fix | Delete
[173] Fix | Delete
class NullTranslations:
[174] Fix | Delete
def __init__(self, fp=None):
[175] Fix | Delete
self._info = {}
[176] Fix | Delete
self._charset = None
[177] Fix | Delete
self._output_charset = None
[178] Fix | Delete
self._fallback = None
[179] Fix | Delete
if fp is not None:
[180] Fix | Delete
self._parse(fp)
[181] Fix | Delete
[182] Fix | Delete
def _parse(self, fp):
[183] Fix | Delete
pass
[184] Fix | Delete
[185] Fix | Delete
def add_fallback(self, fallback):
[186] Fix | Delete
if self._fallback:
[187] Fix | Delete
self._fallback.add_fallback(fallback)
[188] Fix | Delete
else:
[189] Fix | Delete
self._fallback = fallback
[190] Fix | Delete
[191] Fix | Delete
def gettext(self, message):
[192] Fix | Delete
if self._fallback:
[193] Fix | Delete
return self._fallback.gettext(message)
[194] Fix | Delete
return message
[195] Fix | Delete
[196] Fix | Delete
def lgettext(self, message):
[197] Fix | Delete
if self._fallback:
[198] Fix | Delete
return self._fallback.lgettext(message)
[199] Fix | Delete
return message
[200] Fix | Delete
[201] Fix | Delete
def ngettext(self, msgid1, msgid2, n):
[202] Fix | Delete
if self._fallback:
[203] Fix | Delete
return self._fallback.ngettext(msgid1, msgid2, n)
[204] Fix | Delete
if n == 1:
[205] Fix | Delete
return msgid1
[206] Fix | Delete
else:
[207] Fix | Delete
return msgid2
[208] Fix | Delete
[209] Fix | Delete
def lngettext(self, msgid1, msgid2, n):
[210] Fix | Delete
if self._fallback:
[211] Fix | Delete
return self._fallback.lngettext(msgid1, msgid2, n)
[212] Fix | Delete
if n == 1:
[213] Fix | Delete
return msgid1
[214] Fix | Delete
else:
[215] Fix | Delete
return msgid2
[216] Fix | Delete
[217] Fix | Delete
def ugettext(self, message):
[218] Fix | Delete
if self._fallback:
[219] Fix | Delete
return self._fallback.ugettext(message)
[220] Fix | Delete
return unicode(message)
[221] Fix | Delete
[222] Fix | Delete
def ungettext(self, msgid1, msgid2, n):
[223] Fix | Delete
if self._fallback:
[224] Fix | Delete
return self._fallback.ungettext(msgid1, msgid2, n)
[225] Fix | Delete
if n == 1:
[226] Fix | Delete
return unicode(msgid1)
[227] Fix | Delete
else:
[228] Fix | Delete
return unicode(msgid2)
[229] Fix | Delete
[230] Fix | Delete
def info(self):
[231] Fix | Delete
return self._info
[232] Fix | Delete
[233] Fix | Delete
def charset(self):
[234] Fix | Delete
return self._charset
[235] Fix | Delete
[236] Fix | Delete
def output_charset(self):
[237] Fix | Delete
return self._output_charset
[238] Fix | Delete
[239] Fix | Delete
def set_output_charset(self, charset):
[240] Fix | Delete
self._output_charset = charset
[241] Fix | Delete
[242] Fix | Delete
def install(self, unicode=False, names=None):
[243] Fix | Delete
import __builtin__
[244] Fix | Delete
__builtin__.__dict__['_'] = unicode and self.ugettext or self.gettext
[245] Fix | Delete
if hasattr(names, "__contains__"):
[246] Fix | Delete
if "gettext" in names:
[247] Fix | Delete
__builtin__.__dict__['gettext'] = __builtin__.__dict__['_']
[248] Fix | Delete
if "ngettext" in names:
[249] Fix | Delete
__builtin__.__dict__['ngettext'] = (unicode and self.ungettext
[250] Fix | Delete
or self.ngettext)
[251] Fix | Delete
if "lgettext" in names:
[252] Fix | Delete
__builtin__.__dict__['lgettext'] = self.lgettext
[253] Fix | Delete
if "lngettext" in names:
[254] Fix | Delete
__builtin__.__dict__['lngettext'] = self.lngettext
[255] Fix | Delete
[256] Fix | Delete
[257] Fix | Delete
class GNUTranslations(NullTranslations):
[258] Fix | Delete
# Magic number of .mo files
[259] Fix | Delete
LE_MAGIC = 0x950412deL
[260] Fix | Delete
BE_MAGIC = 0xde120495L
[261] Fix | Delete
[262] Fix | Delete
def _parse(self, fp):
[263] Fix | Delete
"""Override this method to support alternative .mo formats."""
[264] Fix | Delete
unpack = struct.unpack
[265] Fix | Delete
filename = getattr(fp, 'name', '')
[266] Fix | Delete
# Parse the .mo file header, which consists of 5 little endian 32
[267] Fix | Delete
# bit words.
[268] Fix | Delete
self._catalog = catalog = {}
[269] Fix | Delete
self.plural = lambda n: int(n != 1) # germanic plural by default
[270] Fix | Delete
buf = fp.read()
[271] Fix | Delete
buflen = len(buf)
[272] Fix | Delete
# Are we big endian or little endian?
[273] Fix | Delete
magic = unpack('<I', buf[:4])[0]
[274] Fix | Delete
if magic == self.LE_MAGIC:
[275] Fix | Delete
version, msgcount, masteridx, transidx = unpack('<4I', buf[4:20])
[276] Fix | Delete
ii = '<II'
[277] Fix | Delete
elif magic == self.BE_MAGIC:
[278] Fix | Delete
version, msgcount, masteridx, transidx = unpack('>4I', buf[4:20])
[279] Fix | Delete
ii = '>II'
[280] Fix | Delete
else:
[281] Fix | Delete
raise IOError(0, 'Bad magic number', filename)
[282] Fix | Delete
# Now put all messages from the .mo file buffer into the catalog
[283] Fix | Delete
# dictionary.
[284] Fix | Delete
for i in xrange(0, msgcount):
[285] Fix | Delete
mlen, moff = unpack(ii, buf[masteridx:masteridx+8])
[286] Fix | Delete
mend = moff + mlen
[287] Fix | Delete
tlen, toff = unpack(ii, buf[transidx:transidx+8])
[288] Fix | Delete
tend = toff + tlen
[289] Fix | Delete
if mend < buflen and tend < buflen:
[290] Fix | Delete
msg = buf[moff:mend]
[291] Fix | Delete
tmsg = buf[toff:tend]
[292] Fix | Delete
else:
[293] Fix | Delete
raise IOError(0, 'File is corrupt', filename)
[294] Fix | Delete
# See if we're looking at GNU .mo conventions for metadata
[295] Fix | Delete
if mlen == 0:
[296] Fix | Delete
# Catalog description
[297] Fix | Delete
lastk = None
[298] Fix | Delete
for item in tmsg.splitlines():
[299] Fix | Delete
item = item.strip()
[300] Fix | Delete
if not item:
[301] Fix | Delete
continue
[302] Fix | Delete
k = v = None
[303] Fix | Delete
if ':' in item:
[304] Fix | Delete
k, v = item.split(':', 1)
[305] Fix | Delete
k = k.strip().lower()
[306] Fix | Delete
v = v.strip()
[307] Fix | Delete
self._info[k] = v
[308] Fix | Delete
lastk = k
[309] Fix | Delete
elif lastk:
[310] Fix | Delete
self._info[lastk] += '\n' + item
[311] Fix | Delete
if k == 'content-type':
[312] Fix | Delete
self._charset = v.split('charset=')[1]
[313] Fix | Delete
elif k == 'plural-forms':
[314] Fix | Delete
v = v.split(';')
[315] Fix | Delete
plural = v[1].split('plural=')[1]
[316] Fix | Delete
self.plural = c2py(plural)
[317] Fix | Delete
# Note: we unconditionally convert both msgids and msgstrs to
[318] Fix | Delete
# Unicode using the character encoding specified in the charset
[319] Fix | Delete
# parameter of the Content-Type header. The gettext documentation
[320] Fix | Delete
# strongly encourages msgids to be us-ascii, but some applications
[321] Fix | Delete
# require alternative encodings (e.g. Zope's ZCML and ZPT). For
[322] Fix | Delete
# traditional gettext applications, the msgid conversion will
[323] Fix | Delete
# cause no problems since us-ascii should always be a subset of
[324] Fix | Delete
# the charset encoding. We may want to fall back to 8-bit msgids
[325] Fix | Delete
# if the Unicode conversion fails.
[326] Fix | Delete
if '\x00' in msg:
[327] Fix | Delete
# Plural forms
[328] Fix | Delete
msgid1, msgid2 = msg.split('\x00')
[329] Fix | Delete
tmsg = tmsg.split('\x00')
[330] Fix | Delete
if self._charset:
[331] Fix | Delete
msgid1 = unicode(msgid1, self._charset)
[332] Fix | Delete
tmsg = [unicode(x, self._charset) for x in tmsg]
[333] Fix | Delete
for i in range(len(tmsg)):
[334] Fix | Delete
catalog[(msgid1, i)] = tmsg[i]
[335] Fix | Delete
else:
[336] Fix | Delete
if self._charset:
[337] Fix | Delete
msg = unicode(msg, self._charset)
[338] Fix | Delete
tmsg = unicode(tmsg, self._charset)
[339] Fix | Delete
catalog[msg] = tmsg
[340] Fix | Delete
# advance to next entry in the seek tables
[341] Fix | Delete
masteridx += 8
[342] Fix | Delete
transidx += 8
[343] Fix | Delete
[344] Fix | Delete
def gettext(self, message):
[345] Fix | Delete
missing = object()
[346] Fix | Delete
tmsg = self._catalog.get(message, missing)
[347] Fix | Delete
if tmsg is missing:
[348] Fix | Delete
if self._fallback:
[349] Fix | Delete
return self._fallback.gettext(message)
[350] Fix | Delete
return message
[351] Fix | Delete
# Encode the Unicode tmsg back to an 8-bit string, if possible
[352] Fix | Delete
if self._output_charset:
[353] Fix | Delete
return tmsg.encode(self._output_charset)
[354] Fix | Delete
elif self._charset:
[355] Fix | Delete
return tmsg.encode(self._charset)
[356] Fix | Delete
return tmsg
[357] Fix | Delete
[358] Fix | Delete
def lgettext(self, message):
[359] Fix | Delete
missing = object()
[360] Fix | Delete
tmsg = self._catalog.get(message, missing)
[361] Fix | Delete
if tmsg is missing:
[362] Fix | Delete
if self._fallback:
[363] Fix | Delete
return self._fallback.lgettext(message)
[364] Fix | Delete
return message
[365] Fix | Delete
if self._output_charset:
[366] Fix | Delete
return tmsg.encode(self._output_charset)
[367] Fix | Delete
return tmsg.encode(locale.getpreferredencoding())
[368] Fix | Delete
[369] Fix | Delete
def ngettext(self, msgid1, msgid2, n):
[370] Fix | Delete
try:
[371] Fix | Delete
tmsg = self._catalog[(msgid1, self.plural(n))]
[372] Fix | Delete
if self._output_charset:
[373] Fix | Delete
return tmsg.encode(self._output_charset)
[374] Fix | Delete
elif self._charset:
[375] Fix | Delete
return tmsg.encode(self._charset)
[376] Fix | Delete
return tmsg
[377] Fix | Delete
except KeyError:
[378] Fix | Delete
if self._fallback:
[379] Fix | Delete
return self._fallback.ngettext(msgid1, msgid2, n)
[380] Fix | Delete
if n == 1:
[381] Fix | Delete
return msgid1
[382] Fix | Delete
else:
[383] Fix | Delete
return msgid2
[384] Fix | Delete
[385] Fix | Delete
def lngettext(self, msgid1, msgid2, n):
[386] Fix | Delete
try:
[387] Fix | Delete
tmsg = self._catalog[(msgid1, self.plural(n))]
[388] Fix | Delete
if self._output_charset:
[389] Fix | Delete
return tmsg.encode(self._output_charset)
[390] Fix | Delete
return tmsg.encode(locale.getpreferredencoding())
[391] Fix | Delete
except KeyError:
[392] Fix | Delete
if self._fallback:
[393] Fix | Delete
return self._fallback.lngettext(msgid1, msgid2, n)
[394] Fix | Delete
if n == 1:
[395] Fix | Delete
return msgid1
[396] Fix | Delete
else:
[397] Fix | Delete
return msgid2
[398] Fix | Delete
[399] Fix | Delete
def ugettext(self, message):
[400] Fix | Delete
missing = object()
[401] Fix | Delete
tmsg = self._catalog.get(message, missing)
[402] Fix | Delete
if tmsg is missing:
[403] Fix | Delete
if self._fallback:
[404] Fix | Delete
return self._fallback.ugettext(message)
[405] Fix | Delete
return unicode(message)
[406] Fix | Delete
return tmsg
[407] Fix | Delete
[408] Fix | Delete
def ungettext(self, msgid1, msgid2, n):
[409] Fix | Delete
try:
[410] Fix | Delete
tmsg = self._catalog[(msgid1, self.plural(n))]
[411] Fix | Delete
except KeyError:
[412] Fix | Delete
if self._fallback:
[413] Fix | Delete
return self._fallback.ungettext(msgid1, msgid2, n)
[414] Fix | Delete
if n == 1:
[415] Fix | Delete
tmsg = unicode(msgid1)
[416] Fix | Delete
else:
[417] Fix | Delete
tmsg = unicode(msgid2)
[418] Fix | Delete
return tmsg
[419] Fix | Delete
[420] Fix | Delete
[421] Fix | Delete
# Locate a .mo file using the gettext strategy
[422] Fix | Delete
def find(domain, localedir=None, languages=None, all=0):
[423] Fix | Delete
# Get some reasonable defaults for arguments that were not supplied
[424] Fix | Delete
if localedir is None:
[425] Fix | Delete
localedir = _default_localedir
[426] Fix | Delete
if languages is None:
[427] Fix | Delete
languages = []
[428] Fix | Delete
for envar in ('LANGUAGE', 'LC_ALL', 'LC_MESSAGES', 'LANG'):
[429] Fix | Delete
val = os.environ.get(envar)
[430] Fix | Delete
if val:
[431] Fix | Delete
languages = val.split(':')
[432] Fix | Delete
break
[433] Fix | Delete
if 'C' not in languages:
[434] Fix | Delete
languages.append('C')
[435] Fix | Delete
# now normalize and expand the languages
[436] Fix | Delete
nelangs = []
[437] Fix | Delete
for lang in languages:
[438] Fix | Delete
for nelang in _expand_lang(lang):
[439] Fix | Delete
if nelang not in nelangs:
[440] Fix | Delete
nelangs.append(nelang)
[441] Fix | Delete
# select a language
[442] Fix | Delete
if all:
[443] Fix | Delete
result = []
[444] Fix | Delete
else:
[445] Fix | Delete
result = None
[446] Fix | Delete
for lang in nelangs:
[447] Fix | Delete
if lang == 'C':
[448] Fix | Delete
break
[449] Fix | Delete
mofile = os.path.join(localedir, lang, 'LC_MESSAGES', '%s.mo' % domain)
[450] Fix | Delete
if os.path.exists(mofile):
[451] Fix | Delete
if all:
[452] Fix | Delete
result.append(mofile)
[453] Fix | Delete
else:
[454] Fix | Delete
return mofile
[455] Fix | Delete
return result
[456] Fix | Delete
[457] Fix | Delete
[458] Fix | Delete
[459] Fix | Delete
# a mapping between absolute .mo file path and Translation object
[460] Fix | Delete
_translations = {}
[461] Fix | Delete
[462] Fix | Delete
def translation(domain, localedir=None, languages=None,
[463] Fix | Delete
class_=None, fallback=False, codeset=None):
[464] Fix | Delete
if class_ is None:
[465] Fix | Delete
class_ = GNUTranslations
[466] Fix | Delete
mofiles = find(domain, localedir, languages, all=1)
[467] Fix | Delete
if not mofiles:
[468] Fix | Delete
if fallback:
[469] Fix | Delete
return NullTranslations()
[470] Fix | Delete
raise IOError(ENOENT, 'No translation file found for domain', domain)
[471] Fix | Delete
# Avoid opening, reading, and parsing the .mo file after it's been done
[472] Fix | Delete
# once.
[473] Fix | Delete
result = None
[474] Fix | Delete
for mofile in mofiles:
[475] Fix | Delete
key = (class_, os.path.abspath(mofile))
[476] Fix | Delete
t = _translations.get(key)
[477] Fix | Delete
if t is None:
[478] Fix | Delete
with open(mofile, 'rb') as fp:
[479] Fix | Delete
t = _translations.setdefault(key, class_(fp))
[480] Fix | Delete
# Copy the translation object to allow setting fallbacks and
[481] Fix | Delete
# output charset. All other instance data is shared with the
[482] Fix | Delete
# cached object.
[483] Fix | Delete
t = copy.copy(t)
[484] Fix | Delete
if codeset:
[485] Fix | Delete
t.set_output_charset(codeset)
[486] Fix | Delete
if result is None:
[487] Fix | Delete
result = t
[488] Fix | Delete
else:
[489] Fix | Delete
result.add_fallback(t)
[490] Fix | Delete
return result
[491] Fix | Delete
[492] Fix | Delete
[493] Fix | Delete
def install(domain, localedir=None, unicode=False, codeset=None, names=None):
[494] Fix | Delete
t = translation(domain, localedir, fallback=True, codeset=codeset)
[495] Fix | Delete
t.install(unicode, names)
[496] Fix | Delete
[497] Fix | Delete
[498] Fix | Delete
[499] Fix | Delete
12
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function