# Buggy code
def detect_language(lang=None, detect_fallback=True):
    """
    returns the language (if it's retrievable)
    """
    # We want to only use the 2 character version of this language
    # hence en_CA becomes en, en_US becomes en.
    if not isinstance(lang, six.string_types):
        if detect_fallback is False:
            # no detection enabled; we're done
            return None

        if hasattr(ctypes, "windll"):
            windll = ctypes.windll.kernel32
            try:
                lang = locale.windows_locale[windll.GetUserDefaultUILanguage()]

                # Our detected windows language
                return lang[0:2].lower()

            except (TypeError, KeyError):
                # Fallback to posix detection
                pass

        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 None if not lang else lang[0:2].lower()


# Traceback (ValueError)
(main:220) - Bazarr is being restarted...
Bazarr starting...
Traceback (most recent call last):
File "bazarr/main.py", line 75, in <module>
update_notifier()
File "/Users/USER/bazarr/bazarr/bazarr/notifier.py", line 13, in update_notifier
a = apprise.Apprise()
File "/Users/USER/bazarr/bazarr/bazarr/../libs/apprise/Apprise.py", line 78, in __init__
self.locale = AppriseLocale()
File "/Users/USER/bazarr/bazarr/bazarr/../libs/apprise/AppriseLocale.py", line 111, in __init__
self.lang = AppriseLocale.detect_language(language)
File "/Users/USER/bazarr/bazarr/bazarr/../libs/apprise/AppriseLocale.py", line 208, in detect_language
lang = locale.getdefaultlocale()[0]
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/locale.py", line 543, in getdefaultlocale
return _parse_localename(localename)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/locale.py", line 475, in _parse_localename
raise ValueError, 'unknown locale: %s' % localename
ValueError: unknown locale: UTF-8

# Fixed code
def detect_language(lang=None, detect_fallback=True):
    """
    returns the language (if it's retrievable)
    """
    # We want to only use the 2 character version of this language
    # hence en_CA becomes en, en_US becomes en.
    if not isinstance(lang, six.string_types):
        if detect_fallback is False:
            # no detection enabled; we're done
            return None

        if hasattr(ctypes, "windll"):
            windll = ctypes.windll.kernel32
            try:
                lang = locale.windows_locale[windll.GetUserDefaultUILanguage()]

                # Our detected windows language
                return lang[0:2].lower()

            except (TypeError, KeyError):
                # Fallback to posix detection
                pass

        try:
            # Detect language
            lang = locale.getdefaultlocale()[0]

        except ValueError as e:
            # This occurs when an invalid locale was parsed from the
            # environment variable. While we still return None in this
            # case, we want to better notify the end user of this. Users
            # receiving this error should check their environment
            # variables.
            logger.warning("Language detection failure / {}".format(str(e)))
            return None

        except TypeError:
            # None is returned if the default can't be determined
            # we're done in this case
            return None

    return None if not lang else lang[0:2].lower()