On Thu, 2009-05-07 at 07:55 -0700, Wouter van der Graaf wrote: > Hi there, > > Spent hours trying to find the solution, but no luck. So here goes... > > Please consider simplified code below: > > {{{ > class Country(models.Model): > ... > label_en = models.CharField(max_length = 128, blank=True, > null=True) > label_nl = models.CharField(max_length = 128, blank=True, > null=True) > > class Meta: > ordering = ('label_en',) > > def __unicode__(self): > if hasattr(settings, 'LANGUAGE_CODE'): > field = 'label_%s' % settings.LANGUAGE_CODE.lower() > if hasattr(self, field): > return getattr(self, field) > return self.label_en > }}} > > Other models use Country with a ForeignKey relationship. The > __unicode__ function works for displaying localized country names. If > LANGUAGE_CODE is 'nl' then the drop down selectbox for Country in the > admin shows Dutch country names, otherwise all country names are > English. > > But it's not exactly what you'd want. Now I want to be able to do two > things: > > 1. Change __unicode__ to use request.LANGUAGE_CODE, the user > preference. > 2. Change ordering to the chosen language field. You don't want a > list in Dutch, ordered by English translations. > > All this is happening runtime, of course. Users in a admin edit page, > using a drop down for the foreign key Country, should see the country > names and ordering change when they select another language. > > Possible? Other ways to achieve a runtime language dependent model? > > Thanks in advance, > > Wouter >
IMHO, that is very definitely the wrong way to do i18n! I say this from a position of authority, as I have had to maintain systems like this in the past, and it is just Plain Wrong :) i18n is a very tricky topic, fortunately very smart people have spent a long long time to come up with the best solution - gettext. It is builtin to python, and is well supported by django. Read more about it here: http://docs.djangoproject.com/en/dev/topics/i18n/#topics-i18n Your scenario is slightly more complicated than the examples shown there, but the principle is the same. Instead of having separate fields for each language translation, your field holds the key phrase which is then used to lookup the correct translation in a gettext catalog. Your change to your model would look like this: class Country(models.Model): description = models.CharField(...) def __unicode__(self): return ugettext_lazy(self.description) I'm afraid this isn't a magic bullet though, for two reasons: 1) Your main aim seems to be to allow natural language ordering? I'm not entirely sure how one could achieve that, as the ordering is done as part of the DB query, which would not have access to the gettext catalog - any ideas anyone? 2) As you add more instances of your model, you would need to be able to add the translation of the description to the gettext catalog. This might be fine in this specific case (how many countries will be dynamically added later?), but wouldn't be for something slightly more dynamic, like tag names. Again, any ideas anyone? i18n, l10n and collation are pet peeves of mine, I am still yet to find excellent solutions to all of these problems :) Cheers Tom --~--~---------~--~----~------------~-------~--~----~ 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 django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---