changeset 21987:4953cd193e84

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
author Yuya Nishihara <yuya@tcha.org>
date Sun, 03 Aug 2014 19:19:23 +0900
parents 48166e46f111
children 12cd3827b860
files mercurial/i18n.py
diffstat 1 files changed, 20 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/i18n.py	Fri Aug 01 22:16:54 2014 -0700
+++ b/mercurial/i18n.py	Sun Aug 03 19:19:23 2014 +0900
@@ -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 @@
     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.