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
--- 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.