Django:使用 gettext 进行 URL 模式翻译

发布于 2024-10-20 17:26:36 字数 507 浏览 3 评论 0原文

在一些 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

风月客 2024-10-27 17:26:36

阅读 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

皓月长歌 2024-10-27 17:26:36

这种方法行不通。您的翻译在应用程序加载时进行。这意味着您的 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.

远山浅 2024-10-27 17:26:36

关于 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.

流星番茄 2024-10-27 17:26:36

你可以这样做:

import six
from django.utils.functional import lazy


def lazy_url_pattern(pattern):
    return lazy(pattern, six.text_type)()

然后在你的调度程序中:

urlpatterns = [
    url(lazy_url_pattern(lambda: r'^{n}/

但它仍然容易出错......

保罗

.format(n=ugettext_lazy(u'foo'))), MyView.as_view(), name='...'),

但它仍然容易出错......

保罗

You could do it like this:

import six
from django.utils.functional import lazy


def lazy_url_pattern(pattern):
    return lazy(pattern, six.text_type)()

And then in your dispatcher:

urlpatterns = [
    url(lazy_url_pattern(lambda: r'^{n}/

But it's still error-prone though....

Paul

.format(n=ugettext_lazy(u'foo'))), MyView.as_view(), name='...'),

But it's still error-prone though....

Paul

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文