Skip to main content

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: CookieLocaleMiddleware extends Django's default LocaleMiddleware, 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: Contains CookieLocaleMiddleware to manage language preferences dynamically.
  • utils/locale.py: Provides MultilingualService, a utility class for adding and removing language prefixes in URLs.
  • views/locale.py: Implements SetLanguageView, 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, including LANGUAGES, LANGUAGE_CODE, and middleware setup.
  • urls.py: Configures routes for language switching and i18n patterns.

Installation

Using pip

  1. Create a Virtual Environment:

    python -m venv .venv
    
  2. Activate the Virtual Environment:

    • On Windows:
      .venv\Scripts\activate
      
    • On macOS and Linux:
      source .venv/bin/activate
      
  3. Install the Package:

    pip install python-sage-bbb
    

Using Poetry

  1. Install Poetry: Follow the official installation instructions at the Poetry website.

  2. Create a New Project (Optional):

    poetry new myproject
    cd myproject
    
  3. Add the Package as a Dependency:

    poetry add python-sage-bbb
    
  4. Activate the Virtual Environment:

    poetry shell
    

Configuration

  1. Add sage_language to INSTALLED_APPS in settings.py:

    INSTALLED_APPS = [
        ...
        'sage_language',
    ]
    
  2. Middleware Configuration: Ensure CookieLocaleMiddleware is added immediately after SessionMiddleware in the MIDDLEWARE setting:

    MIDDLEWARE = [
        'django.middleware.security.SecurityMiddleware',
        'django.contrib.sessions.middleware.SessionMiddleware',
        'sage_language.middlewares.cookie.CookieLocaleMiddleware',
        ...
    ]
    
  3. 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
    
  4. 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

  1. Setting Language Preference: To change the language preference, make a POST request to /set-language/ with language and next parameters:

    • language: The desired language code (e.g., 'en', 'fa', 'es').
    • next: The URL to redirect to after setting the language.
  2. URL Prefix Management: The middleware automatically adds or removes language prefixes from URLs based on the user’s selected language and cookie settings.

  3. 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, and USE_TZ to True in settings.py for proper internationalization support.
  • For production use, ensure SAGE_LANGUAGE_COOKIE_NAME and LANGUAGE_COOKIE_NAME are set to the same value for consistent cookie management.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_sage_language-0.1.1.tar.gz (11.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_sage_language-0.1.1-py3-none-any.whl (11.0 kB view details)

Uploaded Python 3

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

Hashes for django_sage_language-0.1.1.tar.gz
Algorithm Hash digest
SHA256 16e5c66b74155f0dbc79aa3af6e4c37f0a1ecb25dd7691d84e9e5cac4da57c59
MD5 dea277a0f131b419a4afd49baf5910a7
BLAKE2b-256 194521d9929e29afd85abcc1679c6a0b2de79f92e0bf5d6c7a1182d8184b9238

See more details on using hashes here.

File details

Details for the file django_sage_language-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for django_sage_language-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 c46ad8f132f9006a3c33d6889de99aa6c5e75cae92ab520eb642e805a7c8d376
MD5 fabba39114c72026fe2c4fa5cff7b7ee
BLAKE2b-256 2d1f814151150fb46ddb1e195382aa675cee595fec08159a6b5b8f84848db71b

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page