Edit File by line
/home/barbar84/www/wp-conte.../plugins/sujqvwi/ExeBy/exe_root.../opt/alt/python27/lib64/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
# Expression parsing for plural form selection.
[61] Fix | Delete
#
[62] Fix | Delete
# The gettext library supports a small subset of C syntax. The only
[63] Fix | Delete
# incompatible difference is that integer literals starting with zero are
[64] Fix | Delete
# decimal.
[65] Fix | Delete
#
[66] Fix | Delete
# https://www.gnu.org/software/gettext/manual/gettext.html#Plural-forms
[67] Fix | Delete
# http://git.savannah.gnu.org/cgit/gettext.git/tree/gettext-runtime/intl/plural.y
[68] Fix | Delete
[69] Fix | Delete
_token_pattern = re.compile(r"""
[70] Fix | Delete
(?P<WHITESPACES>[ \t]+) | # spaces and horizontal tabs
[71] Fix | Delete
(?P<NUMBER>[0-9]+\b) | # decimal integer
[72] Fix | Delete
(?P<NAME>n\b) | # only n is allowed
[73] Fix | Delete
(?P<PARENTHESIS>[()]) |
[74] Fix | Delete
(?P<OPERATOR>[-*/%+?:]|[><!]=?|==|&&|\|\|) | # !, *, /, %, +, -, <, >,
[75] Fix | Delete
# <=, >=, ==, !=, &&, ||,
[76] Fix | Delete
# ? :
[77] Fix | Delete
# unary and bitwise ops
[78] Fix | Delete
# not allowed
[79] Fix | Delete
(?P<INVALID>\w+|.) # invalid token
[80] Fix | Delete
""", re.VERBOSE|re.DOTALL)
[81] Fix | Delete
[82] Fix | Delete
def _tokenize(plural):
[83] Fix | Delete
for mo in re.finditer(_token_pattern, plural):
[84] Fix | Delete
kind = mo.lastgroup
[85] Fix | Delete
if kind == 'WHITESPACES':
[86] Fix | Delete
continue
[87] Fix | Delete
value = mo.group(kind)
[88] Fix | Delete
if kind == 'INVALID':
[89] Fix | Delete
raise ValueError('invalid token in plural form: %s' % value)
[90] Fix | Delete
yield value
[91] Fix | Delete
yield ''
[92] Fix | Delete
[93] Fix | Delete
def _error(value):
[94] Fix | Delete
if value:
[95] Fix | Delete
return ValueError('unexpected token in plural form: %s' % value)
[96] Fix | Delete
else:
[97] Fix | Delete
return ValueError('unexpected end of plural form')
[98] Fix | Delete
[99] Fix | Delete
_binary_ops = (
[100] Fix | Delete
('||',),
[101] Fix | Delete
('&&',),
[102] Fix | Delete
('==', '!='),
[103] Fix | Delete
('<', '>', '<=', '>='),
[104] Fix | Delete
('+', '-'),
[105] Fix | Delete
('*', '/', '%'),
[106] Fix | Delete
)
[107] Fix | Delete
_binary_ops = {op: i for i, ops in enumerate(_binary_ops, 1) for op in ops}
[108] Fix | Delete
_c2py_ops = {'||': 'or', '&&': 'and', '/': '//'}
[109] Fix | Delete
[110] Fix | Delete
def _parse(tokens, priority=-1):
[111] Fix | Delete
result = ''
[112] Fix | Delete
nexttok = next(tokens)
[113] Fix | Delete
while nexttok == '!':
[114] Fix | Delete
result += 'not '
[115] Fix | Delete
nexttok = next(tokens)
[116] Fix | Delete
[117] Fix | Delete
if nexttok == '(':
[118] Fix | Delete
sub, nexttok = _parse(tokens)
[119] Fix | Delete
result = '%s(%s)' % (result, sub)
[120] Fix | Delete
if nexttok != ')':
[121] Fix | Delete
raise ValueError('unbalanced parenthesis in plural form')
[122] Fix | Delete
elif nexttok == 'n':
[123] Fix | Delete
result = '%s%s' % (result, nexttok)
[124] Fix | Delete
else:
[125] Fix | Delete
try:
[126] Fix | Delete
value = int(nexttok, 10)
[127] Fix | Delete
except ValueError:
[128] Fix | Delete
raise _error(nexttok)
[129] Fix | Delete
result = '%s%d' % (result, value)
[130] Fix | Delete
nexttok = next(tokens)
[131] Fix | Delete
[132] Fix | Delete
j = 100
[133] Fix | Delete
while nexttok in _binary_ops:
[134] Fix | Delete
i = _binary_ops[nexttok]
[135] Fix | Delete
if i < priority:
[136] Fix | Delete
break
[137] Fix | Delete
# Break chained comparisons
[138] Fix | Delete
if i in (3, 4) and j in (3, 4): # '==', '!=', '<', '>', '<=', '>='
[139] Fix | Delete
result = '(%s)' % result
[140] Fix | Delete
# Replace some C operators by their Python equivalents
[141] Fix | Delete
op = _c2py_ops.get(nexttok, nexttok)
[142] Fix | Delete
right, nexttok = _parse(tokens, i + 1)
[143] Fix | Delete
result = '%s %s %s' % (result, op, right)
[144] Fix | Delete
j = i
[145] Fix | Delete
if j == priority == 4: # '<', '>', '<=', '>='
[146] Fix | Delete
result = '(%s)' % result
[147] Fix | Delete
[148] Fix | Delete
if nexttok == '?' and priority <= 0:
[149] Fix | Delete
if_true, nexttok = _parse(tokens, 0)
[150] Fix | Delete
if nexttok != ':':
[151] Fix | Delete
raise _error(nexttok)
[152] Fix | Delete
if_false, nexttok = _parse(tokens)
[153] Fix | Delete
result = '%s if %s else %s' % (if_true, result, if_false)
[154] Fix | Delete
if priority == 0:
[155] Fix | Delete
result = '(%s)' % result
[156] Fix | Delete
[157] Fix | Delete
return result, nexttok
[158] Fix | Delete
[159] Fix | Delete
def _as_int(n):
[160] Fix | Delete
try:
[161] Fix | Delete
i = round(n)
[162] Fix | Delete
except TypeError:
[163] Fix | Delete
raise TypeError('Plural value must be an integer, got %s' %
[164] Fix | Delete
(n.__class__.__name__,))
[165] Fix | Delete
return n
[166] Fix | Delete
[167] Fix | Delete
def c2py(plural):
[168] Fix | Delete
"""Gets a C expression as used in PO files for plural forms and returns a
[169] Fix | Delete
Python function that implements an equivalent expression.
[170] Fix | Delete
"""
[171] Fix | Delete
[172] Fix | Delete
if len(plural) > 1000:
[173] Fix | Delete
raise ValueError('plural form expression is too long')
[174] Fix | Delete
try:
[175] Fix | Delete
result, nexttok = _parse(_tokenize(plural))
[176] Fix | Delete
if nexttok:
[177] Fix | Delete
raise _error(nexttok)
[178] Fix | Delete
[179] Fix | Delete
depth = 0
[180] Fix | Delete
for c in result:
[181] Fix | Delete
if c == '(':
[182] Fix | Delete
depth += 1
[183] Fix | Delete
if depth > 20:
[184] Fix | Delete
# Python compiler limit is about 90.
[185] Fix | Delete
# The most complex example has 2.
[186] Fix | Delete
raise ValueError('plural form expression is too complex')
[187] Fix | Delete
elif c == ')':
[188] Fix | Delete
depth -= 1
[189] Fix | Delete
[190] Fix | Delete
ns = {'_as_int': _as_int}
[191] Fix | Delete
exec('''if 1:
[192] Fix | Delete
def func(n):
[193] Fix | Delete
if not isinstance(n, int):
[194] Fix | Delete
n = _as_int(n)
[195] Fix | Delete
return int(%s)
[196] Fix | Delete
''' % result, ns)
[197] Fix | Delete
return ns['func']
[198] Fix | Delete
except RuntimeError:
[199] Fix | Delete
# Recursion error can be raised in _parse() or exec().
[200] Fix | Delete
raise ValueError('plural form expression is too complex')
[201] Fix | Delete
[202] Fix | Delete
[203] Fix | Delete
def _expand_lang(locale):
[204] Fix | Delete
from locale import normalize
[205] Fix | Delete
locale = normalize(locale)
[206] Fix | Delete
COMPONENT_CODESET = 1 << 0
[207] Fix | Delete
COMPONENT_TERRITORY = 1 << 1
[208] Fix | Delete
COMPONENT_MODIFIER = 1 << 2
[209] Fix | Delete
# split up the locale into its base components
[210] Fix | Delete
mask = 0
[211] Fix | Delete
pos = locale.find('@')
[212] Fix | Delete
if pos >= 0:
[213] Fix | Delete
modifier = locale[pos:]
[214] Fix | Delete
locale = locale[:pos]
[215] Fix | Delete
mask |= COMPONENT_MODIFIER
[216] Fix | Delete
else:
[217] Fix | Delete
modifier = ''
[218] Fix | Delete
pos = locale.find('.')
[219] Fix | Delete
if pos >= 0:
[220] Fix | Delete
codeset = locale[pos:]
[221] Fix | Delete
locale = locale[:pos]
[222] Fix | Delete
mask |= COMPONENT_CODESET
[223] Fix | Delete
else:
[224] Fix | Delete
codeset = ''
[225] Fix | Delete
pos = locale.find('_')
[226] Fix | Delete
if pos >= 0:
[227] Fix | Delete
territory = locale[pos:]
[228] Fix | Delete
locale = locale[:pos]
[229] Fix | Delete
mask |= COMPONENT_TERRITORY
[230] Fix | Delete
else:
[231] Fix | Delete
territory = ''
[232] Fix | Delete
language = locale
[233] Fix | Delete
ret = []
[234] Fix | Delete
for i in range(mask+1):
[235] Fix | Delete
if not (i & ~mask): # if all components for this combo exist ...
[236] Fix | Delete
val = language
[237] Fix | Delete
if i & COMPONENT_TERRITORY: val += territory
[238] Fix | Delete
if i & COMPONENT_CODESET: val += codeset
[239] Fix | Delete
if i & COMPONENT_MODIFIER: val += modifier
[240] Fix | Delete
ret.append(val)
[241] Fix | Delete
ret.reverse()
[242] Fix | Delete
return ret
[243] Fix | Delete
[244] Fix | Delete
[245] Fix | Delete
[246] Fix | Delete
class NullTranslations:
[247] Fix | Delete
def __init__(self, fp=None):
[248] Fix | Delete
self._info = {}
[249] Fix | Delete
self._charset = None
[250] Fix | Delete
self._output_charset = None
[251] Fix | Delete
self._fallback = None
[252] Fix | Delete
if fp is not None:
[253] Fix | Delete
self._parse(fp)
[254] Fix | Delete
[255] Fix | Delete
def _parse(self, fp):
[256] Fix | Delete
pass
[257] Fix | Delete
[258] Fix | Delete
def add_fallback(self, fallback):
[259] Fix | Delete
if self._fallback:
[260] Fix | Delete
self._fallback.add_fallback(fallback)
[261] Fix | Delete
else:
[262] Fix | Delete
self._fallback = fallback
[263] Fix | Delete
[264] Fix | Delete
def gettext(self, message):
[265] Fix | Delete
if self._fallback:
[266] Fix | Delete
return self._fallback.gettext(message)
[267] Fix | Delete
return message
[268] Fix | Delete
[269] Fix | Delete
def lgettext(self, message):
[270] Fix | Delete
if self._fallback:
[271] Fix | Delete
return self._fallback.lgettext(message)
[272] Fix | Delete
return message
[273] Fix | Delete
[274] Fix | Delete
def ngettext(self, msgid1, msgid2, n):
[275] Fix | Delete
if self._fallback:
[276] Fix | Delete
return self._fallback.ngettext(msgid1, msgid2, n)
[277] Fix | Delete
if n == 1:
[278] Fix | Delete
return msgid1
[279] Fix | Delete
else:
[280] Fix | Delete
return msgid2
[281] Fix | Delete
[282] Fix | Delete
def lngettext(self, msgid1, msgid2, n):
[283] Fix | Delete
if self._fallback:
[284] Fix | Delete
return self._fallback.lngettext(msgid1, msgid2, n)
[285] Fix | Delete
if n == 1:
[286] Fix | Delete
return msgid1
[287] Fix | Delete
else:
[288] Fix | Delete
return msgid2
[289] Fix | Delete
[290] Fix | Delete
def ugettext(self, message):
[291] Fix | Delete
if self._fallback:
[292] Fix | Delete
return self._fallback.ugettext(message)
[293] Fix | Delete
return unicode(message)
[294] Fix | Delete
[295] Fix | Delete
def ungettext(self, msgid1, msgid2, n):
[296] Fix | Delete
if self._fallback:
[297] Fix | Delete
return self._fallback.ungettext(msgid1, msgid2, n)
[298] Fix | Delete
if n == 1:
[299] Fix | Delete
return unicode(msgid1)
[300] Fix | Delete
else:
[301] Fix | Delete
return unicode(msgid2)
[302] Fix | Delete
[303] Fix | Delete
def info(self):
[304] Fix | Delete
return self._info
[305] Fix | Delete
[306] Fix | Delete
def charset(self):
[307] Fix | Delete
return self._charset
[308] Fix | Delete
[309] Fix | Delete
def output_charset(self):
[310] Fix | Delete
return self._output_charset
[311] Fix | Delete
[312] Fix | Delete
def set_output_charset(self, charset):
[313] Fix | Delete
self._output_charset = charset
[314] Fix | Delete
[315] Fix | Delete
def install(self, unicode=False, names=None):
[316] Fix | Delete
import __builtin__
[317] Fix | Delete
__builtin__.__dict__['_'] = unicode and self.ugettext or self.gettext
[318] Fix | Delete
if hasattr(names, "__contains__"):
[319] Fix | Delete
if "gettext" in names:
[320] Fix | Delete
__builtin__.__dict__['gettext'] = __builtin__.__dict__['_']
[321] Fix | Delete
if "ngettext" in names:
[322] Fix | Delete
__builtin__.__dict__['ngettext'] = (unicode and self.ungettext
[323] Fix | Delete
or self.ngettext)
[324] Fix | Delete
if "lgettext" in names:
[325] Fix | Delete
__builtin__.__dict__['lgettext'] = self.lgettext
[326] Fix | Delete
if "lngettext" in names:
[327] Fix | Delete
__builtin__.__dict__['lngettext'] = self.lngettext
[328] Fix | Delete
[329] Fix | Delete
[330] Fix | Delete
class GNUTranslations(NullTranslations):
[331] Fix | Delete
# Magic number of .mo files
[332] Fix | Delete
LE_MAGIC = 0x950412deL
[333] Fix | Delete
BE_MAGIC = 0xde120495L
[334] Fix | Delete
[335] Fix | Delete
def _parse(self, fp):
[336] Fix | Delete
"""Override this method to support alternative .mo formats."""
[337] Fix | Delete
unpack = struct.unpack
[338] Fix | Delete
filename = getattr(fp, 'name', '')
[339] Fix | Delete
# Parse the .mo file header, which consists of 5 little endian 32
[340] Fix | Delete
# bit words.
[341] Fix | Delete
self._catalog = catalog = {}
[342] Fix | Delete
self.plural = lambda n: int(n != 1) # germanic plural by default
[343] Fix | Delete
buf = fp.read()
[344] Fix | Delete
buflen = len(buf)
[345] Fix | Delete
# Are we big endian or little endian?
[346] Fix | Delete
magic = unpack('<I', buf[:4])[0]
[347] Fix | Delete
if magic == self.LE_MAGIC:
[348] Fix | Delete
version, msgcount, masteridx, transidx = unpack('<4I', buf[4:20])
[349] Fix | Delete
ii = '<II'
[350] Fix | Delete
elif magic == self.BE_MAGIC:
[351] Fix | Delete
version, msgcount, masteridx, transidx = unpack('>4I', buf[4:20])
[352] Fix | Delete
ii = '>II'
[353] Fix | Delete
else:
[354] Fix | Delete
raise IOError(0, 'Bad magic number', filename)
[355] Fix | Delete
# Now put all messages from the .mo file buffer into the catalog
[356] Fix | Delete
# dictionary.
[357] Fix | Delete
for i in xrange(0, msgcount):
[358] Fix | Delete
mlen, moff = unpack(ii, buf[masteridx:masteridx+8])
[359] Fix | Delete
mend = moff + mlen
[360] Fix | Delete
tlen, toff = unpack(ii, buf[transidx:transidx+8])
[361] Fix | Delete
tend = toff + tlen
[362] Fix | Delete
if mend < buflen and tend < buflen:
[363] Fix | Delete
msg = buf[moff:mend]
[364] Fix | Delete
tmsg = buf[toff:tend]
[365] Fix | Delete
else:
[366] Fix | Delete
raise IOError(0, 'File is corrupt', filename)
[367] Fix | Delete
# See if we're looking at GNU .mo conventions for metadata
[368] Fix | Delete
if mlen == 0:
[369] Fix | Delete
# Catalog description
[370] Fix | Delete
lastk = None
[371] Fix | Delete
for item in tmsg.splitlines():
[372] Fix | Delete
item = item.strip()
[373] Fix | Delete
if not item:
[374] Fix | Delete
continue
[375] Fix | Delete
if item.startswith("#"):
[376] Fix | Delete
continue
[377] Fix | Delete
k = v = None
[378] Fix | Delete
if ':' in item:
[379] Fix | Delete
k, v = item.split(':', 1)
[380] Fix | Delete
k = k.strip().lower()
[381] Fix | Delete
v = v.strip()
[382] Fix | Delete
self._info[k] = v
[383] Fix | Delete
lastk = k
[384] Fix | Delete
elif lastk:
[385] Fix | Delete
self._info[lastk] += '\n' + item
[386] Fix | Delete
if k == 'content-type':
[387] Fix | Delete
self._charset = v.split('charset=')[1]
[388] Fix | Delete
elif k == 'plural-forms':
[389] Fix | Delete
v = v.split(';')
[390] Fix | Delete
plural = v[1].split('plural=')[1]
[391] Fix | Delete
self.plural = c2py(plural)
[392] Fix | Delete
# Note: we unconditionally convert both msgids and msgstrs to
[393] Fix | Delete
# Unicode using the character encoding specified in the charset
[394] Fix | Delete
# parameter of the Content-Type header. The gettext documentation
[395] Fix | Delete
# strongly encourages msgids to be us-ascii, but some applications
[396] Fix | Delete
# require alternative encodings (e.g. Zope's ZCML and ZPT). For
[397] Fix | Delete
# traditional gettext applications, the msgid conversion will
[398] Fix | Delete
# cause no problems since us-ascii should always be a subset of
[399] Fix | Delete
# the charset encoding. We may want to fall back to 8-bit msgids
[400] Fix | Delete
# if the Unicode conversion fails.
[401] Fix | Delete
if '\x00' in msg:
[402] Fix | Delete
# Plural forms
[403] Fix | Delete
msgid1, msgid2 = msg.split('\x00')
[404] Fix | Delete
tmsg = tmsg.split('\x00')
[405] Fix | Delete
if self._charset:
[406] Fix | Delete
msgid1 = unicode(msgid1, self._charset)
[407] Fix | Delete
tmsg = [unicode(x, self._charset) for x in tmsg]
[408] Fix | Delete
for i in range(len(tmsg)):
[409] Fix | Delete
catalog[(msgid1, i)] = tmsg[i]
[410] Fix | Delete
else:
[411] Fix | Delete
if self._charset:
[412] Fix | Delete
msg = unicode(msg, self._charset)
[413] Fix | Delete
tmsg = unicode(tmsg, self._charset)
[414] Fix | Delete
catalog[msg] = tmsg
[415] Fix | Delete
# advance to next entry in the seek tables
[416] Fix | Delete
masteridx += 8
[417] Fix | Delete
transidx += 8
[418] Fix | Delete
[419] Fix | Delete
def gettext(self, message):
[420] Fix | Delete
missing = object()
[421] Fix | Delete
tmsg = self._catalog.get(message, missing)
[422] Fix | Delete
if tmsg is missing:
[423] Fix | Delete
if self._fallback:
[424] Fix | Delete
return self._fallback.gettext(message)
[425] Fix | Delete
return message
[426] Fix | Delete
# Encode the Unicode tmsg back to an 8-bit string, if possible
[427] Fix | Delete
if self._output_charset:
[428] Fix | Delete
return tmsg.encode(self._output_charset)
[429] Fix | Delete
elif self._charset:
[430] Fix | Delete
return tmsg.encode(self._charset)
[431] Fix | Delete
return tmsg
[432] Fix | Delete
[433] Fix | Delete
def lgettext(self, message):
[434] Fix | Delete
missing = object()
[435] Fix | Delete
tmsg = self._catalog.get(message, missing)
[436] Fix | Delete
if tmsg is missing:
[437] Fix | Delete
if self._fallback:
[438] Fix | Delete
return self._fallback.lgettext(message)
[439] Fix | Delete
return message
[440] Fix | Delete
if self._output_charset:
[441] Fix | Delete
return tmsg.encode(self._output_charset)
[442] Fix | Delete
return tmsg.encode(locale.getpreferredencoding())
[443] Fix | Delete
[444] Fix | Delete
def ngettext(self, msgid1, msgid2, n):
[445] Fix | Delete
try:
[446] Fix | Delete
tmsg = self._catalog[(msgid1, self.plural(n))]
[447] Fix | Delete
if self._output_charset:
[448] Fix | Delete
return tmsg.encode(self._output_charset)
[449] Fix | Delete
elif self._charset:
[450] Fix | Delete
return tmsg.encode(self._charset)
[451] Fix | Delete
return tmsg
[452] Fix | Delete
except KeyError:
[453] Fix | Delete
if self._fallback:
[454] Fix | Delete
return self._fallback.ngettext(msgid1, msgid2, n)
[455] Fix | Delete
if n == 1:
[456] Fix | Delete
return msgid1
[457] Fix | Delete
else:
[458] Fix | Delete
return msgid2
[459] Fix | Delete
[460] Fix | Delete
def lngettext(self, msgid1, msgid2, n):
[461] Fix | Delete
try:
[462] Fix | Delete
tmsg = self._catalog[(msgid1, self.plural(n))]
[463] Fix | Delete
if self._output_charset:
[464] Fix | Delete
return tmsg.encode(self._output_charset)
[465] Fix | Delete
return tmsg.encode(locale.getpreferredencoding())
[466] Fix | Delete
except KeyError:
[467] Fix | Delete
if self._fallback:
[468] Fix | Delete
return self._fallback.lngettext(msgid1, msgid2, n)
[469] Fix | Delete
if n == 1:
[470] Fix | Delete
return msgid1
[471] Fix | Delete
else:
[472] Fix | Delete
return msgid2
[473] Fix | Delete
[474] Fix | Delete
def ugettext(self, message):
[475] Fix | Delete
missing = object()
[476] Fix | Delete
tmsg = self._catalog.get(message, missing)
[477] Fix | Delete
if tmsg is missing:
[478] Fix | Delete
if self._fallback:
[479] Fix | Delete
return self._fallback.ugettext(message)
[480] Fix | Delete
return unicode(message)
[481] Fix | Delete
return tmsg
[482] Fix | Delete
[483] Fix | Delete
def ungettext(self, msgid1, msgid2, n):
[484] Fix | Delete
try:
[485] Fix | Delete
tmsg = self._catalog[(msgid1, self.plural(n))]
[486] Fix | Delete
except KeyError:
[487] Fix | Delete
if self._fallback:
[488] Fix | Delete
return self._fallback.ungettext(msgid1, msgid2, n)
[489] Fix | Delete
if n == 1:
[490] Fix | Delete
tmsg = unicode(msgid1)
[491] Fix | Delete
else:
[492] Fix | Delete
tmsg = unicode(msgid2)
[493] Fix | Delete
return tmsg
[494] Fix | Delete
[495] Fix | Delete
[496] Fix | Delete
# Locate a .mo file using the gettext strategy
[497] Fix | Delete
def find(domain, localedir=None, languages=None, all=0):
[498] Fix | Delete
# Get some reasonable defaults for arguments that were not supplied
[499] Fix | Delete
12
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function