handles exception thrown when no localization is detected refs #118

This commit is contained in:
Chris Caron 2019-06-04 22:13:15 -04:00 committed by GitHub
parent cbd35c3b7e
commit f373e41a91
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 48 additions and 14 deletions

View File

@ -192,9 +192,17 @@ class AppriseLocale(object):
if hasattr(ctypes, 'windll'): if hasattr(ctypes, 'windll'):
windll = ctypes.windll.kernel32 windll = ctypes.windll.kernel32
try:
lang = locale.windows_locale[ lang = locale.windows_locale[
windll.GetUserDefaultUILanguage()] windll.GetUserDefaultUILanguage()]
else:
# Our detected windows language
return lang[0:2].lower()
except (TypeError, KeyError):
# Fallback to posix detection
pass
try: try:
# Detect language # Detect language
lang = locale.getdefaultlocale()[0] lang = locale.getdefaultlocale()[0]
@ -204,4 +212,4 @@ class AppriseLocale(object):
# we're done in this case # we're done in this case
return None return None
return lang[0:2].lower() return None if not lang else lang[0:2].lower()

View File

@ -27,6 +27,7 @@ import mock
import ctypes import ctypes
from apprise import AppriseLocale from apprise import AppriseLocale
from apprise.utils import environ
try: try:
# Python v3.4+ # Python v3.4+
@ -133,8 +134,7 @@ def test_gettext_installs(mock_gettext_trans):
pass pass
@mock.patch('locale.getdefaultlocale') def test_detect_language_windows_users():
def test_detect_language(mock_getlocale):
""" """
API: Apprise() Detect language API: Apprise() Detect language
@ -147,14 +147,40 @@ def test_detect_language(mock_getlocale):
setattr(ctypes, 'windll', windll) setattr(ctypes, 'windll', windll)
# The below accesses the windows fallback code # The below accesses the windows fallback code
with environ('LANG', 'LANGUAGE', 'LC_ALL', 'LC_CTYPE', LANG="en_CA"):
assert AppriseLocale.AppriseLocale.detect_language() == 'en' assert AppriseLocale.AppriseLocale.detect_language() == 'en'
assert AppriseLocale.AppriseLocale\ assert AppriseLocale.AppriseLocale\
.detect_language(detect_fallback=False) is None .detect_language(detect_fallback=False) is None
# 0 = IndexError
windll.kernel32.GetUserDefaultUILanguage.return_value = 0
setattr(ctypes, 'windll', windll)
with environ('LANG', 'LC_ALL', 'LC_CTYPE', LANGUAGE="en_CA"):
assert AppriseLocale.AppriseLocale.detect_language() == 'en'
# The below accesses the windows fallback code and fail
# then it will resort to the environment variables
with environ('LANG', 'LANGUAGE', 'LC_ALL', 'LC_CTYPE'):
# Language can't be detected
assert AppriseLocale.AppriseLocale.detect_language() is None
with environ('LANGUAGE', 'LC_ALL', 'LC_CTYPE', LANG="fr_CA"):
# Detect french language
assert AppriseLocale.AppriseLocale.detect_language() == 'fr'
# Tidy
delattr(ctypes, 'windll')
@mock.patch('locale.getdefaultlocale')
def test_detect_language_defaultlocale(mock_getlocale):
"""
API: Apprise() Default locale detection
"""
# Handle case where getdefaultlocale() can't be detected # Handle case where getdefaultlocale() can't be detected
mock_getlocale.return_value = None mock_getlocale.return_value = None
delattr(ctypes, 'windll')
assert AppriseLocale.AppriseLocale.detect_language() is None assert AppriseLocale.AppriseLocale.detect_language() is None
# if detect_language and windows env fail us, then we don't # if detect_language and windows env fail us, then we don't