Opened 16 years ago

Closed 16 years ago

Last modified 16 years ago

#6952 closed (invalid)

Internationalization of strings in a database

Reported by: klaus.weidinger@… Owned by: nobody
Component: Internationalization Version: dev
Severity: Keywords: Internationalization, Translation, Database, Models, Flatpages
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Hi Django-Team,

While building up my website I worried aubout providing it in more than one language someday. At this point I tried to figure out how Djangos I18N-Tool works from the Documentation and if it would cover all my needs. But surprisingly it wouldn't. My problem was to translate strings in database, which can only be reached via an implemented search engine. But doing this was not explained. On request in the German Python forum I was told that this would be impossible (or very hard work) with Django itself. But I think this should get a feature of Django - not because it is possible in any other framework (I haven't yet tried any others) - but because it would improve Django considerably. It would be a new challenge on your way to version 1.0, but it would be worth to face. Therefore I hope, this ticket will get into further discussion and perhaps be kept in mind while developing and improving Django to reach its peak.

One way to get around this problem would be, to give the programmer (the perfectionist) a possibility to mark translatable database columns in the models. The function - or whatever collects the translatable strings - would then only have to add all strings in this database column to the language file. I'm not an expert in Django, but I think this should work, because the real translation or substitution is done after running all the other Python code and therefore wouldn't differ from a translation of a hardcoded (I know you hate this word) string.

It would also be useful, but not this essential, to bring up an easy way for translating flatpages. One way to reach this would be to add another data field to the flatpages, which stores the language code. If requested, Django could then first look up, if the flatpage exists in the preferenced language. If this failed, it could look for any other languages to be supported and then perhaps return another variable (e.g. flatpage.tranlation) which tells, that the translation failed (in the requested language).

If you could manage to implement this features it would be another good reason to use Django (there can't be enough) and would match perfect with Djangos philosophies.

Yours sincerely, Klaus Weidinger

Change History (4)

comment:1 by mrts, 16 years ago

Resolution: invalid
Status: newclosed

I'm closing this as invalid as this is the (horribly :) ) wrong way of doing things.

What you can do:

  • just tag translatable models with a language field and filter objects by their language:
    class Foo(models.Model):
      language = models.CharField(choices=langugage_choices_here)
      bar = models.TextField()
    ...
    foos_in_english = Foo.objects.filter(language='en')
    
  • if that doesn't suit you for whatever reason, use language-specific models once model inheritance hits the trunk:
    class AbstractFoo(models.Model):
      bar = models.TextField()
      class Meta:
        abstract = True
    
    class EnglishFoo(models.Model):
      pass
    
    class SpanishFoo(models.Model):
      pass
    
  • moreover, the functionality you want can already be achieved with very little hackery (but I strongly recommend AGAINST using this approach, it is just plain wrong, I give it only to prove that it's easy with Django), the general approach is as follows:
    # you would create a proper regex that filters out only those fields you need here
    ./manage.py dumpdata foo | sed 's/: \("[^"]\+"\)/: _(\1)/g' > translateme.py
    /foo/django/bin/make-messages.py -l de
    # translate the .po-file, I translated 'hello' to 'halloo'
    /foo/django/bin/compile-messages.py
    ./manage.py shell --plain
    >>> from django.utils import translation
    >>> from bar.models import Bar
    >>> b = Bar.objects.get()
    >>> b.name
    u'hello'
    >>> translation.activate('de')
    >>> translation.ugettext(b.name)
    u'halloo'
    

comment:2 by mrts, 16 years ago

Oh well... corrected examples follow.

class AbstractFoo(models.Model):
  bar = models.TextField()
  class Meta:
    abstract = True

class EnglishFoo(models.Model):
  pass

class SpanishFoo(models.Model):
  pass
cat foo/models.py:
from django.db import models
class Foo(models.Model):
  name = models.CharField(max_length=100)
# create some Foo objects and save them

./manage.py dumpdata foo | sed 's/: \("[^"]\+"\)/: _(\1)/g' > translateme.py
/path_to/django/bin/make-messages.py -l de
# translate the .po-file, e.g. translate 'hello' to 'halloo'
/path_to/django/bin/compile-messages.py

./manage.py shell --plain
>>> from django.utils import translation
>>> from foo.models import Foo
>>> f = Foo.objects.get()
>>> f.name
u'hello'
>>> translation.activate('de')
>>> translation.ugettext(f.name)
u'halloo'

comment:3 by mrts, 16 years ago

Doh, inheritance still wrong...

class AbstractFoo(models.Model):
  bar = models.TextField()
  class Meta:
    abstract = True

class EnglishFoo(AbstractFoo):
  pass

class SpanishFoo(AbstractFoo):
  pass

comment:4 by anonymous, 16 years ago

Thanks for your reply. I will try this, when I've gotten more familiar with the I18N-Mechanism. But I was merely surprised to get told, that this wouldn't work with Django and therefore wrote this ticket.

Note: See TracTickets for help on using tickets.
Back to Top