From b7fefb3c40a7a8dd17f886a58eb9443b80083f0b Mon Sep 17 00:00:00 2001 From: mailchuck Date: Wed, 27 Apr 2016 12:15:30 +0200 Subject: [PATCH] Windows locale fix Python locales (e.g. for time and date) didn't work on Windows. --- src/bitmessageqt/__init__.py | 5 ++++- src/l10n.py | 37 ++++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/src/bitmessageqt/__init__.py b/src/bitmessageqt/__init__.py index 51d6fba5..80bb7a05 100644 --- a/src/bitmessageqt/__init__.py +++ b/src/bitmessageqt/__init__.py @@ -104,9 +104,12 @@ def change_translation(locale): QtGui.QApplication.installTranslator(qsystranslator) lang = pythonlocale.normalize(l10n.getTranslationLanguage()) + if 'win32' in sys.platform or 'win64' in sys.platform: + lang = l10n.getWindowsLocale(lang) try: pythonlocale.setlocale(pythonlocale.LC_ALL, lang) - l10n.encoding = pythonlocale.nl_langinfo(pythonlocale.CODESET) + if 'win32' not in sys.platform and 'win64' not in sys.platform: + l10n.encoding = pythonlocale.nl_langinfo(pythonlocale.CODESET) except: logger.error("Failed to set locale to %s", lang, exc_info=True) diff --git a/src/l10n.py b/src/l10n.py index f9c9b456..a32abb84 100644 --- a/src/l10n.py +++ b/src/l10n.py @@ -16,6 +16,29 @@ DEFAULT_TIME_FORMAT = '%Y-%m-%d %H:%M:%S' encoding = DEFAULT_ENCODING language = DEFAULT_LANGUAGE +windowsLanguageMap = { + "ar": "arabic", + "cs": "czech", + "da": "danish", + "de": "german", + "en": "english", + "eo": "esperanto", + "fr": "french", + "it": "italian", + "ja": "japanese", + "nl": "dutch", + "no": "norwegian", + "pl": "polish", + "pt": "portuguese", + "ru": "russian", + "sk": "slovak", + "zh": "chinese", + "zh_CN": "chinese-simplified", + "zh_HK": "chinese-traditional", + "zh_SG": "chinese-simplified", + "zh_TW": "chinese-traditional" +} + try: import locale encoding = locale.getpreferredencoding(True) or DEFAULT_ENCODING @@ -91,3 +114,17 @@ def getTranslationLanguage(): return userlocale +def getWindowsLocale(posixLocale): + if posixLocale in windowsLanguageMap: + return windowsLanguageMap[posixLocale] + if "." in posixLocale: + loc = posixLocale.split(".", 1) + if loc[0] in windowsLanguageMap: + return windowsLanguageMap[loc[0]] + if "_" in posixLocale: + loc = posixLocale.split("_", 1) + if loc[0] in windowsLanguageMap: + return windowsLanguageMap[loc[0]] + if posixLocale != DEFAULT_LANGUAGE: + return getWindowsLocale(DEFAULT_LANGUAGE) + return None