Code

Opened 5 years ago

Closed 5 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: UI/UX:

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)

translation.py (1.6 KB) - added by Riccardo Di Virgilio <riccardodivirgilio@…> 5 years ago.
Source code

Download all attachments as: .zip

Change History (4)

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

Source code

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

  • Resolution set to duplicate
  • Status changed from new to closed

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

  • Resolution duplicate deleted
  • Status changed from closed to reopened

comment:3 Changed 5 years ago by riccardodivirgilio

  • Resolution set to duplicate
  • Status changed from reopened to closed

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.