This is a package that helps you organize your texts in several languages in a simple way.
pip install django-lexpy
INSTALLED_APPS = [
'django_lexpy'
]
Check that the desired language is correct:
LANGUAGE_CODE = 'en-us'
Run your project:
python manage.py runserver
So a folder with your desired language was created:
lang
└── en_us
├── __init__.py
└── main.py
The main.py
looks like:
messages = {
"message.test": "This is a test message.",
}
Load lexpy in your template and use the tag:
{% load lexpy %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
{% lexpy 'message.test' %}
</body>
</html>
The tag consists of the prefix “message.” followed by the short key of the message that has been defined in your message file:
{% lexpy 'message.welcome' %}
you no longer have to go through your entire project to find your “messages”/texts, centralize them in one place:
# yourproject/lang/en_us/main.py
messages = {
"message.test": "This is a test message.",
"message.welcome": "Welcome to my website.",
"message.help": "Do you need some help?",
}
In addition to organizing your texts, you can simplify the translations without 'strings as keys' in .po
file:
# path/to/python/file.py:123
msgid "Welcome to my site."
msgstr "Bem-vindo ao meu site"
Now you have short keys:
# yourproject/lang/en_us/main.py
messages = {
"message.test": "This is a test message.",
"message.welcome": "Welcome to my website.",
}
Another language:
# yourproject/lang/pt_BR/main.py
messages = {
"message.test": "Isso é uma mensagem de teste.",
"message.welcome": "Bem-vindo ao meu site.",
}
You can also define fields in your message and replace them with dynamic values.
# yourproject/lang/en_us/main.py
messages = {
"message.welcome": "Hi {name}, welcome to my website.",
}
And in your template you can:
{% load lexpy %}
...
{% lexpy 'message.welcome' name="Foobar" %}
...
Then you can see it on your website:
Hi Foobar, welcome to my website.
Unnamed fields are also supported
#yourproject/lang/en_us/main.py messages = { "message.welcome": "Hi {}, welcome to my website.", }{% load lexpy %} ... {% lexpy 'message.welcome' "Foobar" %} ...