Skip to content

Organize your texts and languages in a simple way with short keys.

License

Notifications You must be signed in to change notification settings

audreyteles/django-lexpy

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Django Lexpy - v0.1.2-beta

PyPI PyPI - Downloads

Published on Django Packages

This is a package that helps you organize your texts in several languages in a simple way.

Install

pip install django-lexpy

Configure

settings.py

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

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' %}

Organazing

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?",
}

Internationalization and localization

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.",
}

Replacing Fields in Messages

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" %}
...

About

Organize your texts and languages in a simple way with short keys.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages