Hi,

I am developing a website that contains mainly multilingual content.
There were some questions and ideas on this list on how to achieve
it, so I thought I would share mine and see if anyone else finds it
useful.  I have a working prototype, although not ready for production
use yet.

An example will explain how I use it.  Take a really minimal Category
model:

class Category(models.Model):
    # First, some fields that do not need translations
    creator = models.ForeignKey(User)
    created = models.DateTimeField(auto_now_add=True)

    # And now the translatable fields
    class Translation:
        """
        The definition of translation model.

        The multilingual machinery will automatically add these to the
        Category class:
         * get_name(language_id=None)
         * set_name(value, language_id=None)
         * get_description(language_id=None)
         * set_description(value, language_id=None)
         * name and description properties using the methods above
        """

        name = models.CharField(blank=True, null=False, maxlength=250)
        description = models.TextField(blank=True, null=False)

    def __str__(self):
        # note that you can use name and description fields as usual
        return self.name

    class Admin:
        # again, field names just work
        list_display = ('name', 'description')

After defining such a model you access translated fields like this:

c = Category.objects.all()[0]

# get the category name for the default language
c.name
# or
c.get_name()

# get the category name for language with id=2
c.get_name(2)

# change the name for language with id=2
c.set_name('nazwa', 2)

# change the name for the default language
c.name = 'nazwa'
# or
c.set_name('nazwa')

Internally the library creates another model, CategoryTranslation, that
maps Category and language identifiers to the translatable fields.

I don't want to paste all the code here, so I put the details, ideas,
the library itself and a sample application on my blog:
http://blog.elksoft.pl/index.php/2007/01/23/multilingual-content-in-django/

You can get the code here:
http://blog.elksoft.pl/wp-content/uploads/2007/01/multilingual_sample.zip

Let me know what you think about it.

-mk


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to