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

Yuya Nishihara yuya at tcha.org
Wed Jan 13 07:37:01 CST 2016


On Tue, 12 Jan 2016 12:38:08 -0600, timeless wrote:
> # 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)

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.


More information about the Mercurial-devel mailing list