Skip to main content

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 like openpyxl 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 calling str(tm("title")) will not work.
  • If you want to use the % character in a string, you'll need to escape it with itself %%

Metadata

Release files for django-string-externalization 0.31

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-string-externalization 0.31
File Size Uploaded Provenance Actions
django_string_externalization-0.31.tar.gz 5.1 kB Without provenance Details for django_string_externalization-0.31.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for django-string-externalization 0.31
File Interpreter ABI Platform Actions
django_string_externalization-0.31-py3-none-any.whl Python 3 none any Details for django_string_externalization-0.31-py3-none-any.whl

Total release size: 11.6 kB

Release files / django_string_externalization-0.31.tar.gz

Download URL django_string_externalization-0.31.tar.gz
Size 5.1 kB
Tags Source
SHA-256 checksum
How to use checksums
6290519c7b7dad771ef05b8021065cb98254baec69f74c3574ffcb0bff74c376
BLAKE2b-256 checksum
How to use checksums
1ec895bbaa34c0f3a11c595b1e63f0e74c987c1b03b0c3092d14050976638d87
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.16

Release files / django_string_externalization-0.31-py3-none-any.whl

Download URL django_string_externalization-0.31-py3-none-any.whl
Size 6.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e82197d94adaeaffad0865543c0b2f47bf19105eda68314972c91c98de5e18f0
BLAKE2b-256 checksum
How to use checksums
4131c75cd8c0b893b1d8a5a56e4813f7ed640e1ded66eefcb1ee8df617a18166
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.16

Security for django-string-externalization 0.31

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page