l10n quality fixes

This commit is contained in:
lakshyacis 2019-10-22 19:52:56 +05:30
parent afce500085
commit 1181db66e0
No known key found for this signature in database
GPG Key ID: D2C539C8EC63E9EB

View File

@ -1,4 +1,6 @@
"""
Localization
"""
import logging import logging
import os import os
import time import time
@ -49,7 +51,7 @@ except:
if BMConfigParser().has_option('bitmessagesettings', 'timeformat'): if BMConfigParser().has_option('bitmessagesettings', 'timeformat'):
time_format = BMConfigParser().get('bitmessagesettings', 'timeformat') time_format = BMConfigParser().get('bitmessagesettings', 'timeformat')
#Test the format string # Test the format string
try: try:
time.strftime(time_format) time.strftime(time_format)
except: except:
@ -58,48 +60,52 @@ if BMConfigParser().has_option('bitmessagesettings', 'timeformat'):
else: else:
time_format = DEFAULT_TIME_FORMAT time_format = DEFAULT_TIME_FORMAT
#It seems some systems lie about the encoding they use so we perform # It seems some systems lie about the encoding they use so we perform
#comprehensive decoding tests # comprehensive decoding tests
if time_format != DEFAULT_TIME_FORMAT: if time_format != DEFAULT_TIME_FORMAT:
try: try:
#Check day names # Check day names
for i in xrange(7): for i in xrange(7):
unicode(time.strftime(time_format, (0, 0, 0, 0, 0, 0, i, 0, 0)), encoding) unicode(time.strftime(time_format, (0, 0, 0, 0, 0, 0, i, 0, 0)), encoding)
#Check month names # Check month names
for i in xrange(1, 13): for i in xrange(1, 13):
unicode(time.strftime(time_format, (0, i, 0, 0, 0, 0, 0, 0, 0)), encoding) unicode(time.strftime(time_format, (0, i, 0, 0, 0, 0, 0, 0, 0)), encoding)
#Check AM/PM # Check AM/PM
unicode(time.strftime(time_format, (0, 0, 0, 11, 0, 0, 0, 0, 0)), encoding) unicode(time.strftime(time_format, (0, 0, 0, 11, 0, 0, 0, 0, 0)), encoding)
unicode(time.strftime(time_format, (0, 0, 0, 13, 0, 0, 0, 0, 0)), encoding) unicode(time.strftime(time_format, (0, 0, 0, 13, 0, 0, 0, 0, 0)), encoding)
#Check DST # Check DST
unicode(time.strftime(time_format, (0, 0, 0, 0, 0, 0, 0, 0, 1)), encoding) unicode(time.strftime(time_format, (0, 0, 0, 0, 0, 0, 0, 0, 1)), encoding)
except: except:
logger.exception('Could not decode locale formatted timestamp') logger.exception('Could not decode locale formatted timestamp')
time_format = DEFAULT_TIME_FORMAT time_format = DEFAULT_TIME_FORMAT
encoding = DEFAULT_ENCODING encoding = DEFAULT_ENCODING
def setlocale(category, newlocale): def setlocale(category, newlocale):
"""Set the locale"""
locale.setlocale(category, newlocale) locale.setlocale(category, newlocale)
# it looks like some stuff isn't initialised yet when this is called the # 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 # first time and its init gets the locale settings from the environment
os.environ["LC_ALL"] = newlocale os.environ["LC_ALL"] = newlocale
def formatTimestamp(timestamp = None, as_unicode = True):
#For some reason some timestamps are strings so we need to sanitize. def formatTimestamp(timestamp=None, as_unicode=True):
"""Return a formatted timestamp"""
# For some reason some timestamps are strings so we need to sanitize.
if timestamp is not None and not isinstance(timestamp, int): if timestamp is not None and not isinstance(timestamp, int):
try: try:
timestamp = int(timestamp) timestamp = int(timestamp)
except: except:
timestamp = None timestamp = None
#timestamp can't be less than 0. # timestamp can't be less than 0.
if timestamp is not None and timestamp < 0: if timestamp is not None and timestamp < 0:
timestamp = None timestamp = None
if timestamp is None: if timestamp is None:
timestring = time.strftime(time_format) timestring = time.strftime(time_format)
else: else:
#In case timestamp is too far in the future # In case timestamp is too far in the future
try: try:
timestring = time.strftime(time_format, time.localtime(timestamp)) timestring = time.strftime(time_format, time.localtime(timestamp))
except ValueError: except ValueError:
@ -109,17 +115,21 @@ def formatTimestamp(timestamp = None, as_unicode = True):
return unicode(timestring, encoding) return unicode(timestring, encoding)
return timestring return timestring
def getTranslationLanguage(): def getTranslationLanguage():
userlocale = None """Return the user's language choice"""
if BMConfigParser().has_option('bitmessagesettings', 'userlocale'): userlocale = BMConfigParser().safeGet(
userlocale = BMConfigParser().get('bitmessagesettings', 'userlocale') 'bitmessagesettings', 'userlocale', 'system')
return userlocale if userlocale and userlocale != 'system' else language
if userlocale in [None, '', 'system']:
return language
return userlocale
def getWindowsLocale(posixLocale): def getWindowsLocale(posixLocale):
"""
Get the Windows locale
Technically this converts the locale string from UNIX to Windows format,
because they use different ones in their
libraries. E.g. "en_EN.UTF-8" to "english".
"""
if posixLocale in windowsLanguageMap: if posixLocale in windowsLanguageMap:
return windowsLanguageMap[posixLocale] return windowsLanguageMap[posixLocale]
if "." in posixLocale: if "." in posixLocale: