2015-02-23 74 views
1

我已正確安裝國際化。在Django切換語言與翻譯的URL重定向

它的作品中包含的網址:

/en/bookings/ #English 
/es/reservas/ #Spanish 

在主頁的語言切換工作也沒關係。

- 什麼問題?

當我在翻譯的頁面中更改語言(如/ en/bookings /)時,如果將語言翻譯成西班牙語,我將重定向到/ zh/bookings/。

如果我更改前綴(like this answer),重定向轉到/ es/bookings /不存在。

我不想被重定向到主頁。

- 我喜歡什麼?

如果我在/ en/bookings /並切換到西班牙語,我希望被重定向到/ es/reservas /,以用於所有翻譯的URL。

什麼是最好的方法?

謝謝。

回答

1

當我有同樣的問題,我實現了一個自定義模板標籤(current_url),鑑於上下文的請求,重新呈現爲活動語言的網址:

{% load custom_tags %} 
<ul> 
{% get_language_info_list for LANGUAGES as languages %} 
{% for language in languages %} 
    {# IMPORTANT! enclose the 'current_url' tag in a 'language' block #} 
    {% language language.code %} 
     <li {% if language.code == LANGUAGE_CODE %}class="active"{% endif %}> 
      <a href="{% current_url %}">{{ language.name_local }}</a> 
     </li> 
    {% endlanguage %} 
{% endfor %} 
</ul> 

下面是代碼自定義標籤(custom_tags.py):

import six 
import sys 
from django.template import Node, TemplateSyntaxError, Library 
from django.conf import settings 

register = Library() 

class CurrentURLNode(Node): 

    def __init__(self, asvar=None): 
     self.asvar = asvar 

    def render(self, context): 
     request = context['request'] 
     from django.core.urlresolvers import reverse, NoReverseMatch 
     url = '' 
     try: 
      url = reverse(request.resolver_match.view_name, args=request.resolver_match.args, kwargs=request.resolver_match.kwargs, current_app=context.current_app) 
     except NoReverseMatch: 
      exc_info = sys.exc_info() 
      if settings.SETTINGS_MODULE: 
       project_name = settings.SETTINGS_MODULE.split('.')[0] 
       try: 
        url = reverse(project_name + '.' + request.resolver_match.view_name, 
          args=request.resolver_match.args, kwargs=request.resolver_match.kwargs, 
          current_app=context.current_app) 
       except NoReverseMatch: 
        if self.asvar is None:      
         six.reraise(*exc_info) 
      else: 
       if self.asvar is None: 
        raise 

     if self.asvar: 
      context[self.asvar] = url 
      return '' 
     else: 
      return url 

@register.tag 
def current_url(parser, token): 
    bits = token.split_contents() 
    bits = bits[1:] 
    asvar = None 
    if len(bits) >= 2 and bits[-2] == 'as': 
     asvar = bits[-1] 
     bits = bits[:-2] 
    if len(bits): 
     raise TemplateSyntaxError("Unexpected arguments to current_url tag") 
    return CurrentURLNode(asvar) 

沒有必要使用'set_language'django視圖。無需發出POST請求來更改活動語言。只有html archiant將所有國際化內容鏈接在一起,對SEO來說更好。

1

我有類似的問題,所以我發送我的決議,以節省您的時間。

主(urls.py)

from django.conf.urls import include, url 
from django.conf.urls.i18n import i18n_patterns 

urlpatterns = [ 
    url(r'^i18n/', include('django.conf.urls.i18n')), 
] 

urlpatterns += i18n_patterns(
    url(r'^', include('index.urls', namespace='index')), 
) 

(index.urls.py)

from django.conf.urls import url 
from django.views.generic import TemplateView 
from django.utils.translation import ugettext_lazy as _ 


urlpatterns = [ 
    url(r'^$', TemplateView.as_view(template_name='index/index.html'), name='index'), 
    url(_(r'^python-programming/$'), TemplateView.as_view(template_name='index/new_page.html'), 
     name='new_page'), 
] 

創建模板標籤中,我們支持所有語言返回當前位置的URL(index.templatetags .helper_tags.py)

from django.template import Library 
from django.core.urlresolvers import resolve, reverse 
from django.utils.translation import activate, get_language 

register = Library() 


@register.simple_tag(takes_context=True) 
def change_lang(context, lang=None, *args, **kwargs): 
    """ 
    Get active page's url by a specified language 
    Usage: {% change_lang 'en' %} 
    """ 

    path = context['request'].path 
    url_parts = resolve(path) 

    url = path 
    cur_language = get_language() 
    try: 
     activate(lang) 
     url = reverse(url_parts.view_name, kwargs=url_parts.kwargs) 
    finally: 
     activate(cur_language) 

    return "%s" % url 

創建中間件來更改站點語言,當用戶點擊alt ernative連結此子網站,但在不同的語言 (middleware.py)

from django.utils import translation 
from django.conf import settings 
from django.utils.deprecation import MiddlewareMixin 


class LangBasedOnUrlMiddleware(MiddlewareMixin): 

    @staticmethod 
    def process_request(request): 

     if hasattr(request, 'session'): 
      active_session_lang = request.session.get(translation.LANGUAGE_SESSION_KEY) 

      if active_session_lang == request.LANGUAGE_CODE: 
       return 

      if any(request.LANGUAGE_CODE in language for language in settings.LANGUAGES): 
       translation.activate(request.LANGUAGE_CODE) 
       request.session[translation.LANGUAGE_SESSION_KEY] = request.LANGUAGE_CODE 

它添加到(設置。PY)剛過模板LocaleMiddleware

MIDDLEWARE = [ 
    'django.middleware.security.SecurityMiddleware', 
    'django.contrib.sessions.middleware.SessionMiddleware', 
    'django.middleware.locale.LocaleMiddleware', 
    'our_app.middleware.LangBasedOnUrlMiddleware', 
] 

使用範例:

{% load i18n %} 
{% load helper_tags %} 
    {% get_available_languages as languages %} 
    {% for lang_code, lang_name in languages %} 
     <a href="{{ request.scheme }}://{{ request.META.HTTP_HOST }}{% change_lang lang_code %}"> 
    {% endfor %}