From 025950c05eb8f9b4ea4e54e644f060c251ec73d5 Mon Sep 17 00:00:00 2001 From: Peter Surda Date: Tue, 13 Dec 2016 11:54:01 +0100 Subject: [PATCH] Fix locale init - date/time wasn't localised correctly on startup --- src/bitmessageqt/__init__.py | 18 +++++++++--------- src/l10n.py | 6 ++++++ 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/src/bitmessageqt/__init__.py b/src/bitmessageqt/__init__.py index a274212b..a4e101c0 100644 --- a/src/bitmessageqt/__init__.py +++ b/src/bitmessageqt/__init__.py @@ -48,7 +48,7 @@ from about import * from help import * from iconglossary import * from connect import * -import locale as pythonlocale +import locale import sys from time import strftime, localtime, gmtime import time @@ -83,7 +83,7 @@ def _translate(context, text, disambiguation = None, encoding = None, number = N else: return QtGui.QApplication.translate(context, text, None, QtCore.QCoreApplication.CodecForTr, number) -def change_translation(locale): +def change_translation(newlocale): global qmytranslator, qsystranslator try: if not qmytranslator.isEmpty(): @@ -97,29 +97,29 @@ def change_translation(locale): pass qmytranslator = QtCore.QTranslator() - translationpath = os.path.join (shared.codePath(), 'translations', 'bitmessage_' + locale) + translationpath = os.path.join (shared.codePath(), 'translations', 'bitmessage_' + newlocale) qmytranslator.load(translationpath) QtGui.QApplication.installTranslator(qmytranslator) qsystranslator = QtCore.QTranslator() if shared.frozen: - translationpath = os.path.join (shared.codePath(), 'translations', 'qt_' + locale) + translationpath = os.path.join (shared.codePath(), 'translations', 'qt_' + newlocale) else: - translationpath = os.path.join (str(QtCore.QLibraryInfo.location(QtCore.QLibraryInfo.TranslationsPath)), 'qt_' + locale) + translationpath = os.path.join (str(QtCore.QLibraryInfo.location(QtCore.QLibraryInfo.TranslationsPath)), 'qt_' + newlocale) qsystranslator.load(translationpath) QtGui.QApplication.installTranslator(qsystranslator) - lang = pythonlocale.normalize(l10n.getTranslationLanguage()) + lang = locale.normalize(l10n.getTranslationLanguage()) langs = [lang.split(".")[0] + "." + l10n.encoding, lang.split(".")[0] + "." + 'UTF-8', lang] if 'win32' in sys.platform or 'win64' in sys.platform: langs = [l10n.getWindowsLocale(lang)] for lang in langs: try: - pythonlocale.setlocale(pythonlocale.LC_ALL, lang) + l10n.setlocale(locale.LC_ALL, lang) if 'win32' not in sys.platform and 'win64' not in sys.platform: - l10n.encoding = pythonlocale.nl_langinfo(pythonlocale.CODESET) + l10n.encoding = locale.nl_langinfo(locale.CODESET) else: - l10n.encoding = pythonlocale.getlocale()[1] + l10n.encoding = locale.getlocale()[1] logger.info("Successfully set locale to %s", lang) break except: diff --git a/src/l10n.py b/src/l10n.py index a32abb84..0300085a 100644 --- a/src/l10n.py +++ b/src/l10n.py @@ -1,5 +1,6 @@ import logging +import os import time import shared @@ -78,6 +79,11 @@ if time_format != DEFAULT_TIME_FORMAT: time_format = DEFAULT_TIME_FORMAT encoding = DEFAULT_ENCODING +def setlocale(category, newlocale): + locale.setlocale(category, newlocale) + # it looks like some stuff isn't initialised yet when this is called the + # first time and its init gets the locale settings from the environment + os.environ["LC_ALL"] = newlocale def formatTimestamp(timestamp = None, as_unicode = True): #For some reason some timestamps are strings so we need to sanitize.