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.2.tar.gz (13.2 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.2-py3-none-any.whl (13.0 kB view details)

Uploaded Python 3

File details

Details for the file django_sage_language-0.1.2.tar.gz.

File metadata

  • Download URL: django_sage_language-0.1.2.tar.gz
  • Upload date:
  • Size: 13.2 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.2.tar.gz
Algorithm Hash digest
SHA256 c647f4a988fe91e27a4210ebcc3b0a851edab63ab589c2cdec38118a30f41bb8
MD5 2b53afd5b1e918c6d9ef9567be91faf4
BLAKE2b-256 c4e4ea48e2b7c303593f297742f2c0fd7da358a47768a42a19a9d9a2f50a935d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for django_sage_language-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 e691c6e49b184348991e28903d1cf98176eeb8b8cd9b539121b3c301d8fb6ce5
MD5 4964167d64a98c53bdc6964e6e5c21a5
BLAKE2b-256 45ebdcb83cd9d8078cf566f21662c31e14ef36108383fedf673b612e93df58aa

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