Package: django-transmeta
IntroductionTransmeta is an application for translatable content in Django’s models. Each
language is stored and managed automatically in a different column at database
level.FeaturesAutomatic schema creation with translatable fields.Translatable fields integrated into Django’s admin interface.Command to synchronize database schema to add new translatable fields and new languages.Using transmetaCreating translatable modelsLook at this model:class Book(models.Model):
    title = models.CharField(max_length=200)
    description = models.TextField()
    body = models.TextField(default='')
    price = models.FloatField()Suppose you want to makedescriptionandbodytranslatable. The resulting model after usingtransmetais:from transmeta import TransMeta

class Book(models.Model):
    __metaclass__ = TransMeta

    title = models.CharField(max_length=200)
    description = models.TextField()
    body = models.TextField(default='')
    price = models.FloatField()

    class Meta:
        translate = ('description', 'body', )In python 3:from transmeta import TransMeta

class Book(models.Model, metaclass=transmeta.TransMeta):

    title = models.CharField(max_length=200)
    description = models.TextField()
    body = models.TextField(default='')
    price = models.FloatField()

    class Meta:
        translate = ('description', 'body', )Make sure you have set the default and available languages in yoursettings.py:LANGUAGE_CODE = 'es'

ugettext = lambda s: s # dummy ugettext function, as django's docs say

LANGUAGES = (
    ('es', ugettext('Spanish')),
    ('en', ugettext('English')),
)Notes:It’s possible that you want have a default language in your site, but this is not the default language to transmeta. You can set this variable in your settings:TRANSMETA_DEFAULT_LANGUAGE = 'it'The same it’s possible with the languages:TRANSMETA_LANGUAGES = (
    ('es', ugettext('Spanish')),
    ('en', ugettext('English')),
    ('en', ugettext('Italian')),
)This is the SQL generated with the./manage.py sqlallcommand:BEGIN;
CREATE TABLE "fooapp_book" (
    "id" serial NOT NULL PRIMARY KEY,
    "title" varchar(200) NOT NULL,
    "description_en" text,
    "description_es" text NOT NULL,
    "body_es" text NOT NULL,
    "body_en" text NOT NULL,
    "price" double precision NOT NULL
)
;
COMMIT;Notes:transmetacreates one column for each language. Don’t worry about needing new languages in the future,transmetasolves this problem for you.If one field isnull=Falseand doesn’t have a default value,transmetawill create only oneNOT NULLfield, for the default language. Fields for other secondary languages will be nullable. Also, the primary language will be required in the admin app, while the other fields will be optional (withblank=True). This was done so because the normal approach for content translation is first add content in the main language and later have translators translate into other languages.You can use./manage.py syncdbto create database schema.Playing in the python shelltransmetacreates one field for every available language for every translatable field defined in a model. Field names are suffixed with language short codes, e.g.:description_es,description_en, and so on. In addition it creates afield_namegetter to retrieve the field value in the active language.Let’s play a bit in a python shell to best understand how this works:>>> from fooapp.models import Book
>>> b = Book.objects.create(description_es=u'mi descripcion', description_en=u'my description')
>>> b.description
u'my description'
>>> from django.utils.translation import activate
>>> activate('es')
>>> b.description
u'mi descripcion'
>>> b.description_en
u'my description'Adding new languagesIf you need to add new languages to the existing ones you only need to change your settings.py and ask transmeta to sync the DB again. For example, to add French to our project, you need to add it to LANGUAGES insettings.py:LANGUAGES = (
    ('es', ugettext('Spanish')),
    ('en', ugettext('English')),
    ('fr', ugettext('French')),
)And execute a specialsync_transmeta_dbcommand:$ ./manage.py sync_transmeta_db

This languages can change in "description" field from "fooapp.book" model: fr

SQL to synchronize "fooapp.book" schema:
   ALTER TABLE "fooapp_book" ADD COLUMN "description_fr" text

Are you sure that you want to execute the previous SQL: (y/n) [n]: y
Executing SQL... Done

This languages can change in "body" field from "fooapp.book" model: fr

SQL to synchronize "fooapp.book" schema:
   ALTER TABLE "fooapp_book" ADD COLUMN "body_fr" text

