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.
- 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.
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.
-
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
-
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
-
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")), ]
-
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.
- 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.