简体   繁体   English

Django应用程序中模型的国际化

[英]Internationalization of models in Django applications

My application will be available in two languages: english and german. 我的申请将以两种语言提供:英语和德语。 The application will have a couple of XType objects with a description field. 该应用程序将有几个带有描述字段的XType对象。 How can I translate the description field of XType? 如何翻译XType的描述字段? Does Django provide support for this or I will have to use another Django application? Django是否为此提供支持,或者我将不得不使用另一个Django应用程序?

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. Django不提供模型字段翻译的直接支持。

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 ). 你必须找到一种方法来处理它在Django内或通过可插入的应用程序(如发布已经django-easymode或检查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: django-easymode包含@ i18n装饰器 ,它可以解决你的情况:

At times it becomes a requirement to translate models. 有时它成为翻译模型的必要条件。 Django supports internationalization of static text in templates and code by means of gettext. Django通过gettext支持模板和代码中静态文本的国际化。 For translation of models - dynamic data - easymode offers simple decorators to enable internationalized model fields and localized admin classes. 对于模型的转换 - 动态数据 - easymode提供简单的装饰器,以支持国际化模型字段和本地化管理类。

检查以下其中一个加载项: http//www.google.pl/search? sourceid = chrome&ie = UTF-8&q = Django + model + translation,以获得灵活的解决方案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM