Internationalization#

Demo#

This two-minute demo highlights how translations work in Pegasus apps.

Localization#

Pegasus ships with full support for localizing user-facing text.

Currently, not all the user-facing text is properly tagged for localization but this will be incrementally addressed in future releases.

For full documentation on localization see the Django docs.

Configuration#

Define the list of languages that will be available on your site:

from django.utils.translation import gettext_lazy

LANGUAGES = [
    ('en', gettext_lazy('English')),
    ('fr', gettext_lazy('French')),
]

When adding a new language you will need to create the language files by running:

python ./manage.py makemessages -l [new lang code] --ignore node_modules --ignore venv

After running this, you can update the language files as new strings are added by running:

python ./manage.py makemessage --all --ignore node_modules --ignore venv
python ./manage.py makemessages -d djangojs --all --ignore node_modules --ignore venv
python ./manage.py compilemessages

Basic Usage#

In Python:

from django.utils.translation import gettext

def my_view(request):
    output = gettext("Welcome to my site.")
    return HttpResponse(output)

See the Django docs for more.

In Django templates:

{% load i18n %}
<title>{% translate "This is the title." %}</title>

See the Django docs for more.

In JavaScript:

document.write(gettext('this is to be translated'));

See the Django docs for more.

In Wagtail:

See the Wagtail docs.

Technical notes#

Pegasus is configured to use cookies to track the current locale. This allows localization to work for both authenticated and unauthenticated users.

More information on this approach is available the Django docs: How Django discovers language preference