Opened 11 years ago

Closed 11 years ago

#9600 closed (duplicate)

Translated Models

Reported by: riccardodivirgilio@… Owned by: nobody
Component: Uncategorized Version: 1.0
Severity: Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: yes
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


sorry for my english... i'm italian...

I have found several codes on internet that creates a table that is able to translate in the current language.
I have made a very short and powerful code to do this.

you can declare a CharField in the usal way you declare a CharField in Django, but it creates a column inside the database for every language that he founds inside the settings

it also create a method that returns the right field for the current languages

it orders the models inside the Django Admin if you set inside the modelAdmin list_display = 'name',

/* USAGE /*
from django.db import models
from django.utils.translation import get_language
from myapp.translation import TranslatedCharField, TranslatedTextField

class MultiLanguage(models.Model):

name = TranslatedCharField(max_length = 255, verbose_name = _('name'))
description = TranslatedTextField(null = True, blank = True, verbose_name = _('description'))

def unicode(self):


class Meta:

ordering = 'name_%s' % get_language(),

myobj= MultiLanguage(

name_it = 'Tavolo',
name_en = 'Table',
description_it = 'Usato per mangiare',
description_en = 'Used to eat'
#returns 'Tavolo', if the current lang is IT

#returns 'Usato per mangiare', if the current lang is IT

/* THE CODE: */

from django.utils.translation import get_language,
from django.conf import settings

available_languages = settings.LANGUAGES
TRANSLATION = lambda name, code: str('%s_%s' % (name, code))

class BaseTranslatedField:

def init(self, *args, kwargs):

self.base_args = args
self.base_kwargs = kwargs

def contribute_to_class(self, cls, name):

model = self.get_base_model()
current = TRANSLATION(name, get_language())

for code, description in available_languages.items():

cls.add_to_class(TRANSLATION(name, code), model(*self.base_args,self.base_kwargs))

get_current = lambda self:getattr(self, current)
get_current.admin_order_field = current
cls.add_to_class(name, get_current)

#del self.base_args
#del self.base_kwargs

class TranslatedCharField(BaseTranslatedField, models.CharField):

def get_base_model(self):

return models.CharField

class TranslatedTextField(BaseTranslatedField, models.TextField):

def get_base_model(self):

return models.TextField

this isvery simple and powerful. hope that we can include this code in the next version

the code is stable, i've tested it for mounths in 20 different models inside my django application

Attachments (1) (1.6 KB) - added by Riccardo Di Virgilio <riccardodivirgilio@…> 11 years ago.
Source code

Download all attachments as: .zip

Change History (4)

Changed 11 years ago by Riccardo Di Virgilio <riccardodivirgilio@…>

Attachment: added

Source code

comment:1 Changed 11 years ago by Riccardo Di Virgilio <riccardodivirgilio@…>

Resolution: duplicate
Status: newclosed

comment:2 Changed 11 years ago by Riccardo Di Virgilio <riccardodivirgilio@…>

Resolution: duplicate
Status: closedreopened

comment:3 Changed 11 years ago by Riccardo Di Virgilio

Resolution: duplicate
Status: reopenedclosed
Note: See TracTickets for help on using tickets.
Back to Top