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

login
register
mail settings
Submitter timeless@mozdev.org
Date Jan. 12, 2016, 6:38 p.m.
Message ID <b07e12420422c7767f72.1452623888@waste.org>
Download mbox | patch
Permalink /patch/12701/
State Changes Requested
Delegated to: Yuya Nishihara
Headers show

Comments

timeless@mozdev.org - Jan. 12, 2016, 6:38 p.m.
# HG changeset patch
# User timeless <timeless@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...
Yuya Nishihara - Jan. 13, 2016, 1:37 p.m.
On Tue, 12 Jan 2016 12:38:08 -0600, timeless wrote:
> # HG changeset patch
> # User timeless <timeless@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)

I don't think this hack should reside in mercurial.i18n.

If we want to make hgweb i18n-aware, _() function would have to be isolated
to request. It is the business of a web framework, not command line.

Patch

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.