Internationalization of models in Django applications
My application will be available in two languages: englis开发者_如何学JAVAh 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 does not provide direct support of model field translations.
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-easymode includes the @i18n decorator, which may solve your case:
At times it becomes a requirement to translate models. Django supports internationalization of static text in templates and code by means of gettext. For translation of models - dynamic data - easymode offers simple decorators to enable internationalized model fields and localized admin classes.
精彩评论