Windows locale fix

Python locales (e.g. for time and date) didn't work on Windows.
This commit is contained in:
mailchuck 2016-04-27 12:15:30 +02:00 committed by Peter Surda
parent 3be851297c
commit b7fefb3c40
2 changed files with 41 additions and 1 deletions

View File

@ -104,8 +104,11 @@ 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)
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)

View File

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