简体   繁体   中英

How to use translations in Django?

I'm following the Django tutorials about translation, however I cannot make it work. I'm probably missing something so simple that they didn't bother to write, but I can't see what.

my settings.py has the following declarations :

LANGUAGE_CODE = 'en-us'
USE_I18N = True
_ = lambda s: s
LANGUAGES = (
  ('en', 'English'),
  ('fr', 'French'),
)
LOCALE_PATHS= (
                os.path.join( SITE_ROOT, 'locale').replace('\\','/'),
               )

my view looks like this :

from django.utils.translation import ugettext_lazy as _
from django.utils.translation import ugettext as __
#...
 def translation_test(request):
        output = __("Yes")
        return HttpResponse(output)

and my ProjectRoot/locale/fr/LC_MESSAGES/django.po file has this :

msgid "Yes"
msgstr "Oui"

So I expect my view to generate "Oui", however, it generates "Yes". What am I missing here?

PS I also tried with a template file, because I need this to work too:

my_template.html :

{% extends "base_site.html" %}
{% load i18n %}
<a>{% trans "Yes" %}</a>

Again, no translation. What should I do to make this work?

Thanks in advance.

French should be the active language to see the translated value.

Check https://docs.djangoproject.com/en/1.8/topics/i18n/translation/#how-django-discovers-language-preference to see how django determines the active language.

You can test the translation to see if there's something wrong with your translation config by settings LANGUAGE_CODE = 'fr'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM