##// END OF EJS Templates
i18n: cache the result of every gettext call...
Augie Fackler -
r23031:3c0983cc default
parent child Browse files
Show More
@@ -41,6 +41,8 b' def setdatapath(datapath):'
41 41 global _ugettext
42 42 _ugettext = t.ugettext
43 43
44 _msgcache = {}
45
44 46 def gettext(message):
45 47 """Translate message.
46 48
@@ -55,24 +57,26 b' def gettext(message):'
55 57 if message is None or not _ugettext:
56 58 return message
57 59
58 if type(message) is unicode:
59 # goofy unicode docstrings in test
60 paragraphs = message.split(u'\n\n')
61 else:
62 paragraphs = [p.decode("ascii") for p in message.split('\n\n')]
63 # Be careful not to translate the empty string -- it holds the
64 # meta data of the .po file.
65 u = u'\n\n'.join([p and _ugettext(p) or '' for p in paragraphs])
66 try:
67 # encoding.tolocal cannot be used since it will first try to
68 # decode the Unicode string. Calling u.decode(enc) really
69 # means u.encode(sys.getdefaultencoding()).decode(enc). Since
70 # the Python encoding defaults to 'ascii', this fails if the
71 # translated string use non-ASCII characters.
72 return u.encode(encoding.encoding, "replace")
73 except LookupError:
74 # An unknown encoding results in a LookupError.
75 return message
60 if message not in _msgcache:
61 if type(message) is unicode:
62 # goofy unicode docstrings in test
63 paragraphs = message.split(u'\n\n')
64 else:
65 paragraphs = [p.decode("ascii") for p in message.split('\n\n')]
66 # Be careful not to translate the empty string -- it holds the
67 # meta data of the .po file.
68 u = u'\n\n'.join([p and _ugettext(p) or '' for p in paragraphs])
69 try:
70 # encoding.tolocal cannot be used since it will first try to
71 # decode the Unicode string. Calling u.decode(enc) really
72 # means u.encode(sys.getdefaultencoding()).decode(enc). Since
73 # the Python encoding defaults to 'ascii', this fails if the
74 # translated string use non-ASCII characters.
75 _msgcache[message] = u.encode(encoding.encoding, "replace")
76 except LookupError:
77 # An unknown encoding results in a LookupError.
78 _msgcache[message] = message
79 return _msgcache[message]
76 80
77 81 def _plain():
78 82 if 'HGPLAIN' not in os.environ and 'HGPLAINEXCEPT' not in os.environ:
General Comments 0
You need to be logged in to leave comments. Login now