Mercurial > hg
comparison mercurial/i18n.py @ 34660:d00ec62d156f
i18n: cache translated messages per encoding
This is a simpler workaround alternative to D958, "i18n: clean msgcache when
encoding changes." The cache won't be bloated unless you run tons of commands
with different --encoding options on command server, or serve many repositories
of different web.encoding options on hgweb.
The test was originally written by Jun Wu.
Differential Revision: https://phab.mercurial-scm.org/D1053
author | Yuya Nishihara <yuya@tcha.org> |
---|---|
date | Fri, 13 Oct 2017 21:36:10 +0900 |
parents | 75979c8d4572 |
children | aeaf9c7f7528 |
comparison
equal
deleted
inserted
replaced
34659:3edfd472f3cb | 34660:d00ec62d156f |
---|---|
56 try: | 56 try: |
57 _ugettext = t.ugettext | 57 _ugettext = t.ugettext |
58 except AttributeError: | 58 except AttributeError: |
59 _ugettext = t.gettext | 59 _ugettext = t.gettext |
60 | 60 |
61 _msgcache = {} | 61 _msgcache = {} # encoding: {message: translation} |
62 | 62 |
63 def gettext(message): | 63 def gettext(message): |
64 """Translate message. | 64 """Translate message. |
65 | 65 |
66 The message is looked up in the catalog to get a Unicode string, | 66 The message is looked up in the catalog to get a Unicode string, |
72 # If message is None, t.ugettext will return u'None' as the | 72 # If message is None, t.ugettext will return u'None' as the |
73 # translation whereas our callers expect us to return None. | 73 # translation whereas our callers expect us to return None. |
74 if message is None or not _ugettext: | 74 if message is None or not _ugettext: |
75 return message | 75 return message |
76 | 76 |
77 if message not in _msgcache: | 77 cache = _msgcache.setdefault(encoding.encoding, {}) |
78 if message not in cache: | |
78 if type(message) is unicode: | 79 if type(message) is unicode: |
79 # goofy unicode docstrings in test | 80 # goofy unicode docstrings in test |
80 paragraphs = message.split(u'\n\n') | 81 paragraphs = message.split(u'\n\n') |
81 else: | 82 else: |
82 paragraphs = [p.decode("ascii") for p in message.split('\n\n')] | 83 paragraphs = [p.decode("ascii") for p in message.split('\n\n')] |
88 # decode the Unicode string. Calling u.decode(enc) really | 89 # decode the Unicode string. Calling u.decode(enc) really |
89 # means u.encode(sys.getdefaultencoding()).decode(enc). Since | 90 # means u.encode(sys.getdefaultencoding()).decode(enc). Since |
90 # the Python encoding defaults to 'ascii', this fails if the | 91 # the Python encoding defaults to 'ascii', this fails if the |
91 # translated string use non-ASCII characters. | 92 # translated string use non-ASCII characters. |
92 encodingstr = pycompat.sysstr(encoding.encoding) | 93 encodingstr = pycompat.sysstr(encoding.encoding) |
93 _msgcache[message] = u.encode(encodingstr, "replace") | 94 cache[message] = u.encode(encodingstr, "replace") |
94 except LookupError: | 95 except LookupError: |
95 # An unknown encoding results in a LookupError. | 96 # An unknown encoding results in a LookupError. |
96 _msgcache[message] = message | 97 cache[message] = message |
97 return _msgcache[message] | 98 return cache[message] |
98 | 99 |
99 def _plain(): | 100 def _plain(): |
100 if ('HGPLAIN' not in encoding.environ | 101 if ('HGPLAIN' not in encoding.environ |
101 and 'HGPLAINEXCEPT' not in encoding.environ): | 102 and 'HGPLAINEXCEPT' not in encoding.environ): |
102 return False | 103 return False |