Opened 16 years ago
Closed 16 years ago
#9600 closed (duplicate)
Translated Models
Reported by: | 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 |
Description
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
Attachments (1)
Change History (4)
by , 16 years ago
Attachment: | translation.py added |
---|
comment:1 by , 16 years ago
Resolution: | → duplicate |
---|---|
Status: | new → closed |
comment:2 by , 16 years ago
Resolution: | duplicate |
---|---|
Status: | closed → reopened |
comment:3 by , 16 years ago
Resolution: | → duplicate |
---|---|
Status: | reopened → closed |
Source code