Search code examples
djangolocale

Why would Django localised template strings fail?


I have a standard localised project set up. I have a couple of languages defined in my settings.py. I'm using LocaleMiddleware. I have a few {%blocktrans%} strings in my template file, which are getting pulled into my django.po file. I have run django-admin.py compilemessages and verified that the .mo file is created.

  • There are no 'fuzzy' entries in the .po.
  • In the template, {% get_current_language as LANGUAGE_CODE %}{{LANGUAGE_CODE}} gives the correct language (in this case, zh-cn)
  • I have restarted the [development] server
  • I have ENABLE_I18N = True and USE_L10N = True
  • I have set LANGUAGE_CODE and LANGUAGES in settings.py

When I render view, I know that the right locale is selected because view code that uses it (request.session.get('django_language', settings.LANGUAGE_CODE)) gets the right code. But the translations in the strings in the template just don't get shown.

How do I go about trying to solve this?


Solution

  • Turns out I'd missed something that was simultaneously obvious and not obvious. There are two different standards at work. Now I have fixed it, my settings file has:

    LANGUAGES = (
        ('en', "English"),
        ('zh-CN', "中文")
    )
    

    But my locale directory is locale/zh_CN. I foolishly assumed the same locale standard would be used throughout Django localisation. I was wrong.

    Further reading:

    File where the hyphen type is used: https://code.djangoproject.com/browser/django/trunk/django/conf/global_settings.py

    Docs where the underscore is used. Very last paragraph at the end of this page: https://docs.djangoproject.com/en/dev/topics/i18n/deployment/