# WSGI Language Middleware
## Why?
This Wsgi middleware, takes the language from urls similiar to "/es/documents" or from the Accept language header.
## Instalation
### Prerequisites:
* Python >= 2.6
* Ludibrio (for the tests)
### Install
python setup.py install
## Test
python setup.py -q test
## Usage
### Basic
from language_middleware import LanguageMiddleware
application = my_wsgi_app()
application = LanguageMiddleware(application, default_language = 'es', valid_languages = ('en', 'es', 'fr'))
Call to the url **/fr/documents**, and my_wsgi_app will receive:
environ['HTTP_ACTIVE_LANGUAGE'] = 'fr'
Call to the url **/documents/1**, and my_wsgi_app will receive:
environ['HTTP_ACTIVE_LANGUAGE'] = 'es'
### Clean URL
The clean_url option, removes the language info from the URL.
from language_middleware import LanguageMiddleware
application = my_wsgi_app()
application = LanguageMiddleware(application, default_language = 'es', valid_languages = ('en', 'es', 'fr'), clean_url = True)
Call to the url **/fr/documents**, and my_wsgi_app will receive:
environ['PATH_INFO'] = '/documents'
environ['HTTP_ACTIVE_LANGUAGE'] = 'fr'
Call to the url **/documents/1**, and my_wsgi_app will receive:
environ['PATH_INFO'] = '/documents/1'
environ['HTTP_ACTIVE_LANGUAGE'] = 'es'
### With locale
If you have the following locale directory
/home/user/locale/es/LC_MESSAGES/hello.po
/home/user/locale/es/LC_MESSAGES/hello.mo
/home/user/locale/en/LC_MESSAGES/hello.po
/home/user/locale/en/LC_MESSAGES/hello.mo
You can set an option to active the language, check the test from more info:+
IMPORTANT: **Default language locale path must exists, if not last successfully used locale will be used (if any)**
from language_middleware import LanguageMiddleware
application = my_wsgi_app()
application = LanguageMiddleware(
application,
default_language = 'es',
valid_languages = ('en', 'es', 'fr')
clean_url = True,
locale_path = '/home/user/locale',
locale_name = 'hello'
)
### Set Script Name
Starting with wsgi-language-middleware 0.4 you can pass **set_script_name** to **LanguageMiddleware**, it will set the **SCRIPT_NAME** to a slash followed by the chosen language (e.g. **/es**)
This can help you if your application uses url-creation helpers (e.g. pylons **h.url()**), which do rely on the script name and should automatically prepend links with the given locale.
### Switching off global gettext translation installation
If you want to take care of the locale loading yourself (e.g. by inspecting environ['HTTP_ACTIVE_LANGUAGE'] later on) or do not use gettext, you can suppress the installation of the gettext translation by using **do_gettext_install=False** in **LanguageMiddleware()** .
## Why?
This Wsgi middleware, takes the language from urls similiar to "/es/documents" or from the Accept language header.
## Instalation
### Prerequisites:
* Python >= 2.6
* Ludibrio (for the tests)
### Install
python setup.py install
## Test
python setup.py -q test
## Usage
### Basic
from language_middleware import LanguageMiddleware
application = my_wsgi_app()
application = LanguageMiddleware(application, default_language = 'es', valid_languages = ('en', 'es', 'fr'))
Call to the url **/fr/documents**, and my_wsgi_app will receive:
environ['HTTP_ACTIVE_LANGUAGE'] = 'fr'
Call to the url **/documents/1**, and my_wsgi_app will receive:
environ['HTTP_ACTIVE_LANGUAGE'] = 'es'
### Clean URL
The clean_url option, removes the language info from the URL.
from language_middleware import LanguageMiddleware
application = my_wsgi_app()
application = LanguageMiddleware(application, default_language = 'es', valid_languages = ('en', 'es', 'fr'), clean_url = True)
Call to the url **/fr/documents**, and my_wsgi_app will receive:
environ['PATH_INFO'] = '/documents'
environ['HTTP_ACTIVE_LANGUAGE'] = 'fr'
Call to the url **/documents/1**, and my_wsgi_app will receive:
environ['PATH_INFO'] = '/documents/1'
environ['HTTP_ACTIVE_LANGUAGE'] = 'es'
### With locale
If you have the following locale directory
/home/user/locale/es/LC_MESSAGES/hello.po
/home/user/locale/es/LC_MESSAGES/hello.mo
/home/user/locale/en/LC_MESSAGES/hello.po
/home/user/locale/en/LC_MESSAGES/hello.mo
You can set an option to active the language, check the test from more info:+
IMPORTANT: **Default language locale path must exists, if not last successfully used locale will be used (if any)**
from language_middleware import LanguageMiddleware
application = my_wsgi_app()
application = LanguageMiddleware(
application,
default_language = 'es',
valid_languages = ('en', 'es', 'fr')
clean_url = True,
locale_path = '/home/user/locale',
locale_name = 'hello'
)
### Set Script Name
Starting with wsgi-language-middleware 0.4 you can pass **set_script_name** to **LanguageMiddleware**, it will set the **SCRIPT_NAME** to a slash followed by the chosen language (e.g. **/es**)
This can help you if your application uses url-creation helpers (e.g. pylons **h.url()**), which do rely on the script name and should automatically prepend links with the given locale.
### Switching off global gettext translation installation
If you want to take care of the locale loading yourself (e.g. by inspecting environ['HTTP_ACTIVE_LANGUAGE'] later on) or do not use gettext, you can suppress the installation of the gettext translation by using **do_gettext_install=False** in **LanguageMiddleware()** .
Release files for language_middleware 0.6
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| language_middleware-0.6.tar.gz | 3.1 kB | Details |
Release files / language_middleware-0.6.tar.gz
| Download URL | language_middleware-0.6.tar.gz |
|---|---|
| Size | 3.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
64da8a9761af67c479b27159a7a48a56bb3e9817f26afd4a2e3bca147037838e
|
|
BLAKE2b-256 checksum How to use checksums |
7cdaec806ccc14255de4f0a5564775f54f1d1fa9bf8377c651922d8e3d8aa531
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |