django翻译url和接受语言头

时间:2022-09-11 15:08:45

I've setup translated urls for some languages. The default language for django has been set to en-US.

我已经为一些语言设置了翻译url。django的默认语言被设置为en-US。

If you request a page /registration/ with nl-NL as Accept-Language header, I get a 404. I wasn't expecting such behaviour. Rather I was hoping I would be redirected to /registratie/ ,the tranlated url that corresponds my Accept-Language header. Ofcourse /registratie/ with the nl-Nl Accept-Language header, works fine and gives me the expected 200.

如果您请求一个页面/注册/使用nl-NL作为接收语言头,我将得到404。我没想到会有这种行为。相反,我希望我能被重定向到/registratie/,转换url对应我的接受语言头。当然/注册/使用nl-Nl接受语言头,工作良好,并给我预期的200。

I'm guessing this is just as normal as it can get ?

我猜这是正常的吗?

Isn't there a chance a user might get link from some website and the link is build for the English language, but the user in question has a different supported language setting, Accept-Language header ? In such a case he would be presented with a 404. He should be presented with or the English (default) content or or be redirected to the correct url for his language if supported.

难道用户不可能从某个网站获得链接,而该链接是为英语语言构建的吗?在这种情况下,他将被提交404。如果他的语言被支持,他应该被提供或英语(默认)内容或被重定向到正确的url。

Also what happens if a user has a not supported language setting ? Will the django default LANGUAGE_CODE be used, en-US in my case ?

如果用户拥有不支持的语言设置,会发生什么?是否将使用django默认语言_code,在我的例子中是en-US ?

Is there a way to work around this or handle it in a different better way ?

有没有一种方法可以解决这个问题,或者用另一种更好的方法来处理它?

an extract from my urls.py file, shows how I've setup translated urls:

从我的url中提取。py文件,显示我如何设置翻译url:

url(_(r'^step1/$'), AccountTypeSelectionView.as_view(), name="registration_step1"),

1 个解决方案

#1


2  

I'm making a wild assumption that _ is ugettext_lazy.

我大胆地假设_是ugettext_lazy。

Also what happens if a user has a not supported language setting ? Will the django default LANGUAGE_CODE be used, en-US in my case ?

如果用户拥有不支持的语言设置,会发生什么?是否将使用django默认语言_code,在我的例子中是en-US ?

Yes, ugettext will use the default value if no specific value is set. You can see this behaviour in do_translate in django.utils.translation.trans_real.

是的,如果没有设置特定的值,ugettext将使用默认值。

What happens with ugettext_lazy is that just prior to the URL being looked up, it is translated to the user's language and then the part of the URL is checked against all these. (Django uses a per-thread language setting that is changed to reflect the user's choices.)

在ugettext_lazy中发生的事情是,在查找URL之前,它被翻译成用户的语言,然后URL的一部分根据所有这些进行检查。(Django使用每个线程的语言设置,该设置会被更改以反映用户的选择。)

Isn't there a chance a user might get link from some website and the link is build for the English language, but the user in question has a different supported language setting, Accept-Language header ? In such a case he would be presented with a 404. He should be presented with or the English (default) content or or be redirected to the correct url for his language if supported.

难道用户不可能从某个网站获得链接,而该链接是为英语语言构建的吗?在这种情况下,他将被提交404。如果他的语言被支持,他应该被提供或英语(默认)内容或被重定向到正确的url。

Well, yes I believe that should be a supported feature but a quick look at the source made it appear that URL resolving would have to be changed a bit to accomodate this.

嗯,是的,我认为这应该是一个受支持的特性,但是快速查看一下源代码就会发现,要适应这个特性,URL解析就必须稍微做一些修改。

What you can do, is use a custom 404 page (as briefly mentioned here under Customizing error views):

你所能做的就是使用一个自定义的404页面(正如这里在自定义错误视图中简单提到的):

in your urls.py:

在你的urls . py:

handler404 = 'mysite.views.i18n_retry'

and in a view

在一个视图

from django.utils.translation import override
from django.core.urlresolvers import resolve
def i18n_retry(request):
    for language in settings.LANGUAGES:
        with override(language, deactivate=True):
           try:
              result = resolve(request.path)
              #here you will know what language the url is in
              #if it is a supported format
              #and can do something smart about it
           except Http404:
              #Http404 is thrown whenever resolving fails
              pass
     #here you should probably return a real 404

I've used the override context manager from Django 1.4. but there is a longer way of doing the same without.

我使用了Django 1.4中的重写上下文管理器。但是有一种更长的方法可以不这样做。

#1


2  

I'm making a wild assumption that _ is ugettext_lazy.

我大胆地假设_是ugettext_lazy。

Also what happens if a user has a not supported language setting ? Will the django default LANGUAGE_CODE be used, en-US in my case ?

如果用户拥有不支持的语言设置,会发生什么?是否将使用django默认语言_code,在我的例子中是en-US ?

Yes, ugettext will use the default value if no specific value is set. You can see this behaviour in do_translate in django.utils.translation.trans_real.

是的,如果没有设置特定的值,ugettext将使用默认值。

What happens with ugettext_lazy is that just prior to the URL being looked up, it is translated to the user's language and then the part of the URL is checked against all these. (Django uses a per-thread language setting that is changed to reflect the user's choices.)

在ugettext_lazy中发生的事情是,在查找URL之前,它被翻译成用户的语言,然后URL的一部分根据所有这些进行检查。(Django使用每个线程的语言设置,该设置会被更改以反映用户的选择。)

Isn't there a chance a user might get link from some website and the link is build for the English language, but the user in question has a different supported language setting, Accept-Language header ? In such a case he would be presented with a 404. He should be presented with or the English (default) content or or be redirected to the correct url for his language if supported.

难道用户不可能从某个网站获得链接,而该链接是为英语语言构建的吗?在这种情况下,他将被提交404。如果他的语言被支持,他应该被提供或英语(默认)内容或被重定向到正确的url。

Well, yes I believe that should be a supported feature but a quick look at the source made it appear that URL resolving would have to be changed a bit to accomodate this.

嗯,是的,我认为这应该是一个受支持的特性,但是快速查看一下源代码就会发现,要适应这个特性,URL解析就必须稍微做一些修改。

What you can do, is use a custom 404 page (as briefly mentioned here under Customizing error views):

你所能做的就是使用一个自定义的404页面(正如这里在自定义错误视图中简单提到的):

in your urls.py:

在你的urls . py:

handler404 = 'mysite.views.i18n_retry'

and in a view

在一个视图

from django.utils.translation import override
from django.core.urlresolvers import resolve
def i18n_retry(request):
    for language in settings.LANGUAGES:
        with override(language, deactivate=True):
           try:
              result = resolve(request.path)
              #here you will know what language the url is in
              #if it is a supported format
              #and can do something smart about it
           except Http404:
              #Http404 is thrown whenever resolving fails
              pass
     #here you should probably return a real 404

I've used the override context manager from Django 1.4. but there is a longer way of doing the same without.

我使用了Django 1.4中的重写上下文管理器。但是有一种更长的方法可以不这样做。