Reputation: 718
In the project's settings file, if we want to restrict language choices for i18n, we are supposed to write this:
gettext = lambda s: s
LANGUAGES = (
('Fr', gettext('French')),
('en', gettext('English')),
)
but I write this:
LANGUAGES = (
('fr', 'cool dudes'),
('en', 'Anglais')
)
Whatever I put in the second item of the tuples (with "gettext = lambda s: s" or not), Django brings back "Français" and "English" in my language selector in a rendered page… I've also tried to raw language data in that selector's captions:
{'code':'fr', 'name':'French', 'bidi':False, 'name_local':u'Fran\xe7ais'}
{'code':'en', 'name':'English', 'bidi':False, 'name_local':u'English'}
It puzzles me, so what's the point to have 2-items tuples for this setting?
Upvotes: 1
Views: 296
Reputation: 11405
The Django documentation for Settings describes this second value and how to use it.
...With this arrangement, django-admin.py makemessages will still find and mark these strings for translation, but the translation won't happen at runtime -- so you'll have to remember to wrap the languages in the real gettext() in any code that uses LANGUAGES at runtime.
The second string in the two-tuple is intended to be a human-readable rendition of the language name. Wrap the second value in a call to gettext()
in order to mark them as translatable strings. But the normal implementation of gettext()
isn't available in the settings module, so you have to define a dummy implementation, as your code shows. If you want the language name localised, you do need to wrap the value in the actual gettext()
at the point of use.
You don't show your template code for the language selector UI. Are you using get_language_info()
? The example the docs give doesn't show it, but it looks to me like you'd need to wrap the name_local
element in _()
. (But I haven't tested this.)
from django.utils.translation import get_language_info
li = get_language_info('de')
print(li['name'], _(li['name_local']), li['bidi'])
Upvotes: 1