Skip to main content

django-lang PyPi license

PyPi status PyPi version PyPi python version PyPi downloads PyPi downloads PyPi downloads

GitHub GitHub release GitHub release

Test codecov.io pre-commit.ci status gitthub.com

Check Demo Project

  • Check the demo repo on GitHub

Requirements

  • Python 3.9+ supported.
  • Django 3.2+ supported.

Setup

  1. Install from pip:

    pip install django-lang
    
  2. Modify settings.py by adding the app to INSTALLED_APPS:

    INSTALLED_APPS = [
        # ...
        "lang",
        # ...
    ]
    
  3. Modify settings.py by adding the app to INSTALLED_APPS:

    TEMPLATES = [
        {
            "BACKEND": "django.template.backends.django.DjangoTemplates",
            "DIRS": [os.path.join(PROJECT_DIR, "templates")],
            "APP_DIRS": True,
            "OPTIONS": {
                "context_processors": [
                    "django.template.context_processors.debug",
                    "django.template.context_processors.request",
                    "django.contrib.auth.context_processors.auth",
                    "django.contrib.messages.context_processors.messages",
                    "lang.context_processors.from_settings",
                    "lang.context_processors.seo_i18n",
                ],
            },
        },
    ]
    

    Optional: lang.context_processors.language_switcher_next (fills redirect_to for the packaged language form).

Optional: SetLanguageNextPathMiddleware

Only needed if you use translated URL segments (gettext_lazy under i18n_patterns) and see language switches that POST to set_language but stay on the wrong prefix. Short how-to: docs/set_language_middleware.md.

  1. Modify your project's base template base.html to include language's switcher styles:
    <head>
        ...
        <link rel="stylesheet" type="text/css" href="{% static 'lang/css/nav-link.css' %}">
        ...
    </head>
    
  2. Modify your project's base template base.html to include attributes using translate_url template's tag:
    <head>
        ...
        <meta name="language" content="{{ LANGUAGE_CODE }}" />
        {% include "hreflang.html" %}
        ...
    </head>
    
  3. Modify your project's nav template nav.html to include language's switcher:
    <nav class="navbar">
        ...
        <ul class="nav navbar-nav">
            {% include "lang/nav-link.html" %}
        </ul>
        ...
    </nav>
    

Configuration: lang.conf and APP_CONFIG

Built-in maps live in lang.defaults. At runtime, :mod:lang.conf resolves values lazily:

  1. Top-level Django settings (LANGUAGE_HREFLANG_MAP, LANGUAGE_WIKIPEDIA_SAMEAS, OG_LOCALE_BY_LANGUAGE, HREFLANG_DEFAULT_LANGUAGE, LANGUAGE_FLAG_MAP), if set.
  2. Partial dicts under settings.APP_CONFIG["lang"] merged onto the package defaults (for the three LANGUAGE_* / OG_* maps and flag overrides).
  3. Otherwise the content of lang.defaults.

You do not need to import lang.defaults from settings.py. Typical site-only override for x-default::

APP_CONFIG = {
    "lang": {
        "HREFLANG_DEFAULT_LANGUAGE": "it",
    },
}

Or use the usual flat settings (full replacement for the dict keys, or HREFLANG_DEFAULT_LANGUAGE at top level) if you prefer.

Optional: language control beside the hamburger on small viewports

nav-link-standalone.css shows the extra switcher next to the menu toggle below the lg breakpoint (~992px) and hides the duplicate inside the collapsed drawer. Optional script display-standalone-class.js only adds class display-standalone on <html> for installed web apps; layout no longer depends on it.

  1. In settings.py, add the optional context processor so redirect_to is filled for set_language’s next (unless you pass redirect_to from each view):
    "lang.context_processors.language_switcher_next",
    
  2. In the base layout <head>, after nav-link.css, add:
    <link rel="stylesheet" href="{% static 'lang/css/nav-link-standalone.css' %}">
    <script src="{% static 'lang/js/display-standalone-class.js' %}"></script>
    
  3. Next to your mobile menu button, include:
    {% include "lang/nav-link-standalone.html" %}
    

Packaged templates:

  • hreflang.html (app template root) — <link rel="alternate" hreflang="…"> for the current view.
  • lang/nav-link.html — language <select> (optional context: lang_switcher_id, lang_switcher_extra_class).
  • lang/nav-link-standalone.html — duplicate switcher beside the mobile toggle (small viewports / PWA).

Run Example Project

git clone --depth=50 --branch=django-lang https://github.com/DLRSP/example.git DLRSP/example
cd DLRSP/example
python manage.py runserver

Now browser the app @ http://127.0.0.1:8000

References

Download files

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

Source Distribution

django_lang-0.6.5.tar.gz (31.9 kB view details)

Uploaded Source

Built Distribution

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

django_lang-0.6.5-py3-none-any.whl (22.5 kB view details)

Uploaded Python 3

File details

Details for the file django_lang-0.6.5.tar.gz.

File metadata

  • Download URL: django_lang-0.6.5.tar.gz
  • Upload date:
  • Size: 31.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for django_lang-0.6.5.tar.gz
Algorithm Hash digest
SHA256 cd85d4d67b604b70c2594a50f5849f9083963d1c096232e62dc12cda4b5ad6b9
MD5 e18ffe7cb62b1ddbc26577037b545067
BLAKE2b-256 96340d7ad784e5572e4ae93c25eb3be72d6bb9624b805b8549ec965ac267c6ca

See more details on using hashes here.

File details

Details for the file django_lang-0.6.5-py3-none-any.whl.

File metadata

  • Download URL: django_lang-0.6.5-py3-none-any.whl
  • Upload date:
  • Size: 22.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for django_lang-0.6.5-py3-none-any.whl
Algorithm Hash digest
SHA256 13a5dd48fb24d00615e9651eebf479e870a5d4c84df7530c657f3e2e2e3a52c6
MD5 e865c123a033df755d31aa0df9840c54
BLAKE2b-256 9fe121eabb84c6c2dba6af7e9d0594d38ff8f14e7fd10d3a88aea34fd5850ade

See more details on using hashes here.

Release history Release notifications | RSS feed

0.6.9

2 files

0.6.8

2 files

0.6.7

2 files

0.6.6

2 files

This release

0.6.5 This release

2 files

0.6.4

2 files

0.6.3

2 files

0.6.0

2 files

0.4.2

2 files

0.4.1

2 files

0.3.2

2 files

0.3.1

2 files

0.3.0

2 files

0.2.1

2 files

0.2.0

2 files

0.1.0

2 files

Supported by

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