Skip to main content

Reusable Django app for dynamic multilingual texts with template tags.

Project description

Django DynamicTexts

Django DynamicTexts is a reusable Django library for dynamically storing and displaying texts (with a unique key, title, and content) with full multilingual (i18n) support. It is ideal for project localization, content management, and SEO-friendly pages.


Features

  • Store texts with a unique key, title, and content.
  • Full translation support (e.g., for English and Latvian) using django-modeltranslation.
  • Easy usage in Django templates via custom template tags.
  • Content editing via the Django admin interface.

Installation

  1. Add the app and modeltranslation to INSTALLED_APPS:
    INSTALLED_APPS = [
        ...
        'modeltranslation',
        'texts',
    ]
    
  2. Configure languages:
    LANGUAGES = [
        ('en', 'English'),
        ('lv', 'Latviešu'),
    ]
    MODELTRANSLATION_DEFAULT_LANGUAGE = 'en'
    MODELTRANSLATION_LANGUAGES = ('en', 'lv')
    ENABLE_DYNAMIC_TEXT_TRANSLATIONS = True
    USE_I18N = True
    MIDDLEWARE = [
        ...
        'django.contrib.sessions.middleware.SessionMiddleware',
        'django.middleware.locale.LocaleMiddleware',
        'django.middleware.common.CommonMiddleware',
        ...
    ]
    
  3. Run migrations:
    python manage.py makemigrations texts
    python manage.py migrate
    
  4. Install dependencies:
    pip install django-modeltranslation
    

Usage

  1. Add/edit texts in the admin interface (/admin/), filling in all required translation fields.
  2. Load template tags in your template:
    {% load dynamictexts_tags %}
    
  3. Display texts in your template:
    <h1>{% get_title "about-us" %}</h1>
    <p>{% get_content "about-us" %}</p>
    
    • The correct translation will be shown automatically based on the active language.

Language Switching

  • Use Django's i18n set_language view or another language switching mechanism.
  • Example language switcher form in a template:
    <form action="/i18n/setlang/" method="post">{% csrf_token %}
        <input name="next" type="hidden" value="{{ request.path }}">
        <select name="language">
            <option value="en">English</option>
            <option value="lv">Latviešu</option>
        </select>
        <input type="submit" value="Change language">
    </form>
    <p>Active language: {{ LANGUAGE_CODE }}</p>
    

Best Practices

  • Always fill in all translation fields in the admin interface.
  • Ensure LocaleMiddleware is in the correct place in the MIDDLEWARE list.
  • Use template tags only after {% load dynamictexts_tags %}.
  • If you add new languages, update LANGUAGES and run migrations.

License

MIT

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_dynamictexts-0.1.0.tar.gz (8.6 kB view details)

Uploaded Source

Built Distribution

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

django_dynamictexts-0.1.0-py3-none-any.whl (13.4 kB view details)

Uploaded Python 3

File details

Details for the file django_dynamictexts-0.1.0.tar.gz.

File metadata

  • Download URL: django_dynamictexts-0.1.0.tar.gz
  • Upload date:
  • Size: 8.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.2

File hashes

Hashes for django_dynamictexts-0.1.0.tar.gz
Algorithm Hash digest
SHA256 7e0beb8f6e5d4324745ef1591a9570852caf9e5a14f3c931dee34b7568a9f169
MD5 59c111d279620fb924465cf5a1a10043
BLAKE2b-256 c27f6017375dc1f7d00ec87e44e658d66dd9f2875d66845a2f29b0d7e1383fa4

See more details on using hashes here.

File details

Details for the file django_dynamictexts-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for django_dynamictexts-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 9114e9f85a60dea36905549b9a3479ce8167e7cf2926d6ebbd794e96340a9d68
MD5 913eec884d26e32d9644347a02dc982b
BLAKE2b-256 f3f759dce1fc07ae573a37098800ed61d72f2041b057ae1a9eb96a60879f68e3

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