def process_request(self, request): hostname = request.get_host().split(":")[0] locale, path = utils.strip_path(request.path_info) if localeurl_settings.USE_SESSION and not locale: slocale = request.session.get('django_language') if slocale and utils.supported_language(slocale): locale = slocale if localeurl_settings.USE_ACCEPT_LANGUAGE and not locale: accept_lang_header = request.META.get('HTTP_ACCEPT_LANGUAGE', '') header_langs = parse_accept_lang_header(accept_lang_header) accept_langs = filter( None, [utils.supported_language(lang[0]) for lang in header_langs]) if accept_langs: locale = accept_langs[0] if hasattr(request, 'urlconf') and request.urlconf is not None: urlconf = request.urlconf else: urlconf = get_urlconf() locale_path = utils.locale_path(path, locale, host=hostname, urlconf=urlconf) # locale case might be different in the two paths, that doesn't require # a redirect (besides locale they'll be identical anyway) if locale_path.lower() != request.path_info.lower(): locale_url = utils.add_script_prefix(locale_path) qs = request.META.get("QUERY_STRING", "") if qs: # Force this to remain a byte-string by encoding locale_path # first to avoid Unicode tainting - downstream will need to # handle the job of handling in-the-wild character encodings: locale_url = "%s?%s" % (locale_path.encode("utf-8"), qs) redirect_class = HttpResponsePermanentRedirect if not localeurl_settings.LOCALE_REDIRECT_PERMANENT: redirect_class = HttpResponseRedirect # @@@ iri_to_uri for Django 1.0; 1.1+ do it in HttpResp...Redirect return redirect_class(iri_to_uri(locale_url)) request.path_info = path if not locale: try: locale = request.LANGUAGE_CODE except AttributeError: locale = settings.LANGUAGE_CODE translation.activate(locale) request.LANGUAGE_CODE = translation.get_language()
def process_request(self, request): hostname = request.get_host().split(":")[0] locale, path = utils.strip_path(request.path_info) if localeurl_settings.USE_SESSION and not locale: slocale = request.session.get('django_language') if slocale and utils.supported_language(slocale): locale = slocale if localeurl_settings.USE_ACCEPT_LANGUAGE and not locale: accept_lang_header = request.META.get('HTTP_ACCEPT_LANGUAGE', '') header_langs = parse_accept_lang_header(accept_lang_header) accept_langs = filter( None, [utils.supported_language(lang[0]) for lang in header_langs] ) if accept_langs: locale = accept_langs[0] if hasattr(request, 'urlconf') and request.urlconf is not None: urlconf = request.urlconf else: urlconf = get_urlconf() locale_path = utils.locale_path(path, locale, host=hostname, urlconf=urlconf) # locale case might be different in the two paths, that doesn't require # a redirect (besides locale they'll be identical anyway) if locale_path.lower() != request.path_info.lower(): locale_url = utils.add_script_prefix(locale_path) qs = request.META.get("QUERY_STRING", "") if qs: # Force this to remain a byte-string by encoding locale_path # first to avoid Unicode tainting - downstream will need to # handle the job of handling in-the-wild character encodings: locale_url = "%s?%s" % (locale_path.encode("utf-8"), qs) redirect_class = HttpResponsePermanentRedirect if not localeurl_settings.LOCALE_REDIRECT_PERMANENT: redirect_class = HttpResponseRedirect # @@@ iri_to_uri for Django 1.0; 1.1+ do it in HttpResp...Redirect return redirect_class(iri_to_uri(locale_url)) request.path_info = path if not locale: try: locale = request.LANGUAGE_CODE except AttributeError: locale = settings.LANGUAGE_CODE translation.activate(locale) request.LANGUAGE_CODE = translation.get_language()
def process_request(self, request): locale, path = utils.strip_path(request.path_info) if localeurl_settings.USE_ACCEPT_LANGUAGE and not locale: accept_langs = filter(lambda x: x, [ utils.supported_language(lang[0]) for lang in parse_accept_lang_header( request.META.get('HTTP_ACCEPT_LANGUAGE', '')) ]) if accept_langs: locale = accept_langs[0] locale_path = utils.locale_path(path, locale) if locale_path != request.path_info: locale_url = utils.add_script_prefix(locale_path) qs = request.META.get("QUERY_STRING", "") if qs: # Force this to remain a byte-string by encoding locale_path # first to avoid Unicode tainting - downstream will need to # handle the job of handling in-the-wild character encodings: locale_url = "%s?%s" % (locale_path.encode("utf-8"), qs) redirect_class = HttpResponsePermanentRedirect if not localeurl_settings.LOCALE_REDIRECT_PERMANENT: redirect_class = HttpResponseRedirect # @@@ iri_to_uri for Django 1.0; 1.1+ do it in HttpResp...Redirect return redirect_class(iri_to_uri(locale_url)) request.path_info = path if not locale: try: locale = request.LANGUAGE_CODE except AttributeError: locale = settings.LANGUAGE_CODE translation.activate(locale) request.LANGUAGE_CODE = translation.get_language()
def reverse(*args, **kwargs): reverse_kwargs = kwargs.get('kwargs', {}) locale = utils.supported_language(reverse_kwargs.pop('locale', translation.get_language())) url = django_reverse(*args, **kwargs) _, path = utils.strip_script_prefix(url) return utils.locale_url(path, locale)
def reverse(viewname, urlconf=None, args=[], kwargs={}, prefix=None, current_app=None): kwargs = kwargs or {} locale = kwargs.pop('locale', translation.get_language()) path = django_reverse(viewname, urlconf, args, kwargs, prefix, current_app) if locale == '': return path return utils.locale_url(path, utils.supported_language(locale))
def reverse(*args, **kwargs): reverse_kwargs = kwargs.get('kwargs') or {} locale = utils.supported_language( reverse_kwargs.pop('locale', translation.get_language())) url = django_reverse(*args, **kwargs) _, path = utils.strip_script_prefix(url) return utils.locale_url(path, locale)
def process_request(self, request): locale, path = utils.strip_path(request.path_info) if localeurl_settings.USE_ACCEPT_LANGUAGE and not locale: accept_langs = filter(lambda x: x, [ utils.supported_language(lang[0]) for lang in parse_accept_lang_header( request.META.get('HTTP_ACCEPT_LANGUAGE', '')) ]) if accept_langs: locale = accept_langs[0] locale_path = utils.locale_path(path, locale) if locale_path != request.path_info: if request.META.get("QUERY_STRING", ""): locale_path = "%s?%s" % (locale_path, request.META['QUERY_STRING']) locale_url = utils.add_script_prefix(locale_path) redirect_class = HttpResponsePermanentRedirect if not localeurl_settings.LOCALE_REDIRECT_PERMANENT: redirect_class = HttpResponseRedirect # @@@ iri_to_uri for Django 1.0; 1.1+ do it in HttpResp...Redirect return redirect_class(iri_to_uri(locale_url)) request.path_info = path if not locale: try: locale = request.LANGUAGE_CODE except AttributeError: locale = settings.LANGUAGE_CODE translation.activate(locale) request.LANGUAGE_CODE = translation.get_language()
def process_request(self, request): locale, path = utils.strip_path(request.path_info) if localeurl_settings.USE_ACCEPT_LANGUAGE and not locale: accept_langs = filter(lambda x: x, [utils.supported_language(lang[0]) for lang in parse_accept_lang_header( request.META.get('HTTP_ACCEPT_LANGUAGE', ''))]) if accept_langs: locale = accept_langs[0] locale_path = utils.locale_path(path, locale) if locale_path != request.path_info: if request.META.get("QUERY_STRING", ""): locale_path = "%s?%s" % (locale_path, request.META['QUERY_STRING']) locale_url = utils.add_script_prefix(locale_path) # @@@ iri_to_uri for Django 1.0; 1.1+ do it in HttpResp...Redirect return HttpResponsePermanentRedirect(iri_to_uri(locale_url)) request.path_info = path if not locale: try: locale = request.LANGUAGE_CODE except AttributeError: locale = settings.LANGUAGE_CODE translation.activate(locale) request.LANGUAGE_CODE = translation.get_language()
def reverse(*args, **kwargs): no_locale = kwargs.pop('no_locale', False) locale = translation.get_language() path = django_reverse(*args, **kwargs) if not locale or no_locale: return path return utils.locale_url(path, utils.supported_language(locale))
def process_request(self, request): locale, path = utils.strip_path(request.path_info) if localeurl_settings.USE_ACCEPT_LANGUAGE and not locale: accept_langs = filter(lambda x: x, [utils.supported_language(lang[0]) for lang in parse_accept_lang_header( request.META.get('HTTP_ACCEPT_LANGUAGE', ''))]) if accept_langs: locale = accept_langs[0] locale_path = utils.locale_path(path, locale) if locale_path != request.path_info: locale_url = utils.add_script_prefix(locale_path) qs = request.META.get("QUERY_STRING", "") if qs: # Force this to remain a byte-string by encoding locale_path # first to avoid Unicode tainting - downstream will need to # handle the job of handling in-the-wild character encodings: locale_url = "%s?%s" % (locale_path.encode("utf-8"), qs) redirect_class = HttpResponsePermanentRedirect if not localeurl_settings.LOCALE_REDIRECT_PERMANENT: redirect_class = HttpResponseRedirect # @@@ iri_to_uri for Django 1.0; 1.1+ do it in HttpResp...Redirect return redirect_class(iri_to_uri(locale_url)) request.path_info = path if not locale: try: locale = request.LANGUAGE_CODE except AttributeError: locale = settings.LANGUAGE_CODE translation.activate(locale) request.LANGUAGE_CODE = translation.get_language()
def render(self, context): locale = resolve_variable(self.locale, context) if utils.supported_language(locale) is None: raise ValueError("locale not in settings.LANGUAGES: %s" % locale) path = self.urlnode.render(context) if self.urlnode.asvar: self.urlnode.render(context) context[self.urlnode.asvar] = chlocale(context[self.urlnode.asvar], locale) return '' else: return chlocale(path, locale)
def reverse(*args, **kwargs): reverse_kwargs = kwargs.get('kwargs') or {} prefix = kwargs.get('prefix') or None urlconf = kwargs.get('urlconf') or None if urlconf is None: urlconf = get_urlconf() locale = utils.supported_language(reverse_kwargs.pop( 'locale', translation.get_language())) url = django_reverse(*args, **kwargs) stipped_prefix, path = utils.strip_script_prefix(url, prefix = prefix) return utils.locale_url(path, locale, prefix = prefix, urlconf=urlconf)
def locale_path(path, locale=''): """ Generate the localeurl-enabled path from a path without locale prefix. If the locale is empty settings.LANGUAGE_CODE is used. """ locale = utils.supported_language(locale) if not locale: locale = utils.supported_language(settings.LANGUAGE_CODE) if utils.is_locale_independent(none_locale_path(path)): return none_locale_path(path) elif utils.is_default_locale(locale) and not localeurl_settings.PREFIX_DEFAULT_LOCALE: return none_locale_path(path) elif path.find(settings.GFLUX_URL_PREFIX)!=-1: return ''.join([u'/',settings.GFLUX_URL_PREFIX, locale, path]) elif path.find(settings.APP_URL_PREFIX)!=-1: return ''.join([u'/',settings.APP_URL_PREFIX, locale, path]) elif path.find(settings.GCUSTOMER_URL_PREFIX)!=-1: return ''.join([u'/',settings.GCUSTOMER_URL_PREFIX, locale, path]) elif path.find(settings.JCB_PAY_URL_PREFIX)!=-1: return path else : return none_locale_path(path)
def process_request(self, request): locale, path = utils.strip_path(request.path_info) if localeurl_settings.USE_ACCEPT_LANGUAGE and not locale: accept_langs = filter(lambda x: x, [utils.supported_language(lang[0]) for lang in parse_accept_lang_header( request.META.get('HTTP_ACCEPT_LANGUAGE', ''))]) if accept_langs: locale = accept_langs[0] locale_path = utils.locale_path(path, locale) if locale_path != request.path_info: if request.META.get("QUERY_STRING", ""): locale_path = "%s?%s" % (locale_path, request.META['QUERY_STRING']) return HttpResponsePermanentRedirect(locale_path) request.path_info = path if not locale: try: locale = request.LANGUAGE_CODE except AttributeError: locale = settings.LANGUAGE_CODE translation.activate(locale) request.LANGUAGE_CODE = translation.get_language()
def process_request(self, request): # we check if another middleware has already set the language, eg. from domain name if not hasattr(request, "LANGUAGE_CODE") or not request.LANGUAGE_CODE: locale, path = utils.strip_path(request.path_info) #return HttpResponse(str((request.META["SCRIPT_NAME"], request.path_info, locale))) if localeurl_settings.USE_ACCEPT_LANGUAGE and not locale: accept_langs = filter(lambda x: x, [utils.supported_language(lang[0]) for lang in parse_accept_lang_header( request.META.get('HTTP_ACCEPT_LANGUAGE', ''))]) if accept_langs: locale = accept_langs[0] locale_path = utils.locale_path(path, locale) if locale_path != request.path_info: full_path = ''.join([request.META["SCRIPT_NAME"], locale_path]) query = request.META.get("QUERY_STRING", "") if query: full_path = "%s?%s" % (full_path, query) return HttpResponsePermanentRedirect(full_path) request.path_info = path if not locale: try: locale = request.LANGUAGE_CODE except AttributeError: locale = settings.LANGUAGE_CODE translation.activate(locale) request.LANGUAGE_CODE = locale
def reverse(*args, **kwargs): reverse_kwargs = kwargs.get('kwargs', {}) locale = utils.supported_language(reverse_kwargs.pop('locale', translation.get_language())) path = django_reverse(*args, **kwargs) return utils.locale_url(path, locale)
def test_supported_language(self): self.assertEqual('fr', utils.supported_language('fr')) self.assertEqual('nl-be', utils.supported_language('nl-be')) self.assertEqual('en', utils.supported_language('en-gb')) self.assertEqual(None, utils.supported_language('de'))
# Copyright (c) 2008 Joost Cassee # Licensed under the terms of the MIT License (see LICENSE.txt) import re from django import http from django.conf import settings import django.core.exceptions from django.core import urlresolvers from django.http import HttpResponseRedirect from django.utils import translation import localeurl from localeurl import utils # Make sure the default language is in the list of supported languages assert utils.supported_language(settings.LANGUAGE_CODE) is not None, \ "Please ensure that settings.LANGUAGE_CODE is in settings.LANGUAGES." class LocaleURLMiddleware(object): """ Middleware that sets the language based on the request path prefix and strips that prefix from the path. It will also automatically redirect any path without a prefix, unless PREFIX_DEFAULT_LOCALE is set to True. Exceptions are paths beginning with MEDIA_URL (if settings.LOCALE_INDEPENDENT_MEDIA_URL is set) or matching any regular expression from LOCALE_INDEPENDENT_PATHS from the project settings. For example, the path '/en/admin/' will set request.LANGUAGE_CODE to 'en' and request.path to '/admin/'. Alternatively, the language is set by the first component of the domain name. For example, a request on 'fr.example.com' would set the language to
def reverse(*args, **kwargs): reverse_kwargs = kwargs.get("kwargs", {}) or {} locale = utils.supported_language(reverse_kwargs.pop("locale", translation.get_language())) url = django_reverse(*args, **kwargs) script_name, path_info = utils.strip_script_prefix(url) return utils.locale_url(path_info, locale, script_name)
from django.conf import settings import django.core.exceptions from django.http import HttpResponsePermanentRedirect, HttpResponseRedirect from django.utils import translation from django.utils.encoding import iri_to_uri from django.utils.translation.trans_real import parse_accept_lang_header from localeurl import settings as localeurl_settings # Importing models ensures that reverse() is patched soon enough. Refs #5. from localeurl import utils # Make sure the default language is in the list of supported languages assert utils.supported_language(settings.LANGUAGE_CODE) is not None, \ "Please ensure that settings.LANGUAGE_CODE is in settings.LANGUAGES." class LocaleURLMiddleware(object): """ Middleware that sets the language based on the request path prefix and strips that prefix from the path. It will also automatically redirect any path without a prefix, unless PREFIX_DEFAULT_LOCALE is set to True. Exceptions are paths beginning with MEDIA_URL and/or STATIC_URL (if settings.LOCALE_INDEPENDENT_MEDIA_URL and/or settings.LOCALE_INDEPENDENT_STATIC_URL are set) or matching any regular expression from LOCALE_INDEPENDENT_PATHS from the project settings. For example, the path '/en/admin/' will set request.LANGUAGE_CODE to 'en' and request.path to '/admin/'. Alternatively, the language is set by the first component of the domain name. For example, a request on 'fr.example.com' would set the language to French.
def resolve(*args, **kwargs): locale = utils.supported_language(translation.get_language()) path = args[0] if path.startswith("/%s/" % locale): path = path[len("/%s" % locale):] return django_resolve(path, *args[1:], **kwargs)
def reverse(viewname, urlconf=None, args=[], kwargs={}, prefix=None): locale = utils.supported_language(kwargs.pop('locale', translation.get_language())) path = django_reverse(viewname, urlconf, args, kwargs, prefix) return utils.locale_url(path, locale)