Patchwork i18n: detect UI language without POSIX-style locale variable on Windows (BC)

login
register
mail settings
Submitter Yuya Nishihara
Date Aug. 3, 2014, 12:36 p.m.
Message ID <20140803213600.e6e543018d8496f6bfe19efb@tcha.org>
Download mbox | patch
Permalink /patch/5245/
State Accepted
Headers show

Comments

Yuya Nishihara - Aug. 3, 2014, 12:36 p.m.
This is copied from i18n module of TortoiseHg.  I'm not sure if Windows CLI
users like it.

# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1407061163 -32400
#      Sun Aug 03 19:19:23 2014 +0900
# Node ID 694ed966abb2bfbc6dd643cdea9973e66722cc9b
# Parent  3467cf39aae688e844d5c1c22b7daa85c590bef9
i18n: detect UI language without POSIX-style locale variable on Windows (BC)

On Windows, it isn't common to set LANG environment variable. This patch makes
gettext honor Windows-style UI language [1] if no locale variables are set.

Because of this change, LANG=C or HGPLAIN must be set in order to disable
translation on non-English Windows.

 [1]: http://msdn.microsoft.com/en-us/library/dd374098(v=VS.85).aspx
Matt Mackall - Aug. 4, 2014, 12:40 a.m.
On Sun, 2014-08-03 at 21:36 +0900, Yuya Nishihara wrote:
> This is copied from i18n module of TortoiseHg.  I'm not sure if Windows CLI
> users like it.

Queued for default, thanks. I'm a little worried about this one, but
since THG has presumably been doing this for a long time, we'll probably
get away with it.

Patch

diff --git a/mercurial/i18n.py b/mercurial/i18n.py
--- a/mercurial/i18n.py
+++ b/mercurial/i18n.py
@@ -6,7 +6,7 @@ 
 # GNU General Public License version 2 or any later version.
 
 import encoding
-import gettext, sys, os
+import gettext, sys, os, locale
 
 # modelled after templater.templatepath:
 if getattr(sys, 'frozen', None) is not None:
@@ -20,7 +20,25 @@  for dir in ('.', '..'):
     if os.path.isdir(localedir):
         break
 
-t = gettext.translation('hg', localedir, fallback=True)
+_languages = None
+if (os.name == 'nt'
+    and 'LANGUAGE' not in os.environ
+    and 'LC_ALL' not in os.environ
+    and 'LC_MESSAGES' not in os.environ
+    and 'LANG' not in os.environ):
+    # Try to detect UI language by "User Interface Language Management" API
+    # if no locale variables are set. Note that locale.getdefaultlocale()
+    # uses GetLocaleInfo(), which may be different from UI language.
+    # (See http://msdn.microsoft.com/en-us/library/dd374098(v=VS.85).aspx )
+    try:
+        import ctypes
+        langid = ctypes.windll.kernel32.GetUserDefaultUILanguage()
+        _languages = [locale.windows_locale[langid]]
+    except (ImportError, AttributeError, KeyError):
+        # ctypes not found or unknown langid
+        pass
+
+t = gettext.translation('hg', localedir, _languages, fallback=True)
 
 def gettext(message):
     """Translate message.