コード例 #1
0
    def process_response(self, request, response):
        """
        Override the original method to redirect permanently and facilitate 
        the :func.`translations.urls.translation_patterns` URL redirection
        logic.
        """
        language = translation.get_language()
        default = get_default_language()
        
        #redirect to the original default language URL
        #if language prefix is used
        if (response.status_code == 404 and
            self.is_language_prefix_patterns_used() and default == language and 
            request.path_info.startswith('/%s/' % default)):
            
            urlconf = getattr(request, 'urlconf', None)
            language_path = re.sub(r'^/%s/' % default, '/', request.path_info)
            if settings.APPEND_SLASH and not language_path.endswith('/'):
                language_path = language_path + '/'

            if is_valid_path(language_path, urlconf):
                #we use a permanent redirect here.
                #when changing the default language we need to let the world know
                #that our links have permanently changed and transfer our seo juice 
                #to the new url
                #http://blog.yawd.eu/2012/impact-django-page-redirects-seo/
                return  HttpResponsePermanentRedirect("%s://%s/%s" % (
                    request.is_secure() and 'https' or 'http',
                    request.get_host(), re.sub(r'^/%s/' % default, '', request.get_full_path())))
        
        patch_vary_headers(response, ('Accept-Language',))
        if 'Content-Language' not in response:
            response['Content-Language'] = language
        return response
コード例 #2
0
    def process_request(self, request):
        """
        Enable the default language if a supported db language can not
        be resolved.
        """
        #replace the original language detection method
        language = get_language_from_request(
            request, check_path=self.is_language_prefix_patterns_used())
        
        if language not in get_supported_languages():
            language = get_default_language()

        translation.activate(language)        
        request.LANGUAGE_CODE = translation.get_language()
コード例 #3
0
    def process_request(self, request):
        """
        Enable the default language if a supported db language can not
        be resolved.
        """
        #replace the original language detection method
        language = get_language_from_request(
            request, check_path=self.is_language_prefix_patterns_used())

        if language not in get_supported_languages():
            language = get_default_language()

        translation.activate(language)
        request.LANGUAGE_CODE = translation.get_language()
コード例 #4
0
    def get_name(self, language_id=None):
        """
        Get the related :class:`translations.models.Translation`
        object's display name for a given ``language``.
        """
        #use the current language if not explicitly set
        translation = self.translation(language_id)
        if translation:
            return unicode(translation)

        #attempt to show default translation
        translation = self.translation(utils.get_default_language())  
        if translation:
            return u'%s (%s %s)' % (translation, _('not translated in'), language_id if language_id else get_language())
        else:
            return u'%s #%s (%s %s)' % (self._meta.verbose_name, self.pk, _('not translated in'), language_id if language_id else get_language())
コード例 #5
0
def languages(request):
    """
    This context processor adds three context variables::
    
        `langs`:    A list of the available project languages. This list holds :class:`translations.models.Language` instances.
        `default`:    The default language. This holds the **language** code and not the :class:`translations.models.Language` instance.
        `clean_url`:    The current url with the preceding language code (if there is one) removed. E.g. for the url `'/en/whatever/'` the ``clean_url`` will be `'/whatever/'`. Useful if the project URLs have common slugs etc. and we want to avoid reversing views in our templates in order to find the equivalent url of another language.  
    """
    langs = Language.objects.all()
    default = get_default_language()

    #assumes that no name collisions exist
    clean_url = re.sub('/%s/' % default, '/',
                       request.path) if get_language() == default else re.sub(
                           r'^/(%s)/' % lang_pattern, '/', request.path)

    return {'langs': langs, 'default_lang': default, 'clean_url': clean_url}
コード例 #6
0
def languages(request):
    """
    This context processor adds three context variables::
    
        `langs`:    A list of the available project languages. This list holds :class:`translations.models.Language` instances.
        `default`:    The default language. This holds the **language** code and not the :class:`translations.models.Language` instance.
        `clean_url`:    The current url with the preceding language code (if there is one) removed. E.g. for the url `'/en/whatever/'` the ``clean_url`` will be `'/whatever/'`. Useful if the project URLs have common slugs etc. and we want to avoid reversing views in our templates in order to find the equivalent url of another language.  
    """
    langs = Language.objects.all()
    default = get_default_language()
    
    #assumes that no name collisions exist
    clean_url = re.sub('/%s/' % default, '/', request.path) if get_language() == default else re.sub(r'^/(%s)/' % lang_pattern, '/', request.path)

    return {
        'langs' : langs,
        'default_lang': default,
        'clean_url' : clean_url
    }
コード例 #7
0
    def process_response(self, request, response):
        """
        Override the original method to redirect permanently and facilitate 
        the :func.`translations.urls.translation_patterns` URL redirection
        logic.
        """
        language = translation.get_language()
        default = get_default_language()

        #redirect to the original default language URL
        #if language prefix is used
        if (response.status_code == 404
                and self.is_language_prefix_patterns_used()
                and default == language
                and request.path_info.startswith('/%s/' % default)):

            urlconf = getattr(request, 'urlconf', None)
            language_path = re.sub(r'^/%s/' % default, '/', request.path_info)
            if settings.APPEND_SLASH and not language_path.endswith('/'):
                language_path = language_path + '/'

            if is_valid_path(language_path, urlconf):
                #we use a permanent redirect here.
                #when changing the default language we need to let the world know
                #that our links have permanently changed and transfer our seo juice
                #to the new url
                #http://blog.yawd.eu/2012/impact-django-page-redirects-seo/
                return HttpResponsePermanentRedirect(
                    "%s://%s/%s" %
                    (request.is_secure() and 'https'
                     or 'http', request.get_host(),
                     re.sub(r'^/%s/' % default, '', request.get_full_path())))

        patch_vary_headers(response, ('Accept-Language', ))
        if 'Content-Language' not in response:
            response['Content-Language'] = language
        return response
コード例 #8
0
 def regex(self):
     if get_language() == get_default_language():
         return re.compile(r'')
     return super(TranslationRegexURLResolver, self).regex
コード例 #9
0
 def regex(self):
     if get_language() == get_default_language():
         return re.compile(r'')
     return super(TranslationRegexURLResolver, self).regex