﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
9600	Translated Models	riccardodivirgilio@…	nobody	"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):
        return self.name()
        
    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'
    )

myobj.name()
#returns 'Tavolo', if the current lang is IT

myobj.description()
#returns 'Usato per mangiare', if the current lang is IT

/* THE CODE: translation.py */

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


"		closed	Uncategorized	1.0		duplicate			Unreviewed	0	1	0	0	0	0
