ShipEasy Docs

Django

ShipEasyI18n integration for Django — i18n_t template tag, profile-detection middleware, Jinja2 extension, and settings configuration.

No pip package is required. ShipEasyI18n for Django uses a custom template tag and optional middleware.

Settings

Add ShipEasyI18n configuration to settings.py:

# settings.py
ShipEasyI18n_KEY = os.environ.get('ShipEasyI18n_KEY', '')
ShipEasyI18n_PROFILE = 'en:prod'
ShipEasyI18n_CDN = 'https://cdn.i18n.shipeasy.ai/v1/loader.js'

Template tag setup

Create templatetags/i18n_tags.py in any installed app:

# myapp/templatetags/i18n_tags.py
from django import template
from django.utils.html import format_html
import json

register = template.Library()

@register.simple_tag
def i18n_t(key, **variables):
    """Render a label placeholder that ShipEasyI18n rewrites in the browser."""
    data_vars = f' data-variables="{json.dumps(variables)}"' if variables else ''
    return format_html('<span data-label="{}"{}>{}</span>', key, data_vars, key)

Using the template tag

Load and use i18n_t in any Django template:

{% load i18n_tags %}

<head>
  <title>{% i18n_t 'site.title' %}</title>
  <script
    src="{{ ShipEasyI18n_CDN }}"
    data-key="{{ ShipEasyI18n_KEY }}"
    data-profile="{{ ShipEasyI18n_PROFILE }}"
    async
  ></script>
</head>

<nav>
  <a href="/">{% i18n_t 'nav.home' %}</a>
  <a href="/account/">{% i18n_t 'nav.account' %}</a>
</nav>

Pass variables as keyword arguments:

<h1>{% i18n_t 'user.greeting' name=user.first_name %}</h1>

Profile-detection middleware

Detect the ShipEasyI18n profile from a cookie or Accept-Language header and make it available to templates:

# myapp/middleware.py
class ShipEasyI18nMiddleware:
    def __init__(self, get_response):
        self.get_response = get_response

    def __call__(self, request):
        profile = (
            request.COOKIES.get('i18n_profile')
            or request.META.get('HTTP_X_ShipEasyI18n_PROFILE')
            or 'en:prod'
        )
        request.i18n_profile = profile
        response = self.get_response(request)
        return response

Register it in settings.py:

MIDDLEWARE = [
    # ...
    'myapp.middleware.ShipEasyI18nMiddleware',
]

Jinja2 extension

If you use Jinja2 via django-jinja, register the ShipEasyI18n extension:

# settings.py
TEMPLATES = [{
    'BACKEND': 'django_jinja.backend.Jinja2',
    'OPTIONS': {
        'extensions': ['i18n.jinja2ext.ShipEasyI18nExtension'],
        'globals': {'i18n_key': ShipEasyI18n_KEY, 'i18n_profile': ShipEasyI18n_PROFILE},
    },
}]

Then use the global function in Jinja2 templates:

{{ i18n_t('nav.home') }}

For the full implementation spec including package source code and edge cases, see plans/frameworks/django.md in the repository.