prompt: You will be provided with a partial code base and an issue statement explaining a problem to resolve.
  <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`.
  
  </issue>
  <code>
  [start of pootle/i18n/override.py]
  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 of pootle/i18n/override.py]
  </code>
  
  I need you to solve this issue by generating a single patch file that I can apply directly to this repository using git apply. Please respond with a single patch file in the following format.
  <patch>
  diff --git a/file.py b/file.py
  --- a/file.py
  +++ b/file.py
  @@ -1,27 +1,35 @@
   def euclidean(a, b):
  -    while b:
  -        a, b = b, a % b
  -    return a
  +    if b == 0:
  +        return a
  +    return euclidean(b, a % b)
   
   
   def bresenham(x0, y0, x1, y1):
       points = []
       dx = abs(x1 - x0)
       dy = abs(y1 - y0)
  -    sx = 1 if x0 < x1 else -1
  -    sy = 1 if y0 < y1 else -1
  -    err = dx - dy
  +    x, y = x0, y0
  +    sx = -1 if x0 > x1 else 1
  +    sy = -1 if y0 > y1 else 1
   
  -    while True:
  -        points.append((x0, y0))
  -        if x0 == x1 and y0 == y1:
  -            break
  -        e2 = 2 * err
  -        if e2 > -dy:
  -            err -= dy
  -            x0 += sx
  -        if e2 < dx:
  -            err += dx
  -            y0 += sy
  +    if dx > dy:
  +        err = dx / 2.0
  +        while x != x1:
  +            points.append((x, y))
  +            err -= dy
  +            if err < 0:
  +                y += sy
  +                err += dx
  +            x += sx
  +    else:
  +        err = dy / 2.0
  +        while y != y1:
  +            points.append((x, y))
  +            err -= dx
  +            if err < 0:
  +                x += sx
  +                err += dy
  +            y += sy
  +
  +    points.append((x, y))
       return points
  
  </patch>
golden_diff: 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():
  
verification_info: {"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"}]}