Are you sure that you want to execute the previous SQL: (y/n) [n]: y
Executing SQL... DoneAnd done!Adding new translatable fieldsNow imagine that, after several months using this web app (with many books created), you need to make book price translatable (for example because book price depends on currency).To achieve this, first addpriceto the model’s translatable fields list:class Book(models.Model):
    ...
    price = models.FloatField()

    class Meta:
        translate = ('description', 'body', 'price', )All that’s left now is calling thesync_transmeta_dbcommand to update the DB schema:$ ./manage.py sync_transmeta_db

This languages can change in "price" field from "fooapp.book" model: es, en

SQL to synchronize "fooapp.book" schema:
    ALTER TABLE "fooapp_book" ADD COLUMN "price_es" double precision
    UPDATE "fooapp_book" SET "price_es" = "price"
    ALTER TABLE "fooapp_book" ALTER COLUMN "price_es" SET NOT NULL
    ALTER TABLE "fooapp_book" ADD COLUMN "price_en" double precision
    ALTER TABLE "fooapp_book" DROP COLUMN "price"

Are you sure that you want to execute the previous SQL: (y/n) [n]: y
Executing SQL...DoneWhat the hell this command does?sync_transmeta_dbcommand not only creates new database columns for new translatable field… it copy data from oldpricefield into one of languages, and that is why command ask you for destination language field for actual data. It’s very important that the LANGUAGE_CODE and LANGUAGES (or TRANSMETA_DEFAULT_LANGUAGE, TRANSMETA_LANGUAGES) settings have good values.This command also you can execute, when you want add a language to the site, or you want to change the default language intransmeta. For this last case, you can define a variable in the settings file:TRANSMETA_VALUE_DEFAULT = '---'Admin integrationtransmetatransparently displays all translatable fields into the admin interface. This is easy because models have in fact many fields (one for each language).Changing form fields in the admin is quite a common task, andtransmetaincludes thecanonical_fieldnameutility function to apply these changes for all language fields at once. It’s better explained with an example:from transmeta import canonical_fieldname

class BookAdmin(admin.ModelAdmin):
    def formfield_for_dbfield(self, db_field, **kwargs):
        field = super(BookAdmin, self).formfield_for_dbfield(db_field, **kwargs)
        db_fieldname = canonical_fieldname(db_field)
        if db_fieldname == 'description':
            # this applies to all description_* fields
            field.widget = MyCustomWidget()
        elif field.name == 'body_es':
            # this applies only to body_es field
            field.widget = MyCustomWidget()
        return fieldAuthorstransmeta was created at Yaco Systems, originally for Turismo Andaluz.Transmeta authors are:Marc Garcia <garcia.marc@gmail.com>Manuel Saelices <msaelices@yaco.es>Pablo Martin <goinnn@gmail.com>Change history0.7.3 (2013-09-02)Update the metainfo0.7.2 (2013-09-02)The project has moved to github0.7.1 (2013-09-02)Add manifest0.7.0 (2013-09-02)Python3 compatibleFix the readme0.6.11 (2013-08-20)Added get_mandatory_fieldname function.0.6.10 (2013-03-18)New TRANSMETA_MANDATORY_LANGUAGE setting, to control which field will be NOT NULL in the models.0.6.9 (2012-10-24)Support in method get_field_language for field names with underscores0.6.8 (2012-06-22)Fix a little bug in the command sync_transmeta_db (UnboundLocalError: local variable ‘f’ referenced before assignment)0.6.7 (2012-03-20)Change the representation (verbose_name) of the transmeta labels0.6.6 (2012-02-06)Improvements and usability in the command sync_transmeta_dbFix some bugsDocumentation0.6.5 (2012-01-13)Improvements and usability in the command sync_transmeta_dbWorks with the last django (the command sync_transmeta_db)Works with mysql (the command sync_transmeta_db)0.6.4 (2011-11-29)Fixes error with inheritance in models.0.6.3 (2011-11-29)Allow to use a TRANSMETA_LANGUAGES settings.Added two options to sync_transmeta_db: -y (assume yes on all) and -d (default language code)0.6.2 (2011-03-22)works when default locale have spelling variants as es-ES or en-US.0.6.1 (2011-03-17)get_all_translatable_fields does not returned the correct tuple. Problems with inheritance.0.6.0 (2011-02-24)Make compatible with Django 1.2 and 1.3 when using ugettext_lazy in models verbose_name, fixing a hidden bug also for Django 1.1Download