Django:使用 gettext 进行 URL 模式翻译
在一些 Django 应用程序中,我遇到了带有 gettext 的 URL 模式,例如:
from django.utils.translation import ugettext as _
urlpatterns = patterns('',
...
url(r'^%s$' % _('about/'), about, name='about'),
...
)
起初,以统一的方式与项目的其余部分一起使用国际化 URL 似乎是个好主意,但我对此表示怀疑。
AFAIK,URL 模式在应用程序启动时加载。因此,我怀疑它们将根据向应用程序发出第一个请求的用户的语言偏好来构建。当线程也参与其中时,这可能会变得更加不可预测。
对于安装采用单一语言但可能存在其他语言安装(例如论坛应用程序)的情况,此方法可能是合理的。
你认为这是一个问题还是只是我的想象?这种方法可以用于多语言网站吗? ugettext_lazy
可以避免这个问题吗?
In some Django applications I encountered URL patterns with gettext such as:
from django.utils.translation import ugettext as _
urlpatterns = patterns('',
...
url(r'^%s
At first, it seemed to be a good idea to have internationalized URLs in a uniform way with the rest of the project but I have doubts.
AFAIK, URL patterns are loaded at application start-up. So I suspect they will be build according to the language preferences of the user who makes the first request to the application. This may get even more unpredictable when threads are in the play too.
This approach may be reasonable for cases where an installation will be in single language but there may be other installations in other languages, like forum applications.
Do you think this is a problem or just my imagination? Can this approach be used for multilingual sites? Can ugettext_lazy
avoid this problem?
% _('about/'), about, name='about'),
...
)
At first, it seemed to be a good idea to have internationalized URLs in a uniform way with the rest of the project but I have doubts.
AFAIK, URL patterns are loaded at application start-up. So I suspect they will be build according to the language preferences of the user who makes the first request to the application. This may get even more unpredictable when threads are in the play too.
This approach may be reasonable for cases where an installation will be in single language but there may be other installations in other languages, like forum applications.
Do you think this is a problem or just my imagination? Can this approach be used for multilingual sites? Can ugettext_lazy
avoid this problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
阅读 django 文档:https://docs。 djangoproject.com/en/dev/topics/i18n/translation/#url-internationalization
基本上,您可以使用 ugettext_lazy 来翻译您的模式,并且只要为每个请求设置了语言,它就可以工作。为了确保这一点,您应该使用 LocaleMiddleware。 https://docs.djangoproject.com/en/ dev/ref/middleware/#django.middleware.locale.LocaleMiddleware
Have a read of the django docs: https://docs.djangoproject.com/en/dev/topics/i18n/translation/#url-internationalization
Basically, you can use ugettext_lazy to translate your patterns and it will work provided a language is set for every request. To ensure this, you should use LocaleMiddleware. https://docs.djangoproject.com/en/dev/ref/middleware/#django.middleware.locale.LocaleMiddleware
这种方法行不通。您的翻译在应用程序加载时进行。这意味着您的 URL 模式将采用单一语言,即应用程序的默认语言。
仅当调用翻译的上下文可以访问用户的语言首选项时,翻译才起作用。
为了使您的 URL 成为多语言,您必须使用一些运行时 URL 定义。这些将根据用户当前的语言加载。
This approach will not work. Your translation takes place when the application loads. This means your URL patterns will be of a single language, the default language of your app.
Translations only work when the context they are called from have access to a user's language preference.
For your URLs to be multilingual, you have to use some run-time url definitions. These would be loaded based on the user's current language.
关于 ugettext_lazy 在连接字符串时立即评估的说法是正确的。
像这样的工作: url(_(r'^contact/'), include('contact.urls')),
但是你必须翻译可能错误的模式。
You are right about ugettext_lazy is evaluated right away when concatenating strings.
Sth like that works: url(_(r'^contact/'), include('contact.urls')),
But you have to translate patterns what could be erroneus.
你可以这样做:
然后在你的调度程序中:
但它仍然容易出错......
保罗
You could do it like this:
And then in your dispatcher:
But it's still error-prone though....
Paul