Internationalization (i18n) prepares your application for adaptation to different languages, regions, and cultures, while Localization (l10n) adapts the application to a specific locale. This guide explains how to implement both in Django.
i18n is the process of designing an application to easily support multiple languages, formats, and cultural conventions.
l10n involves adapting the application’s content and formats for a specific locale.
# Enable i18n and l10n in Django settings
USE_I18N = True
USE_L10N = True
USE_TZ = True
LANGUAGES = [
('en', 'English'),
('es', 'Spanish'),
('fr', 'French'),
('de', 'German'),
]
LOCALE_PATHS = [
os.path.join(BASE_DIR, 'locale'),
]
# Mark strings for translation in views
from django.utils.translation import gettext as _
def my_view(request):
message = _("Welcome to our website!")
return render(request, 'index.html', {'message': message})
# Generate translation files
django-admin makemessages -l es
# Spanish translation file
msgid "Welcome to our website!"
msgstr "¡Bienvenido a nuestro sitio web!"
# Compile translations for runtime
django-admin compilemessages
# Localize date formatting in templates
{% load l10n %}
{% localize on %}{{ some_date }}{% endlocalize %}
# Enable LocaleMiddleware
'django.middleware.locale.LocaleMiddleware'
Click a language button below to see how Django (simulated via JS) changes the text translation and date/currency formats.