Search code examples
pythondjangointernationalizationdjango-1.8django-i18n

Django i18n: Translating database values where one word is a name that might change


I wanna translate the notifications users get in my app (Django 1.8). The notifications are stored in the database and all have standard texts, but they include the name of the person that the notification relates to, for example, one might be:

"John has sent you a message"

so the "has sent you a message" part will be the same every time, but the name in the beginning might change to anything, and doesn't need to be translated.

For other database variables and some notifications that have a few standard values I'm just doing {% trans notification.message %} and manually adding the msgid and msgstr to the django.po file, but since this one has the name in the beginning I don't know what to do. Suggestions?


Solution

  • If the texts are 'standard' texts as you put it, you probably shouldn't store them redundantly in the database with just the names change (I don't believe what you are trying to do is possible). You are probably better off with a property or function of the model:

    @property
    def message(self):
        return ugettext('{name} has sent you a message').format(name=self.foo.bar.name)
    

    This way, makemessages will work properly for your translations.