Skip to main content

Versión en español en README.es.md

Introduction

This application allows you use Django model translations easily. Everything is based on FieldTranslation, a class that stores the translation of each field of your application models.

All the process is transparent and the entries are created when save is called.

You will not have to modify FieldTranslation, but you can read the code.

All code in in the github repository: https://github.com/intelligenia/modeltranslation

Instructions

Installation

This application depends on django-cuser and TinyMCE, so you will need to install it and put it in the list of INSTALLED_APPS before modeltranslation.

You are encouraged to use pip to install django-cuser.

The easiest way to install modeltranslation is by installing it from pipy:

pip install modeltranslation

Once you’ve done this, you can install modeltranslation in settings.py:

INSTALLED_APPS = ( “tinymce”, “cuser”, “modeltranslation” )

Add IS_MONOLINGUAL to settings.py

You’ll have to include a new setting in settings.py IS_MONOLINGUAL=False. IS_MONOLINGUAL acts as a switch for modeltranslation:

# modeltranslation only works when IS_MONOLINGUAL is False
IS_MONOLINGUAL=False

Add TRANSLATABLE_MODEL_MODULES to settings.py

Add file setting TRANSLATABLE_MODEL_MODULES to settings.py. TRANSLATABLE_MODEL_MODULES contains a list of module paths that will be translated. For example:

TRANSLATABLE_MODEL_MODULES = ["app1.models", "app2.models", "fees.models", "menus.models", ...]

Import addtranslations

Import addtranslations if each of your models.py files:

from modeltranslation.translation import addtranslations

After that, you’ll have to call addtranslations at the end of this file:

addtranslations(__name__)

This call adds an observer that saves translations when save model method is executed.

Add translatable_fields to your models

Modify your models including a meta field “translatable_fields”. This field is a list with the fields you want to translate.

For example:

from django.db import models

class Event(models.Model):

    name = models.CharField(blank=False, max_length=150, verbose_name=u"Name", help_text=u"Name of the event.")
    description = models.TextField(blank=False, verbose_name=u"Description", help_text=u"Long description of the event.")
    short_description = models.CharField(blank=False, max_length=150, verbose_name=u"Short description", help_text=u"Short description of the event.")

    #...

    ## Event Meta
    class Meta:
        verbose_name = "event"
        verbose_name_plural = "events"
        translatable_fields = ("name", "description", "short_description")

And that’s all!

Now you have everything configurated and can use modeltranslation translations.

How to use translations in ModelForms

Make your ModelForm object inherits of TranslatableModelForm. This will include automatically the extra fields for each language you have in your website.

from modeltranslation.forms import TranslatableModelForm

class EventForm(TranslatableModelForm):
    pass

If you need to modify any of the fields in init method (for example by changing the widget of one field), you’ll have to call _add_translation_form_fields after your changes.

For example:

# EventForm __init__
def __init__(self, event, *args, **kwargs):
    super(EventForm, self).__init__(*args, **kwargs)

    # Adds TinyMCE editor but we also want this editor in other
    # languages' description fields
    self.fields["description"].widget = TinyMCE()

    # Manually adition of translation fields
    self._add_translation_form_fields()

Dynamic translations in Django templates

  1. Include modeltranslation filters in each template you wan to use this special filters.

{% load modeltranslation_tags %}
  1. Use filter “_” with the object and one of its fields. This filter will return the translation of that field in the current language.

For example:

{{ event|_:"name" }} {# Translates event name #}
{{ event.area|_:"name" }} {# Translates area name #}

Dynamic translations in code

This application injects a new method to each translatable model: get_trans_attr. This method returns the translation of the attribute in the current language (if it exists), otherwise returns the default value for this attribute:

For example:

# Original event name
original_event_name event.name

# Translated event name
translated_event_name event.get_trans_attr("name")

Contact and suggestions

Release files for modeltranslation 0.25

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for modeltranslation 0.25
File Size Uploaded
modeltranslation-0.25.tar.gz 5.1 MB Details

Release files / modeltranslation-0.25.tar.gz

Download URL modeltranslation-0.25.tar.gz
Size 5.1 MB
Tags Source
SHA-256 checksum
How to use checksums
6b13273ea51e36cccecf32ea87299f7ef19a741222b6d0cf3753870fa5f754bc
BLAKE2b-256 checksum
How to use checksums
30f4b00c8efa1e229d15d720ccf078f2aa035fd3bd5cbbf5dcd99ec71fef1a90
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

0.25 This release

1 release file

0.24

1 release file

0.23

1 release file

0.22

1 release file

0.21

1 release file

0.2

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page