codecov-io: # [Codecov](https://codecov.io/gh/caronc/apprise/pull/119?src=pr&el=h1) Report
> Merging [#119](https://codecov.io/gh/caronc/apprise/pull/119?src=pr&el=desc) into [master](https://codecov.io/gh/caronc/apprise/commit/cbd35c3b7e4b0559fbdb16b481d7c2cabbb602a1?src=pr&el=desc) will **not change** coverage.
> The diff coverage is `100%`.

[![Impacted file tree graph](https://codecov.io/gh/caronc/apprise/pull/119/graphs/tree.svg?width=650&token=skAfz96t81&height=150&src=pr)](https://codecov.io/gh/caronc/apprise/pull/119?src=pr&el=tree)

```diff
@@          Coverage Diff          @@
##           master   #119   +/-   ##
=====================================
  Coverage     100%   100%           
=====================================
  Files          48     48           
  Lines        5313   5317    +4     
  Branches      817    817           
=====================================
+ Hits         5313   5317    +4
```


| [Impacted Files](https://codecov.io/gh/caronc/apprise/pull/119?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [apprise/AppriseLocale.py](https://codecov.io/gh/caronc/apprise/pull/119/diff?src=pr&el=tree#diff-YXBwcmlzZS9BcHByaXNlTG9jYWxlLnB5) | `100% <100%> (ø)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/caronc/apprise/pull/119?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/caronc/apprise/pull/119?src=pr&el=footer). Last update [cbd35c3...6a66f6d](https://codecov.io/gh/caronc/apprise/pull/119?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).

diff --git a/apprise/AppriseLocale.py b/apprise/AppriseLocale.py
index ea88acc..714d201 100644
--- a/apprise/AppriseLocale.py
+++ b/apprise/AppriseLocale.py
@@ -192,16 +192,24 @@ class AppriseLocale(object):
 
             if hasattr(ctypes, 'windll'):
                 windll = ctypes.windll.kernel32
-                lang = locale.windows_locale[
-                    windll.GetUserDefaultUILanguage()]
-            else:
                 try:
-                    # Detect language
-                    lang = locale.getdefaultlocale()[0]
+                    lang = locale.windows_locale[
+                        windll.GetUserDefaultUILanguage()]
+
+                    # Our detected windows language
+                    return lang[0:2].lower()
+
+                except (TypeError, KeyError):
+                    # Fallback to posix detection
+                    pass
 
-                except TypeError:
-                    # None is returned if the default can't be determined
-                    # we're done in this case
-                    return None
+            try:
+                # Detect language
+                lang = locale.getdefaultlocale()[0]
+
+            except TypeError:
+                # None is returned if the default can't be determined
+                # we're done in this case
+                return None
 
-        return lang[0:2].lower()
+        return None if not lang else lang[0:2].lower()

AppriseLocale.py TypeError: 'NoneType' object has no attribute '__getitem__'
An system without any universal language identifier existing in it's environment at all causes an unhanded exception:
```python
  File "/usr/lib/python2.7/site-packages/apprise/Apprise.py", line 78, in __init__
    self.locale = AppriseLocale()
  File "/usr/lib/python2.7/site-packages/apprise/AppriseLocale.py", line 111, in __init__
    self.lang = AppriseLocale.detect_language(language)
  File "/usr/lib/python2.7/site-packages/apprise/AppriseLocale.py", line 207, in detect_language
    return lang[0:2].lower()
TypeError: 'NoneType' object has no attribute '__getitem__'
```

This was identified [here](https://github.com/linuxserver/docker-lazylibrarian/issues/30).
diff --git a/test/test_locale.py b/test/test_locale.py
index 15de3d1..ce2fcc8 100644
--- a/test/test_locale.py
+++ b/test/test_locale.py
@@ -27,6 +27,7 @@ import mock
 import ctypes
 
 from apprise import AppriseLocale
+from apprise.utils import environ
 
 try:
     # Python v3.4+
@@ -133,8 +134,7 @@ def test_gettext_installs(mock_gettext_trans):
         pass
 
 
-@mock.patch('locale.getdefaultlocale')
-def test_detect_language(mock_getlocale):
+def test_detect_language_windows_users():
     """
     API: Apprise() Detect language
 
@@ -147,14 +147,40 @@ def test_detect_language(mock_getlocale):
         setattr(ctypes, 'windll', windll)
 
     # The below accesses the windows fallback code
-    assert AppriseLocale.AppriseLocale.detect_language() == 'en'
+    with environ('LANG', 'LANGUAGE', 'LC_ALL', 'LC_CTYPE', LANG="en_CA"):
+        assert AppriseLocale.AppriseLocale.detect_language() == 'en'
 
     assert AppriseLocale.AppriseLocale\
         .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
     mock_getlocale.return_value = None
-    delattr(ctypes, 'windll')
     assert AppriseLocale.AppriseLocale.detect_language() is None
 
     # if detect_language and windows env fail us, then we don't

pip install -e .[dev]
parse_log_pytest
requirements.txt
pytest pytest-cov tox coverage flake8 mock
3.9
requirements.txt
pytest --no-header -rA --tb=line --color=no -p no:cacheprovider -W ignore::DeprecationWarning
-e git+https://github.com/caronc/apprise.git@cbd35c3b7e4b0559fbdb16b481d7c2cabbb602a1#egg=apprise
cachetools==5.5.2
certifi==2025.1.31
chardet==5.2.0
charset-normalizer==3.4.1
click==8.1.8
colorama==0.4.6
coverage==7.8.0
decorator==5.2.1
distlib==0.3.9
exceptiongroup==1.2.2
filelock==3.18.0
flake8==7.2.0
idna==3.10
importlib_metadata==8.6.1
iniconfig==2.1.0
Markdown==3.7
mccabe==0.7.0
mock==5.2.0
oauthlib==3.2.2
packaging==24.2
platformdirs==4.3.7
pluggy==1.5.0
pycodestyle==2.13.0
pyflakes==3.3.2
pyproject-api==1.9.0
pytest==8.3.5
pytest-cov==6.0.0
PyYAML==6.0.2
requests==2.32.3
requests-oauthlib==2.0.0
six==1.17.0
tomli==2.2.1
tox==4.25.0
typing_extensions==4.13.0
urllib3==2.3.0
virtualenv==20.29.3
zipp==3.21.0

name: apprise
channels:
  - defaults
  - https://repo.anaconda.com/pkgs/main
  - https://repo.anaconda.com/pkgs/r
  - conda-forge
dependencies:
  - _libgcc_mutex=0.1=main
  - _openmp_mutex=5.1=1_gnu
  - ca-certificates=2025.2.25=h06a4308_0
  - ld_impl_linux-64=2.40=h12ee557_0
  - libffi=3.4.4=h6a678d5_1
  - libgcc-ng=11.2.0=h1234567_1
  - libgomp=11.2.0=h1234567_1
  - libstdcxx-ng=11.2.0=h1234567_1
  - ncurses=6.4=h6a678d5_0
  - openssl=3.0.16=h5eee18b_0
  - pip=25.0=py39h06a4308_0
  - python=3.9.21=he870216_1
  - readline=8.2=h5eee18b_0
  - setuptools=75.8.0=py39h06a4308_0
  - sqlite=3.45.3=h5eee18b_0
  - tk=8.6.14=h39e8969_0
  - tzdata=2025a=h04d1e81_0
  - wheel=0.45.1=py39h06a4308_0
  - xz=5.6.4=h5eee18b_1
  - zlib=1.2.13=h5eee18b_1
  - pip:
      - cachetools==5.5.2
      - certifi==2025.1.31
      - chardet==5.2.0
      - charset-normalizer==3.4.1
      - click==8.1.8
      - colorama==0.4.6
      - coverage==7.8.0
      - decorator==5.2.1
      - distlib==0.3.9
      - exceptiongroup==1.2.2
      - filelock==3.18.0
      - flake8==7.2.0
      - idna==3.10
      - importlib-metadata==8.6.1
      - iniconfig==2.1.0
      - markdown==3.7
      - mccabe==0.7.0
      - mock==5.2.0
      - oauthlib==3.2.2
      - packaging==24.2
      - platformdirs==4.3.7
      - pluggy==1.5.0
      - pycodestyle==2.13.0
      - pyflakes==3.3.2
      - pyproject-api==1.9.0
      - pytest==8.3.5
      - pytest-cov==6.0.0
      - pyyaml==6.0.2
      - requests==2.32.3
      - requests-oauthlib==2.0.0
      - six==1.17.0
      - tomli==2.2.1
      - tox==4.25.0
      - typing-extensions==4.13.0
      - urllib3==2.3.0
      - virtualenv==20.29.3
      - zipp==3.21.0
prefix: /opt/conda/envs/apprise

test/test_locale.py::test_detect_language_windows_users test/test_locale.py::test_detect_language_defaultlocale
test/test_locale.py::test_apprise_locale test/test_locale.py::test_gettext_init test/test_locale.py::test_gettext_translations test/test_locale.py::test_gettext_installs
apprise/AppriseLocale.py
apprise/AppriseLocale.py