LangPack — key-based localization (translation) framework for Python applications. Out of the box supports Django and storing translations in YAML files ;)
Add LangPack into your INSTALLED_APPS and configure file loaders. Note you should be install pyyaml package before using YAML for translations.
INSTALLED_APPS = [
'langpack.contrib.django',
]
LANGPACK_LOADERS = [
('langpack.loaders.YamlLoader', ['yaml', 'yml']),
]
Create your first translation file project/locales/mainpage.yaml with the following content:
en:
welcome_back: "Welcome back, {name}!"
And use it in your template file:
{% load trans from langpack %}
<h1>{% trans "mainpage.welcome" name=request.user.first_name %}</h1>
You can also use translations in python files:
from langpack.contrib.django import trans_lazy
from django.shortcuts import render
def welcome_page(request):
text = trans_lazy('mainpage.welcome_back', name=request.user.first_name)
return render('mainpage.html', {'welcome_text': text})
LangPack is fully integrated with Django localization system. All tools, such as LocaleMiddleware
or i18n_patterns
works without problems. We just take current locale form builtin Django method get_language()
.