[PATCH 5 of 6] i18n: support changing the locale dynamically

timeless timeless at mozdev.org
Tue Jan 12 12:38:08 CST 2016


# HG changeset patch
# User timeless <timeless at mozdev.org>
# Date 1452606181 0
#      Tue Jan 12 13:43:01 2016 +0000
# Node ID b07e12420422c7767f72171c842426750d0f54a3
# Parent  104e7b46c453161871f7e9c4e3d60b9aaff13d02
i18n: support changing the locale dynamically

This only works for singlethreaded processes

To do this correctly, we'd have to have _
forward to `ui` or use ThreadLocal storage...

diff --git a/mercurial/i18n.py b/mercurial/i18n.py
--- a/mercurial/i18n.py
+++ b/mercurial/i18n.py
@@ -40,15 +40,23 @@
         pass
 
 _ugettext = None
+_msgcache = {}
+_langcache = {}
 
-def setdatapath(datapath):
+def setdatapath(datapath, locale=None):
+    global _msgcache
     localedir = os.path.join(datapath, 'locale')
-    t = gettextmod.translation('hg', localedir, _languages, fallback=True)
+    if locale is None:
+        locale = _languages
+    key = ':'.join(locale or '')
+    if not key in _langcache:
+        _langcache[key] = _msgcache = {}
+    else:
+        _msgcache = _langcache[key]
+    t = gettextmod.translation('hg', localedir, locale, fallback=True)
     global _ugettext
     _ugettext = t.ugettext
 
-_msgcache = {}
-
 def gettext(message):
     """Translate message.
 


More information about the Mercurial-devel mailing list