Skip to content

Commit

Permalink
no longer call setlocale() as advised by Python docs
Browse files Browse the repository at this point in the history
  • Loading branch information
caronc committed Aug 20, 2023
1 parent f0499f9 commit 1b1d116
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 15 deletions.
28 changes: 15 additions & 13 deletions apprise/AppriseLocale.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import locale
import contextlib
import os
import re
from os.path import join
from os.path import dirname
from os.path import abspath
Expand Down Expand Up @@ -96,6 +97,17 @@ class AppriseLocale:
"""

# Locale regular expression
_local_re = re.compile(
r'^\s*(?P<lang>[a-z]{2})([_:]((?P<country>[a-z]{2}))?'
r'(\.(?P<enc>[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
Expand Down Expand Up @@ -210,19 +222,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
Expand Down
3 changes: 1 addition & 2 deletions test/test_locale.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,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.
Expand Down

0 comments on commit 1b1d116

Please sign in to comment.