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 2.9KB

5 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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, of course).
  13. """
  14. response_redirect_class = HttpResponseRedirect
  15. def process_request(self, request):
  16. urlconf = getattr(request, 'urlconf', settings.ROOT_URLCONF)
  17. i18n_patterns_used, prefixed_default_language = is_language_prefix_patterns_used(urlconf)
  18. language = translation.get_language_from_request(request, check_path=i18n_patterns_used)
  19. language_from_path = translation.get_language_from_path(request.path_info)
  20. if not language_from_path and i18n_patterns_used and not prefixed_default_language:
  21. language = settings.LANGUAGE_CODE
  22. translation.activate(language)
  23. request.LANGUAGE_CODE = translation.get_language()
  24. def process_response(self, request, response):
  25. language = translation.get_language()
  26. language_from_path = translation.get_language_from_path(request.path_info)
  27. urlconf = getattr(request, 'urlconf', settings.ROOT_URLCONF)
  28. i18n_patterns_used, prefixed_default_language = is_language_prefix_patterns_used(urlconf)
  29. if (response.status_code == 404 and not language_from_path and
  30. i18n_patterns_used and prefixed_default_language):
  31. # Maybe the language code is missing in the URL? Try adding the
  32. # language prefix and redirecting to that URL.
  33. language_path = '/%s%s' % (language, request.path_info)
  34. path_valid = is_valid_path(language_path, urlconf)
  35. path_needs_slash = (
  36. not path_valid and (
  37. settings.APPEND_SLASH and not language_path.endswith('/') and
  38. is_valid_path('%s/' % language_path, urlconf)
  39. )
  40. )
  41. if path_valid or path_needs_slash:
  42. script_prefix = get_script_prefix()
  43. # Insert language after the script prefix and before the
  44. # rest of the URL
  45. language_url = request.get_full_path(force_append_slash=path_needs_slash).replace(
  46. script_prefix,
  47. '%s%s/' % (script_prefix, language),
  48. 1
  49. )
  50. return self.response_redirect_class(language_url)
  51. if not (i18n_patterns_used and language_from_path):
  52. patch_vary_headers(response, ('Accept-Language',))
  53. response.setdefault('Content-Language', language)
  54. return response