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)


 def _defaultlanguage():
     if != '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":
+    #
+    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"""