Django 在 url 中内化?如何制作这样的网址:“en/articles”和“pt/artigos”...?

发布于 2024-12-13 19:22:24 字数 266 浏览 0 评论 0原文

嘿!

我需要根据语言制作网址。

示例:

如果我的语言为英语,子类别名为“文章”,则 url 可能如下所示:

/en/articles/...

如果语言为葡萄牙语,且已翻译的子类别为“artigos”,则 url 将为:

/pt/artigos/...

我该怎么做? 我必须使用 urls.py 或一些猴子补丁?

谢谢

Hy!

I would need to make urls based on language.

Example:

If I had the language english and subcategory named "articles" then the url might be like this:

/en/articles/...

If the language were portuguese and subcategory already translated is "artigos" then the url will be:

/pt/artigos/...

How can I do it?
I must use the urls.py or some monkeypatches?

thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

撩起发的微风 2024-12-20 19:22:24

该功能在尚未发布的 1.4 版本中已经存在。您可以在发行说明中阅读有关它的内容。

如果您确实需要此功能,并且现有应用程序无法满足您的需求,您仍然可以尝试自己应用相应的补丁。

This features is already existing in the yet-to-be released version 1.4. You can read about it in the release note.

If your really need this feature, and no existing app feets your needs, you can still try to apply the corresponding patch yourself.

森林迷了鹿 2024-12-20 19:22:24

Django LocaleURL 正是一个中间件,可以完成此任务。该文档可以在源代码中找到,也可以在在线中找到。

编辑:我读到了您想要翻译 url 本身的事实...我不知道有任何代码可以提供此功能。也许您可以扩展 LocalURL 中间件来为您处理翻译。假设您有一个像 (?P\w+) 这样的正则表达式匹配,您可以在中间件中确定要使用哪个视图。也许类似于下面的映射?

if article_slug in ['articles', 'artigos', 'article']:
    article(request) # Call the article view

Django LocaleURL is a piece of middleware that does exactly this. The documentation can be found in the source, or online.

Edit: I read over the fact that you want to translate the url itself... I'm not aware of any piece of code that provides this. Perhaps you could extend the LocalURL middleware to take care of the translations for you. Say you have a regex match like (?P<articles>\w+), you could in the middleware determine which view you want to use. Something like the following mapping perhaps?

if article_slug in ['articles', 'artigos', 'article']:
    article(request) # Call the article view
对你再特殊 2024-12-20 19:22:24

我一直在使用 transurlvania 并取得了巨大成功,它完全满足了您的需要,甚至更多,但是我看到了在下一个 Django 版本中 django-i18nurls 将包含在 django 核心中所以也许最好了解一下

I've been using transurlvania with great success, it does exactly what you need and more, however i see that in the next Django release django-i18nurls will be included in django core so perhaps it would be better to learn that

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