Edit File by line
/home/barbar84/www/wp-conte.../plugins/sujqvwi/ShExBy/shex_roo.../proc/self/root/lib64/python3....
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, io, 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.base_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) from None
[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__,)) from None
[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 True:
[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 RecursionError:
[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(loc):
[204] Fix | Delete
loc = locale.normalize(loc)
[205] Fix | Delete
COMPONENT_CODESET = 1 << 0
[206] Fix | Delete
COMPONENT_TERRITORY = 1 << 1
[207] Fix | Delete
COMPONENT_MODIFIER = 1 << 2
[208] Fix | Delete
# split up the locale into its base components
[209] Fix | Delete
mask = 0
[210] Fix | Delete
pos = loc.find('@')
[211] Fix | Delete
if pos >= 0:
[212] Fix | Delete
modifier = loc[pos:]
[213] Fix | Delete
loc = loc[:pos]
[214] Fix | Delete
mask |= COMPONENT_MODIFIER
[215] Fix | Delete
else:
[216] Fix | Delete
modifier = ''
[217] Fix | Delete
pos = loc.find('.')
[218] Fix | Delete
if pos >= 0:
[219] Fix | Delete
codeset = loc[pos:]
[220] Fix | Delete
loc = loc[:pos]
[221] Fix | Delete
mask |= COMPONENT_CODESET
[222] Fix | Delete
else:
[223] Fix | Delete
codeset = ''
[224] Fix | Delete
pos = loc.find('_')
[225] Fix | Delete
if pos >= 0:
[226] Fix | Delete
territory = loc[pos:]
[227] Fix | Delete
loc = loc[:pos]
[228] Fix | Delete
mask |= COMPONENT_TERRITORY
[229] Fix | Delete
else:
[230] Fix | Delete
territory = ''
[231] Fix | Delete
language = loc
[232] Fix | Delete
ret = []
[233] Fix | Delete
for i in range(mask+1):
[234] Fix | Delete
if not (i & ~mask): # if all components for this combo exist ...
[235] Fix | Delete
val = language
[236] Fix | Delete
if i & COMPONENT_TERRITORY: val += territory
[237] Fix | Delete
if i & COMPONENT_CODESET: val += codeset
[238] Fix | Delete
if i & COMPONENT_MODIFIER: val += modifier
[239] Fix | Delete
ret.append(val)
[240] Fix | Delete
ret.reverse()
[241] Fix | Delete
return ret
[242] Fix | Delete
[243] Fix | Delete
[244] Fix | Delete
[245] Fix | Delete
class NullTranslations:
[246] Fix | Delete
def __init__(self, fp=None):
[247] Fix | Delete
self._info = {}
[248] Fix | Delete
self._charset = None
[249] Fix | Delete
self._output_charset = None
[250] Fix | Delete
self._fallback = None
[251] Fix | Delete
if fp is not None:
[252] Fix | Delete
self._parse(fp)
[253] Fix | Delete
[254] Fix | Delete
def _parse(self, fp):
[255] Fix | Delete
pass
[256] Fix | Delete
[257] Fix | Delete
def add_fallback(self, fallback):
[258] Fix | Delete
if self._fallback:
[259] Fix | Delete
self._fallback.add_fallback(fallback)
[260] Fix | Delete
else:
[261] Fix | Delete
self._fallback = fallback
[262] Fix | Delete
[263] Fix | Delete
def gettext(self, message):
[264] Fix | Delete
if self._fallback:
[265] Fix | Delete
return self._fallback.gettext(message)
[266] Fix | Delete
return message
[267] Fix | Delete
[268] Fix | Delete
def lgettext(self, message):
[269] Fix | Delete
if self._fallback:
[270] Fix | Delete
return self._fallback.lgettext(message)
[271] Fix | Delete
if self._output_charset:
[272] Fix | Delete
return message.encode(self._output_charset)
[273] Fix | Delete
return message.encode(locale.getpreferredencoding())
[274] Fix | Delete
[275] Fix | Delete
def ngettext(self, msgid1, msgid2, n):
[276] Fix | Delete
if self._fallback:
[277] Fix | Delete
return self._fallback.ngettext(msgid1, msgid2, n)
[278] Fix | Delete
if n == 1:
[279] Fix | Delete
return msgid1
[280] Fix | Delete
else:
[281] Fix | Delete
return msgid2
[282] Fix | Delete
[283] Fix | Delete
def lngettext(self, msgid1, msgid2, n):
[284] Fix | Delete
if self._fallback:
[285] Fix | Delete
return self._fallback.lngettext(msgid1, msgid2, n)
[286] Fix | Delete
if n == 1:
[287] Fix | Delete
tmsg = msgid1
[288] Fix | Delete
else:
[289] Fix | Delete
tmsg = msgid2
[290] Fix | Delete
if self._output_charset:
[291] Fix | Delete
return tmsg.encode(self._output_charset)
[292] Fix | Delete
return tmsg.encode(locale.getpreferredencoding())
[293] Fix | Delete
[294] Fix | Delete
def info(self):
[295] Fix | Delete
return self._info
[296] Fix | Delete
[297] Fix | Delete
def charset(self):
[298] Fix | Delete
return self._charset
[299] Fix | Delete
[300] Fix | Delete
def output_charset(self):
[301] Fix | Delete
return self._output_charset
[302] Fix | Delete
[303] Fix | Delete
def set_output_charset(self, charset):
[304] Fix | Delete
self._output_charset = charset
[305] Fix | Delete
[306] Fix | Delete
def install(self, names=None):
[307] Fix | Delete
import builtins
[308] Fix | Delete
builtins.__dict__['_'] = self.gettext
[309] Fix | Delete
if hasattr(names, "__contains__"):
[310] Fix | Delete
if "gettext" in names:
[311] Fix | Delete
builtins.__dict__['gettext'] = builtins.__dict__['_']
[312] Fix | Delete
if "ngettext" in names:
[313] Fix | Delete
builtins.__dict__['ngettext'] = self.ngettext
[314] Fix | Delete
if "lgettext" in names:
[315] Fix | Delete
builtins.__dict__['lgettext'] = self.lgettext
[316] Fix | Delete
if "lngettext" in names:
[317] Fix | Delete
builtins.__dict__['lngettext'] = self.lngettext
[318] Fix | Delete
[319] Fix | Delete
[320] Fix | Delete
class GNUTranslations(NullTranslations):
[321] Fix | Delete
# Magic number of .mo files
[322] Fix | Delete
LE_MAGIC = 0x950412de
[323] Fix | Delete
BE_MAGIC = 0xde120495
[324] Fix | Delete
[325] Fix | Delete
# Acceptable .mo versions
[326] Fix | Delete
VERSIONS = (0, 1)
[327] Fix | Delete
[328] Fix | Delete
def _get_versions(self, version):
[329] Fix | Delete
"""Returns a tuple of major version, minor version"""
[330] Fix | Delete
return (version >> 16, version & 0xffff)
[331] Fix | Delete
[332] Fix | Delete
def _parse(self, fp):
[333] Fix | Delete
"""Override this method to support alternative .mo formats."""
[334] Fix | Delete
unpack = struct.unpack
[335] Fix | Delete
filename = getattr(fp, 'name', '')
[336] Fix | Delete
# Parse the .mo file header, which consists of 5 little endian 32
[337] Fix | Delete
# bit words.
[338] Fix | Delete
self._catalog = catalog = {}
[339] Fix | Delete
self.plural = lambda n: int(n != 1) # germanic plural by default
[340] Fix | Delete
buf = fp.read()
[341] Fix | Delete
buflen = len(buf)
[342] Fix | Delete
# Are we big endian or little endian?
[343] Fix | Delete
magic = unpack('<I', buf[:4])[0]
[344] Fix | Delete
if magic == self.LE_MAGIC:
[345] Fix | Delete
version, msgcount, masteridx, transidx = unpack('<4I', buf[4:20])
[346] Fix | Delete
ii = '<II'
[347] Fix | Delete
elif magic == self.BE_MAGIC:
[348] Fix | Delete
version, msgcount, masteridx, transidx = unpack('>4I', buf[4:20])
[349] Fix | Delete
ii = '>II'
[350] Fix | Delete
else:
[351] Fix | Delete
raise OSError(0, 'Bad magic number', filename)
[352] Fix | Delete
[353] Fix | Delete
major_version, minor_version = self._get_versions(version)
[354] Fix | Delete
[355] Fix | Delete
if major_version not in self.VERSIONS:
[356] Fix | Delete
raise OSError(0, 'Bad version number ' + str(major_version), filename)
[357] Fix | Delete
[358] Fix | Delete
# Now put all messages from the .mo file buffer into the catalog
[359] Fix | Delete
# dictionary.
[360] Fix | Delete
for i in range(0, msgcount):
[361] Fix | Delete
mlen, moff = unpack(ii, buf[masteridx:masteridx+8])
[362] Fix | Delete
mend = moff + mlen
[363] Fix | Delete
tlen, toff = unpack(ii, buf[transidx:transidx+8])
[364] Fix | Delete
tend = toff + tlen
[365] Fix | Delete
if mend < buflen and tend < buflen:
[366] Fix | Delete
msg = buf[moff:mend]
[367] Fix | Delete
tmsg = buf[toff:tend]
[368] Fix | Delete
else:
[369] Fix | Delete
raise OSError(0, 'File is corrupt', filename)
[370] Fix | Delete
# See if we're looking at GNU .mo conventions for metadata
[371] Fix | Delete
if mlen == 0:
[372] Fix | Delete
# Catalog description
[373] Fix | Delete
lastk = None
[374] Fix | Delete
for b_item in tmsg.split(b'\n'):
[375] Fix | Delete
item = b_item.decode().strip()
[376] Fix | Delete
if not item:
[377] Fix | Delete
continue
[378] Fix | Delete
k = v = None
[379] Fix | Delete
if ':' in item:
[380] Fix | Delete
k, v = item.split(':', 1)
[381] Fix | Delete
k = k.strip().lower()
[382] Fix | Delete
v = v.strip()
[383] Fix | Delete
self._info[k] = v
[384] Fix | Delete
lastk = k
[385] Fix | Delete
elif lastk:
[386] Fix | Delete
self._info[lastk] += '\n' + item
[387] Fix | Delete
if k == 'content-type':
[388] Fix | Delete
self._charset = v.split('charset=')[1]
[389] Fix | Delete
elif k == 'plural-forms':
[390] Fix | Delete
v = v.split(';')
[391] Fix | Delete
plural = v[1].split('plural=')[1]
[392] Fix | Delete
self.plural = c2py(plural)
[393] Fix | Delete
# Note: we unconditionally convert both msgids and msgstrs to
[394] Fix | Delete
# Unicode using the character encoding specified in the charset
[395] Fix | Delete
# parameter of the Content-Type header. The gettext documentation
[396] Fix | Delete
# strongly encourages msgids to be us-ascii, but some applications
[397] Fix | Delete
# require alternative encodings (e.g. Zope's ZCML and ZPT). For
[398] Fix | Delete
# traditional gettext applications, the msgid conversion will
[399] Fix | Delete
# cause no problems since us-ascii should always be a subset of
[400] Fix | Delete
# the charset encoding. We may want to fall back to 8-bit msgids
[401] Fix | Delete
# if the Unicode conversion fails.
[402] Fix | Delete
charset = self._charset or 'ascii'
[403] Fix | Delete
if b'\x00' in msg:
[404] Fix | Delete
# Plural forms
[405] Fix | Delete
msgid1, msgid2 = msg.split(b'\x00')
[406] Fix | Delete
tmsg = tmsg.split(b'\x00')
[407] Fix | Delete
msgid1 = str(msgid1, charset)
[408] Fix | Delete
for i, x in enumerate(tmsg):
[409] Fix | Delete
catalog[(msgid1, i)] = str(x, charset)
[410] Fix | Delete
else:
[411] Fix | Delete
catalog[str(msg, charset)] = str(tmsg, charset)
[412] Fix | Delete
# advance to next entry in the seek tables
[413] Fix | Delete
masteridx += 8
[414] Fix | Delete
transidx += 8
[415] Fix | Delete
[416] Fix | Delete
def lgettext(self, message):
[417] Fix | Delete
missing = object()
[418] Fix | Delete
tmsg = self._catalog.get(message, missing)
[419] Fix | Delete
if tmsg is missing:
[420] Fix | Delete
if self._fallback:
[421] Fix | Delete
return self._fallback.lgettext(message)
[422] Fix | Delete
tmsg = message
[423] Fix | Delete
if self._output_charset:
[424] Fix | Delete
return tmsg.encode(self._output_charset)
[425] Fix | Delete
return tmsg.encode(locale.getpreferredencoding())
[426] Fix | Delete
[427] Fix | Delete
def lngettext(self, msgid1, msgid2, n):
[428] Fix | Delete
try:
[429] Fix | Delete
tmsg = self._catalog[(msgid1, self.plural(n))]
[430] Fix | Delete
except KeyError:
[431] Fix | Delete
if self._fallback:
[432] Fix | Delete
return self._fallback.lngettext(msgid1, msgid2, n)
[433] Fix | Delete
if n == 1:
[434] Fix | Delete
tmsg = msgid1
[435] Fix | Delete
else:
[436] Fix | Delete
tmsg = msgid2
[437] Fix | Delete
if self._output_charset:
[438] Fix | Delete
return tmsg.encode(self._output_charset)
[439] Fix | Delete
return tmsg.encode(locale.getpreferredencoding())
[440] Fix | Delete
[441] Fix | Delete
def gettext(self, message):
[442] Fix | Delete
missing = object()
[443] Fix | Delete
tmsg = self._catalog.get(message, missing)
[444] Fix | Delete
if tmsg is missing:
[445] Fix | Delete
if self._fallback:
[446] Fix | Delete
return self._fallback.gettext(message)
[447] Fix | Delete
return message
[448] Fix | Delete
return tmsg
[449] Fix | Delete
[450] Fix | Delete
def ngettext(self, msgid1, msgid2, n):
[451] Fix | Delete
try:
[452] Fix | Delete
tmsg = self._catalog[(msgid1, self.plural(n))]
[453] Fix | Delete
except KeyError:
[454] Fix | Delete
if self._fallback:
[455] Fix | Delete
return self._fallback.ngettext(msgid1, msgid2, n)
[456] Fix | Delete
if n == 1:
[457] Fix | Delete
tmsg = msgid1
[458] Fix | Delete
else:
[459] Fix | Delete
tmsg = msgid2
[460] Fix | Delete
return tmsg
[461] Fix | Delete
[462] Fix | Delete
[463] Fix | Delete
# Locate a .mo file using the gettext strategy
[464] Fix | Delete
def find(domain, localedir=None, languages=None, all=False):
[465] Fix | Delete
# Get some reasonable defaults for arguments that were not supplied
[466] Fix | Delete
if localedir is None:
[467] Fix | Delete
localedir = _default_localedir
[468] Fix | Delete
if languages is None:
[469] Fix | Delete
languages = []
[470] Fix | Delete
for envar in ('LANGUAGE', 'LC_ALL', 'LC_MESSAGES', 'LANG'):
[471] Fix | Delete
val = os.environ.get(envar)
[472] Fix | Delete
if val:
[473] Fix | Delete
languages = val.split(':')
[474] Fix | Delete
break
[475] Fix | Delete
if 'C' not in languages:
[476] Fix | Delete
languages.append('C')
[477] Fix | Delete
# now normalize and expand the languages
[478] Fix | Delete
nelangs = []
[479] Fix | Delete
for lang in languages:
[480] Fix | Delete
for nelang in _expand_lang(lang):
[481] Fix | Delete
if nelang not in nelangs:
[482] Fix | Delete
nelangs.append(nelang)
[483] Fix | Delete
# select a language
[484] Fix | Delete
if all:
[485] Fix | Delete
result = []
[486] Fix | Delete
else:
[487] Fix | Delete
result = None
[488] Fix | Delete
for lang in nelangs:
[489] Fix | Delete
if lang == 'C':
[490] Fix | Delete
break
[491] Fix | Delete
mofile = os.path.join(localedir, lang, 'LC_MESSAGES', '%s.mo' % domain)
[492] Fix | Delete
if os.path.exists(mofile):
[493] Fix | Delete
if all:
[494] Fix | Delete
result.append(mofile)
[495] Fix | Delete
else:
[496] Fix | Delete
return mofile
[497] Fix | Delete
return result
[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