We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
Try simpler language code as fallback before settings.LANGUAGE_CODE
In https://github.com/translate/pootle/blob/10913224/pootle/i18n/override.py#L87-L101 if the language code `it-IT` (for example) is tried and eventually falls back to `settings.LANGUAGE_CODE`, but it makes sense to first try `it` (simpler version of `it-IT`) before falling back to `settings.LANGUAGE_CODE`.

--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `pootle/i18n/override.py`
Content:
```
1 # -*- coding: utf-8 -*-
2 #
3 # Copyright (C) Pootle contributors.
4 #
5 # This file is a part of the Pootle project. It is distributed under the GPL3
6 # or later license. See the LICENSE file for a copy of the license and the
7 # AUTHORS file for copyright and authorship information.
8 
9 """Overrides and support functions for arbitrary locale support."""
10 
11 import os
12 
13 from translate.lang import data
14 
15 from django.utils import translation
16 from django.utils.translation import LANGUAGE_SESSION_KEY, trans_real
17 
18 from pootle.i18n import gettext
19 
20 
21 def find_languages(locale_path):
22     """Generate supported languages list from the :param:`locale_path`
23     directory.
24     """
25     dirs = os.listdir(locale_path)
26     langs = []
27     for lang in dirs:
28         if (data.langcode_re.match(lang) and
29             os.path.isdir(os.path.join(locale_path, lang))):
30             langs.append((trans_real.to_language(lang),
31                           data.languages.get(lang, (lang,))[0]))
32     return langs
33 
34 
35 def supported_langs():
36     """Returns a list of supported locales."""
37     from django.conf import settings
38     return settings.LANGUAGES
39 
40 
41 def get_lang_from_session(request, supported):
42     if hasattr(request, 'session'):
43         lang_code = request.session.get(LANGUAGE_SESSION_KEY, None)
44         if lang_code and lang_code in supported:
45             return lang_code
46 
47     return None
48 
49 
50 def get_lang_from_cookie(request, supported):
51     """See if the user's browser sent a cookie with a preferred language."""
52     from django.conf import settings
53     lang_code = request.COOKIES.get(settings.LANGUAGE_COOKIE_NAME)
54 
55     if lang_code and lang_code in supported:
56         return lang_code
57 
58     return None
59 
60 
61 def get_lang_from_http_header(request, supported):
62     """If the user's browser sends a list of preferred languages in the
63     HTTP_ACCEPT_LANGUAGE header, parse it into a list. Then walk through
64     the list, and for each entry, we check whether we have a matching
65     pootle translation project. If so, we return it.
66 
67     If nothing is found, return None.
68     """
69     accept = request.META.get('HTTP_ACCEPT_LANGUAGE', '')
70     for accept_lang, __ in trans_real.parse_accept_lang_header(accept):
71         if accept_lang == '*':
72             return None
73 
74         normalized = data.normalize_code(data.simplify_to_common(accept_lang))
75         if normalized in ['en-us', 'en']:
76             return None
77         if normalized in supported:
78             return normalized
79 
80         # FIXME: horribly slow way of dealing with languages with @ in them
81         for lang in supported.keys():
82             if normalized == data.normalize_code(lang):
83                 return lang
84     return None
85 
86 
87 def get_language_from_request(request, check_path=False):
88     """Try to get the user's preferred language by first checking the
89     cookie and then by checking the HTTP language headers.
90 
91     If all fails, try fall back to default language.
92     """
93     supported = dict(supported_langs())
94     for lang_getter in (get_lang_from_session,
95                         get_lang_from_cookie,
96                         get_lang_from_http_header):
97         lang = lang_getter(request, supported)
98         if lang is not None:
99             return lang
100     from django.conf import settings
101     return settings.LANGUAGE_CODE
102 
103 
104 def get_language_bidi():
105     """Override for Django's get_language_bidi that's aware of more RTL
106     languages.
107     """
108     return gettext.language_dir(translation.get_language()) == 'rtl'
109 
110 
111 def hijack_translation():
112     """Sabotage Django's fascist linguistical regime."""
113     # Override functions that check if language is known to Django
114     translation.check_for_language = lambda lang_code: True
115     trans_real.check_for_language = lambda lang_code: True
116     translation.get_language_from_request = get_language_from_request
117 
118     # Override django's inadequate bidi detection
119     translation.get_language_bidi = get_language_bidi
120
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/pootle/i18n/override.py b/pootle/i18n/override.py
--- a/pootle/i18n/override.py
+++ b/pootle/i18n/override.py
@@ -38,24 +38,35 @@
     return settings.LANGUAGES
 
 
-def get_lang_from_session(request, supported):
-    if hasattr(request, 'session'):
-        lang_code = request.session.get(LANGUAGE_SESSION_KEY, None)
-        if lang_code and lang_code in supported:
-            return lang_code
+def get_language_supported(lang_code, supported):
+    normalized = data.normalize_code(data.simplify_to_common(lang_code))
+    if normalized in supported:
+        return normalized
+
+    # FIXME: horribly slow way of dealing with languages with @ in them
+    for lang in supported.keys():
+        if normalized == data.normalize_code(lang):
+            return lang
 
     return None
 
 
+def get_lang_from_session(request, supported):
+    if not hasattr(request, 'session'):
+        return None
+    lang_code = request.session.get(LANGUAGE_SESSION_KEY, None)
+    if not lang_code:
+        return None
+    return get_language_supported(lang_code, supported)
+
+
 def get_lang_from_cookie(request, supported):
     """See if the user's browser sent a cookie with a preferred language."""
     from django.conf import settings
     lang_code = request.COOKIES.get(settings.LANGUAGE_COOKIE_NAME)
-
-    if lang_code and lang_code in supported:
-        return lang_code
-
-    return None
+    if not lang_code:
+        return None
+    return get_language_supported(lang_code, supported)
 
 
 def get_lang_from_http_header(request, supported):
@@ -70,17 +81,9 @@
     for accept_lang, __ in trans_real.parse_accept_lang_header(accept):
         if accept_lang == '*':
             return None
-
-        normalized = data.normalize_code(data.simplify_to_common(accept_lang))
-        if normalized in ['en-us', 'en']:
-            return None
-        if normalized in supported:
-            return normalized
-
-        # FIXME: horribly slow way of dealing with languages with @ in them
-        for lang in supported.keys():
-            if normalized == data.normalize_code(lang):
-                return lang
+        supported_lang = get_language_supported(accept_lang, supported)
+        if supported_lang:
+            return supported_lang
     return None
 
 
@@ -98,7 +101,9 @@
         if lang is not None:
             return lang
     from django.conf import settings
-    return settings.LANGUAGE_CODE
+    if settings.LANGUAGE_CODE in supported:
+        return settings.LANGUAGE_CODE
+    return 'en-us'
 
 
 def get_language_bidi():

{"golden_diff": "diff --git a/pootle/i18n/override.py b/pootle/i18n/override.py\n--- a/pootle/i18n/override.py\n+++ b/pootle/i18n/override.py\n@@ -38,24 +38,35 @@\n     return settings.LANGUAGES\n \n \n-def get_lang_from_session(request, supported):\n-    if hasattr(request, 'session'):\n-        lang_code = request.session.get(LANGUAGE_SESSION_KEY, None)\n-        if lang_code and lang_code in supported:\n-            return lang_code\n+def get_language_supported(lang_code, supported):\n+    normalized = data.normalize_code(data.simplify_to_common(lang_code))\n+    if normalized in supported:\n+        return normalized\n+\n+    # FIXME: horribly slow way of dealing with languages with @ in them\n+    for lang in supported.keys():\n+        if normalized == data.normalize_code(lang):\n+            return lang\n \n     return None\n \n \n+def get_lang_from_session(request, supported):\n+    if not hasattr(request, 'session'):\n+        return None\n+    lang_code = request.session.get(LANGUAGE_SESSION_KEY, None)\n+    if not lang_code:\n+        return None\n+    return get_language_supported(lang_code, supported)\n+\n+\n def get_lang_from_cookie(request, supported):\n     \"\"\"See if the user's browser sent a cookie with a preferred language.\"\"\"\n     from django.conf import settings\n     lang_code = request.COOKIES.get(settings.LANGUAGE_COOKIE_NAME)\n-\n-    if lang_code and lang_code in supported:\n-        return lang_code\n-\n-    return None\n+    if not lang_code:\n+        return None\n+    return get_language_supported(lang_code, supported)\n \n \n def get_lang_from_http_header(request, supported):\n@@ -70,17 +81,9 @@\n     for accept_lang, __ in trans_real.parse_accept_lang_header(accept):\n         if accept_lang == '*':\n             return None\n-\n-        normalized = data.normalize_code(data.simplify_to_common(accept_lang))\n-        if normalized in ['en-us', 'en']:\n-            return None\n-        if normalized in supported:\n-            return normalized\n-\n-        # FIXME: horribly slow way of dealing with languages with @ in them\n-        for lang in supported.keys():\n-            if normalized == data.normalize_code(lang):\n-                return lang\n+        supported_lang = get_language_supported(accept_lang, supported)\n+        if supported_lang:\n+            return supported_lang\n     return None\n \n \n@@ -98,7 +101,9 @@\n         if lang is not None:\n             return lang\n     from django.conf import settings\n-    return settings.LANGUAGE_CODE\n+    if settings.LANGUAGE_CODE in supported:\n+        return settings.LANGUAGE_CODE\n+    return 'en-us'\n \n \n def get_language_bidi():\n", "issue": "Try simpler language code as fallback before settings.LANGUAGE_CODE\nIn https://github.com/translate/pootle/blob/10913224/pootle/i18n/override.py#L87-L101 if the language code `it-IT` (for example) is tried and eventually falls back to `settings.LANGUAGE_CODE`, but it makes sense to first try `it` (simpler version of `it-IT`) before falling back to `settings.LANGUAGE_CODE`.\n", "before_files": [{"content": "# -*- coding: utf-8 -*-\n#\n# Copyright (C) Pootle contributors.\n#\n# This file is a part of the Pootle project. It is distributed under the GPL3\n# or later license. See the LICENSE file for a copy of the license and the\n# AUTHORS file for copyright and authorship information.\n\n\"\"\"Overrides and support functions for arbitrary locale support.\"\"\"\n\nimport os\n\nfrom translate.lang import data\n\nfrom django.utils import translation\nfrom django.utils.translation import LANGUAGE_SESSION_KEY, trans_real\n\nfrom pootle.i18n import gettext\n\n\ndef find_languages(locale_path):\n    \"\"\"Generate supported languages list from the :param:`locale_path`\n    directory.\n    \"\"\"\n    dirs = os.listdir(locale_path)\n    langs = []\n    for lang in dirs:\n        if (data.langcode_re.match(lang) and\n            os.path.isdir(os.path.join(locale_path, lang))):\n            langs.append((trans_real.to_language(lang),\n                          data.languages.get(lang, (lang,))[0]))\n    return langs\n\n\ndef supported_langs():\n    \"\"\"Returns a list of supported locales.\"\"\"\n    from django.conf import settings\n    return settings.LANGUAGES\n\n\ndef get_lang_from_session(request, supported):\n    if hasattr(request, 'session'):\n        lang_code = request.session.get(LANGUAGE_SESSION_KEY, None)\n        if lang_code and lang_code in supported:\n            return lang_code\n\n    return None\n\n\ndef get_lang_from_cookie(request, supported):\n    \"\"\"See if the user's browser sent a cookie with a preferred language.\"\"\"\n    from django.conf import settings\n    lang_code = request.COOKIES.get(settings.LANGUAGE_COOKIE_NAME)\n\n    if lang_code and lang_code in supported:\n        return lang_code\n\n    return None\n\n\ndef get_lang_from_http_header(request, supported):\n    \"\"\"If the user's browser sends a list of preferred languages in the\n    HTTP_ACCEPT_LANGUAGE header, parse it into a list. Then walk through\n    the list, and for each entry, we check whether we have a matching\n    pootle translation project. If so, we return it.\n\n    If nothing is found, return None.\n    \"\"\"\n    accept = request.META.get('HTTP_ACCEPT_LANGUAGE', '')\n    for accept_lang, __ in trans_real.parse_accept_lang_header(accept):\n        if accept_lang == '*':\n            return None\n\n        normalized = data.normalize_code(data.simplify_to_common(accept_lang))\n        if normalized in ['en-us', 'en']:\n            return None\n        if normalized in supported:\n            return normalized\n\n        # FIXME: horribly slow way of dealing with languages with @ in them\n        for lang in supported.keys():\n            if normalized == data.normalize_code(lang):\n                return lang\n    return None\n\n\ndef get_language_from_request(request, check_path=False):\n    \"\"\"Try to get the user's preferred language by first checking the\n    cookie and then by checking the HTTP language headers.\n\n    If all fails, try fall back to default language.\n    \"\"\"\n    supported = dict(supported_langs())\n    for lang_getter in (get_lang_from_session,\n                        get_lang_from_cookie,\n                        get_lang_from_http_header):\n        lang = lang_getter(request, supported)\n        if lang is not None:\n            return lang\n    from django.conf import settings\n    return settings.LANGUAGE_CODE\n\n\ndef get_language_bidi():\n    \"\"\"Override for Django's get_language_bidi that's aware of more RTL\n    languages.\n    \"\"\"\n    return gettext.language_dir(translation.get_language()) == 'rtl'\n\n\ndef hijack_translation():\n    \"\"\"Sabotage Django's fascist linguistical regime.\"\"\"\n    # Override functions that check if language is known to Django\n    translation.check_for_language = lambda lang_code: True\n    trans_real.check_for_language = lambda lang_code: True\n    translation.get_language_from_request = get_language_from_request\n\n    # Override django's inadequate bidi detection\n    translation.get_language_bidi = get_language_bidi\n", "path": "pootle/i18n/override.py"}], "after_files": [{"content": "# -*- coding: utf-8 -*-\n#\n# Copyright (C) Pootle contributors.\n#\n# This file is a part of the Pootle project. It is distributed under the GPL3\n# or later license. See the LICENSE file for a copy of the license and the\n# AUTHORS file for copyright and authorship information.\n\n\"\"\"Overrides and support functions for arbitrary locale support.\"\"\"\n\nimport os\n\nfrom translate.lang import data\n\nfrom django.utils import translation\nfrom django.utils.translation import LANGUAGE_SESSION_KEY, trans_real\n\nfrom pootle.i18n import gettext\n\n\ndef find_languages(locale_path):\n    \"\"\"Generate supported languages list from the :param:`locale_path`\n    directory.\n    \"\"\"\n    dirs = os.listdir(locale_path)\n    langs = []\n    for lang in dirs:\n        if (data.langcode_re.match(lang) and\n            os.path.isdir(os.path.join(locale_path, lang))):\n            langs.append((trans_real.to_language(lang),\n                          data.languages.get(lang, (lang,))[0]))\n    return langs\n\n\ndef supported_langs():\n    \"\"\"Returns a list of supported locales.\"\"\"\n    from django.conf import settings\n    return settings.LANGUAGES\n\n\ndef get_language_supported(lang_code, supported):\n    normalized = data.normalize_code(data.simplify_to_common(lang_code))\n    if normalized in supported:\n        return normalized\n\n    # FIXME: horribly slow way of dealing with languages with @ in them\n    for lang in supported.keys():\n        if normalized == data.normalize_code(lang):\n            return lang\n\n    return None\n\n\ndef get_lang_from_session(request, supported):\n    if not hasattr(request, 'session'):\n        return None\n    lang_code = request.session.get(LANGUAGE_SESSION_KEY, None)\n    if not lang_code:\n        return None\n    return get_language_supported(lang_code, supported)\n\n\ndef get_lang_from_cookie(request, supported):\n    \"\"\"See if the user's browser sent a cookie with a preferred language.\"\"\"\n    from django.conf import settings\n    lang_code = request.COOKIES.get(settings.LANGUAGE_COOKIE_NAME)\n    if not lang_code:\n        return None\n    return get_language_supported(lang_code, supported)\n\n\ndef get_lang_from_http_header(request, supported):\n    \"\"\"If the user's browser sends a list of preferred languages in the\n    HTTP_ACCEPT_LANGUAGE header, parse it into a list. Then walk through\n    the list, and for each entry, we check whether we have a matching\n    pootle translation project. If so, we return it.\n\n    If nothing is found, return None.\n    \"\"\"\n    accept = request.META.get('HTTP_ACCEPT_LANGUAGE', '')\n    for accept_lang, __ in trans_real.parse_accept_lang_header(accept):\n        if accept_lang == '*':\n            return None\n        supported_lang = get_language_supported(accept_lang, supported)\n        if supported_lang:\n            return supported_lang\n    return None\n\n\ndef get_language_from_request(request, check_path=False):\n    \"\"\"Try to get the user's preferred language by first checking the\n    cookie and then by checking the HTTP language headers.\n\n    If all fails, try fall back to default language.\n    \"\"\"\n    supported = dict(supported_langs())\n    for lang_getter in (get_lang_from_session,\n                        get_lang_from_cookie,\n                        get_lang_from_http_header):\n        lang = lang_getter(request, supported)\n        if lang is not None:\n            return lang\n    from django.conf import settings\n    if settings.LANGUAGE_CODE in supported:\n        return settings.LANGUAGE_CODE\n    return 'en-us'\n\n\ndef get_language_bidi():\n    \"\"\"Override for Django's get_language_bidi that's aware of more RTL\n    languages.\n    \"\"\"\n    return gettext.language_dir(translation.get_language()) == 'rtl'\n\n\ndef hijack_translation():\n    \"\"\"Sabotage Django's fascist linguistical regime.\"\"\"\n    # Override functions that check if language is known to Django\n    translation.check_for_language = lambda lang_code: True\n    trans_real.check_for_language = lambda lang_code: True\n    translation.get_language_from_request = get_language_from_request\n\n    # Override django's inadequate bidi detection\n    translation.get_language_bidi = get_language_bidi\n", "path": "pootle/i18n/override.py"}]}