Commits

Yuya Nishihara committed bf9d3ee

i18n: detect UI language more properly on Windows

It uses GetUserDefaultUILanguage() instead of locale.getdefaultlocale(),
because locale may differ from UI language.

Due to lack of GetUserDefaultUILanguage of pywin32, it requires ctypes module.

  • Participants
  • Parent commits 1c5b14d
  • Branches stable

Comments (0)

Files changed (1)

tortoisehg/util/i18n.py

 def _defaultlanguage():
     if os.name != 'nt' or util.any(e in os.environ for e in _localeenvs):
         return  # honor posix-style env var
-    # On Windows, system locale is provided by getdefaultlocale(), but
-    # gettext doesn't take it into account.
-    return locale.getdefaultlocale()[0]
+
+    # On Windows, UI language can be determined by GetUserDefaultUILanguage(),
+    # but gettext doesn't take it into account.
+    # Note that locale.getdefaultlocale() uses GetLocaleInfo(), which may be
+    # different from UI language.
+    #
+    # For details, please read "User Interface Language Management":
+    # http://msdn.microsoft.com/en-us/library/dd374098(v=VS.85).aspx
+    try:
+        from ctypes import windll  # requires Python>=2.5
+        langid = windll.kernel32.GetUserDefaultUILanguage()
+        return locale.windows_locale[langid]
+    except (ImportError, AttributeError, KeyError):
+        pass
 
 def setlanguage(lang=None):
     """Change translation catalog to the specified language"""