Django 在 url 中内化?如何制作这样的网址:“en/articles”和“pt/artigos”...?
嘿!
我需要根据语言制作网址。
示例:
如果我的语言为英语,子类别名为“文章”,则 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
该功能在尚未发布的 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.
Django LocaleURL 正是一个中间件,可以完成此任务。该文档可以在源代码中找到,也可以在在线中找到。
编辑:我读到了您想要翻译 url 本身的事实...我不知道有任何代码可以提供此功能。也许您可以扩展 LocalURL 中间件来为您处理翻译。假设您有一个像
(?P\w+)
这样的正则表达式匹配,您可以在中间件中确定要使用哪个视图。也许类似于下面的映射?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?我一直在使用 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