开发者

Translate module (app) name in django

I am trying to translate the app names within my django project, I found this code on the web:

init.py

from django.utils.translation import gettext_noop
gettext_noop("AppName")

template

{% trans app.name %}

However in the current version of django this code is present, so I guess theres a way of doing this in django 1.3 already, (the code above is from 2008).. please advise how to get the name into translation files

{% for app in app_list %}
{% blocktrans with app.name as name %}{{ name }}{% endblocktrans %}

This also tells me that this should already be possible somehow:

django/contrib/admin/sites.py line 356+

       开发者_开发技巧     if app_label in app_dict:
                app_dict[app_label]['models'].append(model_dict)
            else:
                app_dict[app_label] = {
                    'name': app_label.title(),
                    'app_url': app_label + '/',
                    'has_module_perms': has_module_perms,
                    'models': [model_dict],
                }


Concerning blocktrans, you're misunderstanding how it works with values being passed in. If you have something like this:

{% blocktrans with foo=bar %}-- {{ foo }} --{% endblocktrans %}

This is roughly equivalent to either of the following in Python:

gettext('-- %(foo)s --') % {'foo': bar}
gettext('-- {foo} --').format(foo=bar)

The important thing here is that app.name will not be being translated. It's passed in verbatim, after translation. The msgid in the translation catalogue will be "-- %(foo)s --" (because it turns it into Python format for the translation lookup).

{% trans app.name %} will work, but it won't get the app names into the translation catalogue with manage.py makemessages, because this latter just scans through the code to work out what's there, statically. If you do something like gettext(app.name) in your Python code, it won't be able to work out what it should put in the catalogue because it has not evaluated the code, but just analysed it statically.

The final summary is that using gettext_noop is the correct way of getting the messages to appear in the translation catalogue. makemessages does not attempt to be too clever for its own good and leaves this to you.

Related to ticket 3591 is ticket 1668, which, while out of date, is probably worth while your reading if you haven't already (you may well have done as you've come across 3591).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