From 813382fc7d39313e67651e8daafaa2db121c9a0b Mon Sep 17 00:00:00 2001 From: Chris Caron Date: Sat, 19 Aug 2023 23:33:07 -0400 Subject: [PATCH] no longer call setlocale() as advised by Python docs --- apprise/AppriseLocale.py | 28 +++++++++++++++------------- test/test_locale.py | 3 +-- 2 files changed, 16 insertions(+), 15 deletions(-) diff --git a/apprise/AppriseLocale.py b/apprise/AppriseLocale.py index ede2ebbc9e..6dce6c481c 100644 --- a/apprise/AppriseLocale.py +++ b/apprise/AppriseLocale.py @@ -27,6 +27,7 @@ import locale import contextlib import os +import re from os.path import join from os.path import dirname from os.path import abspath @@ -89,6 +90,17 @@ class AppriseLocale: """ + # Locale regular expression + _local_re = re.compile( + r'^\s*(?P[a-z]{2})([_:]((?P[a-z]{2}))?' + r'(\.(?P[a-z0-9]+))?|.+)?', re.IGNORECASE) + + # Define our default encoding + _default_encoding = 'utf-8' + + # Define our default language + _default_language = 'en' + def __init__(self, language=None): """ Initializes our object, if a language is specified, then we @@ -203,19 +215,9 @@ def detect_language(lang=None, detect_fallback=True): for variable in ('LC_ALL', 'LC_CTYPE', 'LANG', 'LANGUAGE'): localename = lookup(variable, None) if localename: - if variable == 'LANGUAGE': - localename = localename.split(':')[0] - - try: - locale.setlocale(locale.LC_ALL, localename) - - except locale.Error: - logger.warning( - 'Invalid language define %s=%s', - variable, - lookup(variable, None)) - continue - break + result = AppriseLocale._local_re.match(localename) + if result and result.group('lang'): + return result.group('lang').lower() try: # Acquire our locale diff --git a/test/test_locale.py b/test/test_locale.py index fc76ccc5df..e3bedd4c42 100644 --- a/test/test_locale.py +++ b/test/test_locale.py @@ -157,8 +157,7 @@ def test_detect_language_windows_users(): assert AppriseLocale.AppriseLocale.detect_language() == 'en' -@pytest.mark.skipif(sys.platform == "win32", reason="Does not work on Windows") -def test_detect_language_windows_users_croaks_please_review(): +def test_detect_language_using_env(): """ When enabling CI testing on Windows, those tests did not produce the correct results. They may want to be reviewed.