No project description provided
Project description
django_string_externalization
This is a tool for django apps to externalize their strings into yaml files. Messages for multiple languages are co-located in the same file. This is ideal for bilingual apps.
page_title:
en: "My App"
fr: "Mon App"
You can define translations accross multiple files.
Usage
First, you'll want to create a yaml file with your strings. File names must end with .text.yaml
.
# mytext.text.yaml
page_title:
en: "My App"
fr: "Mon App"
# variables
welcome_message:
en: Welcome %(firstname)s %(lastname)s
fr: Bienvenue %(firstname)s %(lastname)s
the_current_year_is:
en: The current year is %(current_year)s
fr: L'année en cours est %(current_year)s
Then, you'll want to create a TextMakerCreator
object: TextMakerCreator(global_keys, text_files).
Global keys are an optional concept that let you define variables available to all of your text entries.
global_keys = {
"en" : { "current_year" : "2019-20" },
"fr" : { "current_year" : "2019-2020" },
}
tm = TextMakerCreator(global_keys, ["my_app/mytext.text.yaml"]).get_tm_func()
tm("page_title") == "My App"
tm("welcome_message", extra_keys={"firstname":"Alex", "lastname": "Leduc"}) == "Welcome Alex Leduc"
tm("the_current_year_is") == "The current year is 2019-20"
You can create many text-maker functions, with indepedent or overlapping text files. This is useful if you want isolated possibly name-clashing text entries.
Watching
If you're using django's dev-server, you can use WatchingTextMakerCreator
instead, and it will reload text entries when the yaml files change.
If you use the watching feature, you'll need watchdog installed. This library supports version 2
Sanitization and markdown
HTML is sanitized by default, using this whitelist of tags,
ALLOWED_TAGS = ["a", "b", "em", "i", "li", "ol", "p", "strong", "ul", "br", "div"]
You can disable sanitization by passing sanitize_output=False
to the tm
function. Inputs (extra_args
) will still be sanitized, unless you also pass sanitize_input=False
.
You can provide custom sanization and markdown logic by overriding the TextMaker's class render_markdown
and sanitize
methods.
Unless you override these methods, you must install the mistune
(version 2) and bleach
(version 3) packages
Pitfalls and gotchas
tm()
returns lazy objects which resolve and behave like strings, this can trip up some packages likeopenpyxl
when it tries to write these objects to cells. You can force the lazy object to resolve by performing string operations, e.g.tm("title")+""
. Simply callingstr(tm("title"))
will not work.- If you want to use the
%
character in a string, you'll need to escape it with itself%%
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file django_string_externalization-0.31.tar.gz
.
File metadata
- Download URL: django_string_externalization-0.31.tar.gz
- Upload date:
- Size: 5.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.16
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6290519c7b7dad771ef05b8021065cb98254baec69f74c3574ffcb0bff74c376 |
|
MD5 | 3ad45f22ab42d436ef418a783bb7597b |
|
BLAKE2b-256 | 1ec895bbaa34c0f3a11c595b1e63f0e74c987c1b03b0c3092d14050976638d87 |
File details
Details for the file django_string_externalization-0.31-py3-none-any.whl
.
File metadata
- Download URL: django_string_externalization-0.31-py3-none-any.whl
- Upload date:
- Size: 6.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.16
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e82197d94adaeaffad0865543c0b2f47bf19105eda68314972c91c98de5e18f0 |
|
MD5 | 1bdc37319302a8d9393b883c197ac99f |
|
BLAKE2b-256 | 4131c75cd8c0b893b1d8a5a56e4813f7ed640e1ded66eefcb1ee8df617a18166 |