开发者

Why would Django localised template strings fail?

开发者 https://www.devze.com 2023-03-16 23:27 出处:网络
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 gett

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开发者_StackOverflow社区 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?


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/


I hate to ask this but, have you set USE_I18N = True in settings.py ?

Also you wrote {%blocktranslate%} in your question, whereas it should be {% blocktrans %}


Given all of that, the only things remaining are 1) the actual translation code is not being called at all (for some reason), or 2) it is bailing out before it gets its work done.

I keep a personal copy of Django around (i.e. not the system copy) so I can stick print's deep in the guts of something. Try putting some prints in django/templatetags/i18n.py and see if you are actually arriving in the right place.

0

精彩评论

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