Django 多语言 - 如何预览任何语言的内容?
我正在使用 django-multilingual 作为基于 Django 的网站。当我为模型定义 __unicode__ 函数以这种方式返回时:
def __unicode__(self):
return unicode(self.title)
但是我的默认语言是英语,并且我有一些仅以荷兰语插入的项目。当我预览完整列表时,我得到“无”作为标题。
有没有一种简单的方法可以尝试获取英文标题,然后检查任何其他语言的标题以进行预览?
I'm using django-multilingual for a Django based website. When I define the __unicode__
function for a model to return this way:
def __unicode__(self):
return unicode(self.title)
However my default language is English and I have some items that are inserted in Dutch only. When I preview the full list, I get "None" as a title.
Is there an easy way to try to get the English title and then check for a title in any other language just for preview?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
迭代所有翻译可以轻松完成,如下所示:
其中 obj 是模型对象,lang 将表示您设置中的元组('bg'、'保加利亚语')文件。
如果您有“uk-gb”等语言,则需要 lang[0].replace("-","_") ,因为这些值会放入 name_uk_gb强>
Iterating over all translations can be easily done like this:
Where obj is the model object, and lang will represent a tuple ('bg', 'Bulgarian') from your settings file.
lang[0].replace("-","_") is required, in case that you have languages like "uk-gb", because those values are placed into name_uk_gb
好吧,假设您的模型将荷兰语版本存储在名为
dutch_title
的属性中,您可以执行此操作。如果不知道“某些项目仅以荷兰语插入”是什么意思,则很难更有帮助地回答您的问题。
Well, assuming your model stores the Dutch version in an attribute called
dutch_title
, you could do this.Without knowing what you mean by "some items are inserted in Dutch only", it's a bit difficult to answer your question more helpfully.
根据您使用的应用程序(例如 - django-multilingual),您可以使用:
(示例来自:http://code.google.com/p/django-multilingual/source/browse/trunk/testproject/templates/articles/category_list .html)
我记得这个应用程序还有一些其他分支,它们曾经用来改变这种行为,如下所示:
如果您使用其他应用程序,您可以随时运行manage.py shell并测试使用 dir(MyModel) 或 dir(MyModel.fields) 来检查那里定义了哪些字段:)
如果这些字段名称有神奇的 getter 并且你在 dir(...) 中看不到任何有趣的东西,你可以参考您喜欢的本地化应用程序的代码,看看幕后发生了什么:)
Depending on the app you are using (for example - django-multilingual), you can use:
(example from: http://code.google.com/p/django-multilingual/source/browse/trunk/testproject/templates/articles/category_list.html)
As i remember there were some other forks of this app, that used to change that behavior like this:
In case you use other app, you can always run the manage.py shell and test with dir(MyModel), or dir(MyModel.fields) to check what fields are defined there :)
In case there are magic getters for those field names and you cant see anything interesting in dir(...), you can refer to the code of your prefered l18n app and see what going under the hood :)