Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

locale.py 3.4KB

1 year ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. from django.conf import settings
  2. from django.conf.urls.i18n import is_language_prefix_patterns_used
  3. from django.http import HttpResponseRedirect
  4. from django.urls import get_script_prefix, is_valid_path
  5. from django.utils import translation
  6. from django.utils.cache import patch_vary_headers
  7. from django.utils.deprecation import MiddlewareMixin
  8. class LocaleMiddleware(MiddlewareMixin):
  9. """
  10. Parse a request and decide what translation object to install in the
  11. current thread context. This allows pages to be dynamically translated to
  12. the language the user desires (if the language is available).
  13. """
  14. response_redirect_class = HttpResponseRedirect
  15. def process_request(self, request):
  16. urlconf = getattr(request, "urlconf", settings.ROOT_URLCONF)
  17. (
  18. i18n_patterns_used,
  19. prefixed_default_language,
  20. ) = is_language_prefix_patterns_used(urlconf)
  21. language = translation.get_language_from_request(
  22. request, check_path=i18n_patterns_used
  23. )
  24. language_from_path = translation.get_language_from_path(request.path_info)
  25. if (
  26. not language_from_path
  27. and i18n_patterns_used
  28. and not prefixed_default_language
  29. ):
  30. language = settings.LANGUAGE_CODE
  31. translation.activate(language)
  32. request.LANGUAGE_CODE = translation.get_language()
  33. def process_response(self, request, response):
  34. language = translation.get_language()
  35. language_from_path = translation.get_language_from_path(request.path_info)
  36. urlconf = getattr(request, "urlconf", settings.ROOT_URLCONF)
  37. (
  38. i18n_patterns_used,
  39. prefixed_default_language,
  40. ) = is_language_prefix_patterns_used(urlconf)
  41. if (
  42. response.status_code == 404
  43. and not language_from_path
  44. and i18n_patterns_used
  45. and prefixed_default_language
  46. ):
  47. # Maybe the language code is missing in the URL? Try adding the
  48. # language prefix and redirecting to that URL.
  49. language_path = "/%s%s" % (language, request.path_info)
  50. path_valid = is_valid_path(language_path, urlconf)
  51. path_needs_slash = not path_valid and (
  52. settings.APPEND_SLASH
  53. and not language_path.endswith("/")
  54. and is_valid_path("%s/" % language_path, urlconf)
  55. )
  56. if path_valid or path_needs_slash:
  57. script_prefix = get_script_prefix()
  58. # Insert language after the script prefix and before the
  59. # rest of the URL
  60. language_url = request.get_full_path(
  61. force_append_slash=path_needs_slash
  62. ).replace(script_prefix, "%s%s/" % (script_prefix, language), 1)
  63. # Redirect to the language-specific URL as detected by
  64. # get_language_from_request(). HTTP caches may cache this
  65. # redirect, so add the Vary header.
  66. redirect = self.response_redirect_class(language_url)
  67. patch_vary_headers(redirect, ("Accept-Language", "Cookie"))
  68. return redirect
  69. if not (i18n_patterns_used and language_from_path):
  70. patch_vary_headers(response, ("Accept-Language",))
  71. response.headers.setdefault("Content-Language", language)
  72. return response