def handler(request): """ Main handler view that calls the views to render a page or redirects to the appropriate language URL. """ url = request.path languages = language_list() language = None # Skip multiple slashes in the URL if '//' in url: url = re.sub('/+', '/', url) return HttpResponseRedirect('%s' % url) try: language = request.LANGUAGE_CODE except AttributeError: raise StandardError, \ 'Please add django.middleware.locale.LocaleMiddleware to your MIDDLEWARE_CLASSES.' # This shouldn't happen if not language in languages: language = settings.LANGUAGE_CODE if not language in languages: language = LANGUAGE_DEFAULT if not language in languages: raise Exception, \ _("Please define LANGUAGES in your project's settings.") # Determine the language and redirect the user, if required if LANGUAGE_REDIRECT: if url: for l in languages: if url.startswith('/%s/' % l): return handle_page(request, l, url[len(l) + 2:-1]) # Make sure the language code is prepended to the URL # See also: http://www.mail-archive.com/[email protected]/msg11604.html if not DISPLAY_ROOT and url == '/': # Avoid two redirects: handle_page will return a redirect to the correct page. return handle_page(request, language, '') return HttpResponseRedirect('/%s%s' % (language, url)) else: # TODO: Not implemented pass return handle_page(request, language, url[1:-1])
def handler(request): """ Main handler view that calls the views to render a page or redirects to the appropriate language URL. """ url = request.path urljunks = filter(lambda non_empty_junk: non_empty_junk, url.split('/')) languages = language_list() language = None # Skip multiple slashes in the URL if '//' in url: url = re.sub("/+", "/", url) return HttpResponseRedirect('%s' % url) try: request_language = request.LANGUAGE_CODE[:2] except AttributeError: raise StandardError, "Please add django.middleware.locale.LocaleMiddleware to your MIDDLEWARE_CLASSES." # This shouldn't happen if not request_language in languages: request_language = settings.LANGUAGE_CODE[:2] if not request_language in languages: request_language = LANGUAGE_DEFAULT[:2] if not request_language in languages: try: request_language = languages[0] except: raise Exception, _( "Please define LANGUAGES in your project's settings.") # Determine the language and redirect the user, if required if LANGUAGE_REDIRECT: if len(urljunks): url_language = urljunks[0] if url_language in languages: # Check request language against url language and change if necessary. # This allows django i18n language change over URL (and violates a lot of specs). # Requires a redirect to let django's locale middleware handle the statechange. if request_language != url_language: request_language = url_language if hasattr(request, 'session'): request.session['django_language'] = url_language else: response = HttpResponseRedirect(url) response.set_cookie(settings.LANGUAGE_COOKIE_NAME, url_language) return response translation.activate(request_language) return handle_page(request, request_language, '/'.join(urljunks[1:])) if url == '/': # Don't redirect root return handle_page(request, request_language, '') return HttpResponsePermanentRedirect('/%s%s' % (request_language, url)) return handle_page(request, request_language, url[1:-1])
def set_language(request): """ Redirect to a given url while setting the chosen language in the session or cookie. The url and the language code need to be specified in the request parameters. Since this view changes how the user will see the rest of the site, it must only be accessed as a POST request. If called as a GET request, it will redirect to the page in the request (the 'next' parameter) without changing any state. """ next = request.REQUEST.get('next', None) redirect = False if not next: next = request.META.get('HTTP_REFERER', None) if next: url_junks = next.split('/') if url_junks[0].startswith('http'): offset = 3 next = '/' else: offset = 0 next = '' if LANGUAGE_REDIRECT: if url_junks[offset] in language_list(): redirect = True del url_junks[offset] next += '/'.join(url_junks[offset:]) else: next = '/' response = HttpResponseRedirect('/') if request.method in ('POST', 'GET'): lang_code = request.REQUEST.get('language', None) if lang_code and check_for_language(lang_code): if LANGUAGE_REDIRECT and redirect: if lang_code in language_list(): next = u'/%s%s' % (lang_code, next) if hasattr(request, 'session'): request.session['django_language'] = lang_code else: response.set_cookie(settings.LANGUAGE_COOKIE_NAME, lang_code) response['Location'] = iri_to_uri(next) return response
def get_content( self, language=None, all=False, position='', ): if not language: language = translation.get_language() published_page_contents = \ self.pagecontent_set.filter(is_published=True, position=position) page_content = None # Determine the PageContent we want to render page_contents = \ published_page_contents.filter(language=language) if page_contents: if all: page_content = page_contents else: page_content = page_contents[0] else: # Use a PageContent in an alternative language for alt_language in language_list(): if alt_language == language: continue page_contents = \ published_page_contents.filter(language=alt_language) if page_contents: if all: page_content = page_contents else: page_content = page_contents[0] break if not page_content: # TODO: What's better? # return None page_content = PageContent(page=self) if all: page_content = [PageContent(page=self)] if all and page_content: for c in page_content: c.prepare() return page_content return page_content.prepare()
def handler(request): """ Main handler view that calls the views to render a page or redirects to the appropriate language URL. """ url = request.path urljunks = filter(lambda non_empty_junk: non_empty_junk, url.split('/')) languages = language_list() language = None # Skip multiple slashes in the URL if '//' in url: url = re.sub("/+" , "/", url) return HttpResponseRedirect('%s' % url) try: request_language = request.LANGUAGE_CODE[:2] except AttributeError: raise StandardError, "Please add django.middleware.locale.LocaleMiddleware to your MIDDLEWARE_CLASSES." # This shouldn't happen if not request_language in languages: request_language = settings.LANGUAGE_CODE[:2] if not request_language in languages: request_language = LANGUAGE_DEFAULT[:2] if not request_language in languages: try: request_language = languages[0] except: raise Exception, _("Please define LANGUAGES in your project's settings.") # Determine the language and redirect the user, if required if LANGUAGE_REDIRECT: if len(urljunks): url_language = urljunks[0] if url_language in languages: # Check request language against url language and change if necessary. # This allows django i18n language change over URL (and violates a lot of specs). # Requires a redirect to let django's locale middleware handle the statechange. if request_language != url_language: request_language = url_language if hasattr(request, 'session'): request.session['django_language'] = url_language else: response = HttpResponseRedirect(url) response.set_cookie(settings.LANGUAGE_COOKIE_NAME, url_language) return response translation.activate(request_language) return handle_page(request, request_language, '/'.join(urljunks[1:])) if url == '/': # Don't redirect root return handle_page(request, request_language, '') return HttpResponsePermanentRedirect('/%s%s' % (request_language, url)) return handle_page(request, request_language, url[1:-1])
def get_content(self, language=None, all=False, position=''): if not language: language = translation.get_language() published_page_contents = self.pagecontent_set.filter( is_published=True, position=position) page_content = None # Determine the PageContent we want to render page_contents = published_page_contents.filter(language=language) if page_contents: if all: page_content = page_contents else: page_content = page_contents[0] else: # Use a PageContent in an alternative language for alt_language in language_list(): if alt_language == language: continue page_contents = published_page_contents.filter( language=alt_language) if page_contents: if all: page_content = page_contents else: page_content = page_contents[0] break if not page_content: # TODO: What's better? # return None page_content = PageContent(page=self) if all: page_content = [PageContent(page=self)] if all and page_content: for c in page_content: c.prepare() return page_content return page_content.prepare()