##// 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 global _ugettext
41 global _ugettext
42 _ugettext = t.ugettext
42 _ugettext = t.ugettext
43
43
44 _msgcache = {}
45
44 def gettext(message):
46 def gettext(message):
45 """Translate message.
47 """Translate message.
46
48
@@ -55,24 +57,26 b' def gettext(message):'
55 if message is None or not _ugettext:
57 if message is None or not _ugettext:
56 return message
58 return message
57
59
58 if type(message) is unicode:
60 if message not in _msgcache:
59 # goofy unicode docstrings in test
61 if type(message) is unicode:
60 paragraphs = message.split(u'\n\n')
62 # goofy unicode docstrings in test
61 else:
63 paragraphs = message.split(u'\n\n')
62 paragraphs = [p.decode("ascii") for p in message.split('\n\n')]
64 else:
63 # Be careful not to translate the empty string -- it holds the
65 paragraphs = [p.decode("ascii") for p in message.split('\n\n')]
64 # meta data of the .po file.
66 # Be careful not to translate the empty string -- it holds the
65 u = u'\n\n'.join([p and _ugettext(p) or '' for p in paragraphs])
67 # meta data of the .po file.
66 try:
68 u = u'\n\n'.join([p and _ugettext(p) or '' for p in paragraphs])
67 # encoding.tolocal cannot be used since it will first try to
69 try:
68 # decode the Unicode string. Calling u.decode(enc) really
70 # encoding.tolocal cannot be used since it will first try to
69 # means u.encode(sys.getdefaultencoding()).decode(enc). Since
71 # decode the Unicode string. Calling u.decode(enc) really
70 # the Python encoding defaults to 'ascii', this fails if the
72 # means u.encode(sys.getdefaultencoding()).decode(enc). Since
71 # translated string use non-ASCII characters.
73 # the Python encoding defaults to 'ascii', this fails if the
72 return u.encode(encoding.encoding, "replace")
74 # translated string use non-ASCII characters.
73 except LookupError:
75 _msgcache[message] = u.encode(encoding.encoding, "replace")
74 # An unknown encoding results in a LookupError.
76 except LookupError:
75 return message
77 # An unknown encoding results in a LookupError.
78 _msgcache[message] = message
79 return _msgcache[message]
76
80
77 def _plain():
81 def _plain():
78 if 'HGPLAIN' not in os.environ and 'HGPLAINEXCEPT' not in os.environ:
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