How to maintain different country versions of same language in Django?

巧了我就是萌 提交于 2020-01-12 06:56:29

问题


I would like to have a few different versions of the same language in Django, customized for different countries (e.g. locale/en, locale/en_CA, locale/en_US, etc.). If there is no language for specific country I would expect to use the default language version (locale/en)).

Then in the settings file for each site I specify LANGUAGE_CODE and LANGUAGES.

For some reason, even if I specify the following settings, the locale/en_US translations are still used:

LANGUAGE_CODE = 'en'
LANGUAGES = (
    ('en', ugettext('English')),
)

Though I clearly specify that the language code should be en (not en-us).

Am I missing something? Already tried to find the answer in multiple places, including Django documentation.


回答1:


This is a quirk of Python (not specifically Django) and the gettext module.

Ticket 8626 was raised on the Django issue tracker around the time of the 1.0 release and after some suggestions and debate, the Django devs deemed it a "won't fix".

There are suggestions in the ticket thread to use 'en-en' as the default. My memory is a little rough but if I recall correctly this approach didn't play well with other parts of my i18n tooling (e.g. the pox library). I gave up and settled for using en-US as the default for the project and listing the other variants (e.g. en-au) as alternatives.




回答2:


A workaround to the issue would be to add following snippet to your settings.py file.

import locale
locale.locale_alias.pop('en', None)

Special credit to Venelin Stoykov who was able to investigate the behavior of the Python locale module.




回答3:


may I suggest you to put a breakpoint into the LocaleMiddleware Class?

In this way, you might found out a clue which thing was actually getting your way from getting the right Language.

Becaue according the source code of LocaleMiddleware Class and the How Django discovers language preference , there could be so many things can affect the result.



来源:https://stackoverflow.com/questions/39577656/how-to-maintain-different-country-versions-of-same-language-in-django

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!