Django应用程序中的模型国际化 [英] Internationalization of models in Django applications
问题描述
我的申请将提供两种语言:英语和德语。该应用程序将具有几个具有描述字段的XType对象。如何翻译XType的描述字段? Django是否为此提供支持,否则我将不得不使用另一个Django应用程序?
My application will be available in two languages: english and german. The application will have a couple of XType objects with a description field. How can I translate the description field of XType? Does Django provide support for this or I will have to use another Django application?
class XType(models.Model):
description = models.CharField(max_length=50)
def __unicode__(self):
return self.description
class X(models.Model):
type = models.ForeignKey(XType)
推荐答案
Django不提供模型字段转换的直接支持。
Django does not provide direct support of model field translations.
您必须找到一种方法来处理Django或通过可插入的应用程序(如已发布的django-easymode或检查< a href =http://blog.muhuk.com/2010/01/06/dynamic-translation-apps-for-django.html =nofollow> http://blog.muhuk.com/2010/ 01/06 / dynamic-translation-apps-for-django.html )。
You have to find a way to deal with it either within Django or via plugable applications (like posted already django-easymode or check http://blog.muhuk.com/2010/01/06/dynamic-translation-apps-for-django.html).
如果你想在你的应用程序中处理它,你可能想尝试像语言一样保存一个实例,然后在检索数据时进行相应的过滤:
If you want to deal within your app with it you might want to try something like saving one instance per language and then filter accordingly when retrieving data:
class XType(models.Model):
language = models.CharField(max_length=5)
description = models.CharField(max_length=50)
当然取决于您的项目需求。
Depends of course a lot on your project needs.
这篇关于Django应用程序中的模型国际化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!