ziima / django-multilingual-ds9

Yet another branch of django-multilingual with improved and legible core.
MIT License
10 stars 2 forks source link

Django Multilingual Deep Space 9

Django-multilingual-DS9 is a branch of django-multilingual, forked from django-multilingual-ng, with restructured core.

This project is not longer maintained. Recommended replacement is django-modeltranslation.

Requirements:

Django-multilingual-ds9 does not have to be fully compatible with django-multilingual-ng, but basic features should not differ.

Usage

Define models

from django.db import models

from multilingual import MultilingualModel

class MyModel(MultilingualModel):
    # Fields without language versions
    data_field = models.IntegerField()

    class Translation:
        # Fields with language versions
        name = models.CharField(max_length=100)

Add to administration

from django.contrib.admin import site
from multilingual import MultilingualModelAdmin

from myapp.models import MyModel

site.register(MyModel, MultilingualModelAdmin)

Use in views and templates

e = MyModel.objects.create()
# Get translation for current language
e.name
# Get translation with fallback
e.name_any
# Get translation instance
e.translation
e.translation_en

# Get all translation instances
e.translations.all()

# Filter objects by translation field
qs = MyModel.objects.filter(name='some')
# Returns objects with no translation and objects with missing name translation
qs = MyModel.objects.filter(name__isnull=True)
# The values_list may also contain several None objects if translations are missing
qs = MyModel.objects.values_list('name', flat=True)

# Get object with translation in one query
MyModel.objects.select_related('translation').get(pk=1)
# Get objects with missing translation
MyModel.objects.filter(translation__isnull=True)

# Change current language
from django.utils.translation import activate
activate('cs')

# Force usage of specific language in multilingual code
from multilingual import language
language.lock('cs')
language.release()

Features

Known bugs