79583202

Date: 2025-04-20 09:03:40
Score: 0.5
Natty:
Report link

A bit late but for anyone who find this I would recommand one of 2 options:

localized_template function

  1. write untranslated page templates in language-specific subfolders (ex en/my-page.html, en-gb/my-page.html, es/my-page.html)

  2. write a localized_template utility function, for ex in an app named my_app

# my_app/views.py

from os import path

from django.http import HttpResponse
from django.shortcuts import render
from django.utils.translation import get_language

def localized_template(name: str) -> str:
    return path.join(get_language(), name)

# usable in render like
def my_page(request: HttpRequest) -> HttpResponse:
    return render(request, localized_template("my-page.html"))

Translatable Page model

  1. create a model for the "long text content", for ex in this case a Page model from an app named my_app
# my_app/models.py

from django.db.models import Model, TextField

class Page(Model):
    content: TextField[str, str] = TextField()
  1. add django model translation or an equivalent and configure it for your app

  2. setup the model translation

# my_app/translation.py

from modeltranslation.translator import register, TranslationOptions
from .models import Page

@register(Page)
class PageTranslationOptions(TranslationOptions):
    fields = ('content',)

After that, how you edit the model is up to you. If you need to persist default data, you could dump it as fixtures:

./manage.py dumpdata my_app.page > default_pages.json
Reasons:
  • Long answer (-1):
  • Has code block (-0.5):
  • Contains question mark (0.5):
  • Unregistered user (0.5):
  • Low reputation (1):
Posted by: dialka