My favorites | Sign in
Project Home Downloads Wiki Issues Source
Project Information
Members

Easily translate your django model fields to supported language.

Translation is made just like gettext(). Pairs (original, translated) are stored in DB for each language. If no translation is found gettext() is used.

Example usage

  • First of all add translate to installed applications
  • Then run syncdb, this will create tables, and row for each language
  • Add class Translate to your model with fields containing a tuple with fields you would like to be translated
  • Translated fields you can access via model.localized_field_name property
  • To translate your models include translate.urls, then you can browse and translate objects. Also you can get translation url from model by calling get_translate_url(). NOTE: translation is performed for current language. Use localeurl for example to switch current language.
class Article(models.Model):
    name = models.CharField(max_length=20)
    text = models.TextField()

    class Translate: ## Mark translatable fields
        fields = ('name', 'text')
    
    def __unicode__(self):
        return self.localized_name

Then in view you can say:

 {{ article.localized_text }}

or:

 {% load translate_tags %}
 {{ article.localized_text|translate }}

If you want to use special model that keeps translations related to your one:

class Article(models.Model):
    name = models.CharField(max_length=20)
    text = models.TextField()

    class Translate: ## Mark translatable fields
        fields = ('name', 'text')
        use_own_model = True
    
    def __unicode__(self):
        return self.localized_name

Then run syncdb new model will be created, that stores references to object, language and translated values. NOTE: Don't forget to alter translation table when adding new items to Translate.fields

TODO

  • Translate views should be accesible via admin page (should be easy with django-1.1)
Powered by Google Project Hosting