Django internationalization: prefix_default_langua

2019-06-09 11:21发布

问题:

I'm adding internationalization to my Django project. The idea of how it should work is the following:

  • When a user enters "/" i.e. the "homepage" we try to get his/her language preferences from our session. If it's not our default language (EN), we redirect the user to the local version, like /ES/, /RU/, /CN/, etc

  • If we have no such session data, we check Accept-Language and redirect the user to the local version if we support such

  • If we don't support user's Accept-Language OR if it's EN - we don't redirect user! we just show the "default language"

I.e. all "not default languages" should have their prefixes, but default one won't. Why the default language should have a prefix, right? )))

The issue is, I can't figure out how to set this logic. If I set prefix_default_language=False to disable the /EN/ prefix for default language, it also disables the "not default" redirect. I found a package that solves the problem: https://github.com/st4lk/django-solid-i18n-urls#behaviour settings.SOLID_I18N_USE_REDIRECTS = True but it doesn't work in Django 2+

I can't believe I'm the only one hating the default language prefixes )) Although I could not find any solution with both redirect and no prefix for the default language. Hope someone will advice something here