diff 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
line wrap: on
line diff
--- a/mercurial/i18n.py	Thu Oct 12 22:09:11 2017 +0900
+++ b/mercurial/i18n.py	Fri Oct 13 21:36:10 2017 +0900
@@ -58,7 +58,7 @@
     except AttributeError:
         _ugettext = t.gettext
 
-_msgcache = {}
+_msgcache = {}  # encoding: {message: translation}
 
 def gettext(message):
     """Translate message.
@@ -74,7 +74,8 @@
     if message is None or not _ugettext:
         return message
 
-    if message not in _msgcache:
+    cache = _msgcache.setdefault(encoding.encoding, {})
+    if message not in cache:
         if type(message) is unicode:
             # goofy unicode docstrings in test
             paragraphs = message.split(u'\n\n')
@@ -90,11 +91,11 @@
             # the Python encoding defaults to 'ascii', this fails if the
             # translated string use non-ASCII characters.
             encodingstr = pycompat.sysstr(encoding.encoding)
-            _msgcache[message] = u.encode(encodingstr, "replace")
+            cache[message] = u.encode(encodingstr, "replace")
         except LookupError:
             # An unknown encoding results in a LookupError.
-            _msgcache[message] = message
-    return _msgcache[message]
+            cache[message] = message
+    return cache[message]
 
 def _plain():
     if ('HGPLAIN' not in encoding.environ