A Django Project for changing language.
Project description
Sage Language Middleware for Django
This Django project provides middleware and utility classes to support multilingual applications through URL prefixes and cookies for language settings. The core feature is a custom CookieLocaleMiddleware, which dynamically manages user language preferences and redirects users to the appropriate language version of the site based on URL paths or cookies.
Features
- Dynamic Language Detection: Determines the preferred language by examining URL prefixes and cookies.
- Custom Middleware:
CookieLocaleMiddlewareextends Django's defaultLocaleMiddleware, enhancing support for multilingual content. - URL Prefix Management: Utilities to add or remove language prefixes in URLs, supporting a clean and consistent URL structure.
- Customizable Language Settings: Integrates with Django's internationalization settings (
LANGUAGES,LANGUAGE_CODE, etc.) for easy customization. - Error Checking: Ensures proper configuration of language settings and middleware placement.
Project Structure
middlewares/cookie.py: ContainsCookieLocaleMiddlewareto manage language preferences dynamically.utils/locale.py: ProvidesMultilingualService, a utility class for adding and removing language prefixes in URLs.views/locale.py: ImplementsSetLanguageView, allowing users to set their preferred language through a POST request.checks.py: Validates the configuration of middleware, language settings, and cookies.settings.py: Configures Django's settings, includingLANGUAGES,LANGUAGE_CODE, and middleware setup.urls.py: Configures routes for language switching and i18n patterns.
Installation
Using pip
-
Create a Virtual Environment:
python -m venv .venv
-
Activate the Virtual Environment:
- On Windows:
.venv\Scripts\activate
- On macOS and Linux:
source .venv/bin/activate
- On Windows:
-
Install the Package:
pip install python-sage-bbb
Using Poetry
-
Install Poetry: Follow the official installation instructions at the Poetry website.
-
Create a New Project (Optional):
poetry new myproject cd myproject
-
Add the Package as a Dependency:
poetry add python-sage-bbb
-
Activate the Virtual Environment:
poetry shell
Configuration
-
Add
sage_languagetoINSTALLED_APPSinsettings.py:INSTALLED_APPS = [ ... 'sage_language', ]
-
Middleware Configuration: Ensure
CookieLocaleMiddlewareis added immediately afterSessionMiddlewarein theMIDDLEWAREsetting:MIDDLEWARE = [ 'django.middleware.security.SecurityMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'sage_language.middlewares.cookie.CookieLocaleMiddleware', ... ]
-
Set Language Settings: Define supported languages, default language, and cookie names:
LANGUAGES = [ ('en', 'English'), ('fa', 'Farsi'), ('es', 'Spanish'), ] LANGUAGE_CODE = 'en' SAGE_LANGUAGE_COOKIE_NAME = "ivan_language" LANGUAGE_COOKIE_NAME = SAGE_LANGUAGE_COOKIE_NAME
-
Add URL Configuration for Language Switching: In
urls.py, set up the language switching view:from sage_language.views import SetLanguageView urlpatterns = [ path('set-language/', SetLanguageView.as_view(), name='set_language'), path("i18n/", include("django.conf.urls.i18n")), ]
Usage
-
Setting Language Preference: To change the language preference, make a POST request to
/set-language/withlanguageandnextparameters:language: The desired language code (e.g., 'en', 'fa', 'es').next: The URL to redirect to after setting the language.
-
URL Prefix Management: The middleware automatically adds or removes language prefixes from URLs based on the user’s selected language and cookie settings.
-
Automatic Language Redirection: If a user’s preferred language (stored in a cookie) differs from the language in the URL, the middleware will redirect them to the correct URL.
Notes
- Make sure to set
USE_I18N,USE_L10N, andUSE_TZtoTrueinsettings.pyfor proper internationalization support. - For production use, ensure
SAGE_LANGUAGE_COOKIE_NAMEandLANGUAGE_COOKIE_NAMEare set to the same value for consistent cookie management.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file django_sage_language-0.1.1.tar.gz.
File metadata
- Download URL: django_sage_language-0.1.1.tar.gz
- Upload date:
- Size: 11.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.3 CPython/3.12.4 Windows/11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
16e5c66b74155f0dbc79aa3af6e4c37f0a1ecb25dd7691d84e9e5cac4da57c59
|
|
| MD5 |
dea277a0f131b419a4afd49baf5910a7
|
|
| BLAKE2b-256 |
194521d9929e29afd85abcc1679c6a0b2de79f92e0bf5d6c7a1182d8184b9238
|
File details
Details for the file django_sage_language-0.1.1-py3-none-any.whl.
File metadata
- Download URL: django_sage_language-0.1.1-py3-none-any.whl
- Upload date:
- Size: 11.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.3 CPython/3.12.4 Windows/11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c46ad8f132f9006a3c33d6889de99aa6c5e75cae92ab520eb642e805a7c8d376
|
|
| MD5 |
fabba39114c72026fe2c4fa5cff7b7ee
|
|
| BLAKE2b-256 |
2d1f814151150fb46ddb1e195382aa675cee595fec08159a6b5b8f84848db71b
|