Patchwork D1053: i18n: cache translated messages per encoding

login
register
mail settings
Submitter phabricator
Date Oct. 13, 2017, 5:25 p.m.
Message ID <e587ec3f1171a80c09a7f734fbebbf39@localhost.localdomain>
Download mbox | patch
Permalink /patch/24837/
State Not Applicable
Headers show

Comments

phabricator - Oct. 13, 2017, 5:25 p.m.
This revision was automatically updated to reflect the committed changes.
Closed by commit rHGd00ec62d156f: i18n: cache translated messages per encoding (authored by yuja, committed by ).

REPOSITORY
  rHG Mercurial

CHANGES SINCE LAST UPDATE
  https://phab.mercurial-scm.org/D1053?vs=2670&id=2694

REVISION DETAIL
  https://phab.mercurial-scm.org/D1053

AFFECTED FILES
  mercurial/i18n.py
  tests/test-i18n.t

CHANGE DETAILS




To: yuja, #hg-reviewers, quark, ryanmce
Cc: ryanmce, quark, mercurial-devel

Patch

diff --git a/tests/test-i18n.t b/tests/test-i18n.t
--- a/tests/test-i18n.t
+++ b/tests/test-i18n.t
@@ -48,3 +48,23 @@ 
   $ $PYTHON check-translation.py *.po
   $ $PYTHON check-translation.py --doctest
   $ cd $TESTTMP
+
+Check i18n cache isn't reused after encoding change:
+
+  $ cat > $TESTTMP/encodingchange.py << EOF
+  > from mercurial import encoding, registrar
+  > from mercurial.i18n import _
+  > cmdtable = {}
+  > command = registrar.command(cmdtable)
+  > @command(b'encodingchange', norepo=True)
+  > def encodingchange(ui):
+  >     for encode in (b'ascii', b'UTF-8', b'ascii', b'UTF-8'):
+  >         encoding.encoding = encode
+  >         ui.write(b'%s\n' % _(b'(EXPERIMENTAL)'))
+  > EOF
+
+  $ LANGUAGE=ja hg --config extensions.encodingchange=$TESTTMP/encodingchange.py encodingchange
+  (?????)
+  (\xe5\xae\x9f\xe9\xa8\x93\xe7\x9a\x84\xe5\xae\x9f\xe8\xa3\x85) (esc)
+  (?????)
+  (\xe5\xae\x9f\xe9\xa8\x93\xe7\x9a\x84\xe5\xae\x9f\xe8\xa3\x85) (esc)
diff --git a/mercurial/i18n.py b/mercurial/i18n.py
--- a/mercurial/i18n.py
+++ b/mercurial/i18n.py
@@ -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