Use default language for urls without language prefix (django)
solid_i18n contains middleware and url patterns to use default language at root path (without language prefix).
Default language is set in settings.LANGUAGE_CODE.
Starting from Django 1.10, built-in i18n_patterns accept optional argument prefix_default_language. If it is False, then Django will serve url without language prefix by itself. Look docs for more details.
This package can still be useful in following cases (look below for settings details): - You need settings.SOLID_I18N_USE_REDIRECTS = True behaviour - You need settings.SOLID_I18N_HANDLE_DEFAULT_PREFIX = True behaviour - You need settings.SOLID_I18N_DEFAULT_PREFIX_REDIRECT = True behaviour - You need settings.SOLID_I18N_PREFIX_STRICT = True behaviour
In all other cases no need in current package, just use Django>=1.10.
There are two modes:
Install this package to your python distribution:
pip install solid_i18n
Set languages in settings.py:
# Default language, that will be used for requests without language prefix LANGUAGE_CODE = 'en' # supported languages LANGUAGES = ( ('en', 'English'), ('ru', 'Russian'), ) # enable django translation USE_I18N = True # Optional. If you want to use redirects, set this to True SOLID_I18N_USE_REDIRECTS = False
Add SolidLocaleMiddleware instead of LocaleMiddleware to MIDDLEWARE_CLASSES:
MIDDLEWARE_CLASSES = ( 'django.contrib.sessions.middleware.SessionMiddleware', 'solid_i18n.middleware.SolidLocaleMiddleware', 'django.middleware.common.CommonMiddleware', )
Use solid_i18n_patterns instead of i18n_patterns
from django.conf.urls import patterns, include, url from solid_i18n.urls import solid_i18n_patterns urlpatterns = solid_i18n_patterns( url(r'^about/$', 'about.view', name='about'), url(r'^news/', include(news_patterns, namespace='news')), )
Start the development server and visit http://127.0.0.1:8000/about/ to see english content. Visit http://127.0.0.1:8000/ru/about/ to see russian content. If SOLID_I18N_USE_REDIRECTS was set to True and if your preferred language is equal to Russian, request to path http://127.0.0.1:8000/about/ will be redirected to http://127.0.0.1:8000/ru/about/. But if preferred language is English, http://127.0.0.1:8000/about/ will be shown.
# settings.py LANGUAGES = ( ('en', 'English'), ('my', 'Burmese'), ) # urls.py urlpatterns = solid_i18n_patterns('', url(r'^my-slug/$', some_view), )
If SOLID_I18N_PREFIX_STRICT=False, then url /my-slug/ will respond with 404, since language my-slug is not found. This happens, because we have a registered language tag my. Language tag can have form like this:
So django in this case tries to find language ‘my-slug’. But it fails and that is why django respond 404. And your view some_view will not be called.
But, if we set SOLID_I18N_PREFIX_STRICT=True, then resolve system will get language only from exact ‘my’ prefix. In case of /my-slug/ url the prefix is not exact, and our some_view will be found and called.
Located here, it is ready to use, just install solid_i18n (this package):
pip install solid_i18n
clone example site:
git clone https://github.com/st4lk/django-solid-i18n-urls.git
step in example/ and run development server:
cd django-solid-i18n-urls/example python manage.py runserver
Add python 3.2, 3.3 support.
Add django 1.6 support
Update README and data for pypi
First version in pypi
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
|File Name & Checksum SHA256 Checksum Help||Version||File Type||Upload Date|
|solid_i18n-1.4.1-py2.py3-none-any.whl (14.1 kB) Copy SHA256 Checksum SHA256||2.7||Wheel||Feb 4, 2017|
|solid_i18n-1.4.1.tar.gz (13.7 kB) Copy SHA256 Checksum SHA256||–||Source||Feb 4, 2017|