Skip to main content

locale-simple

PyPI version Python versions License: MIT CPAN sibling npm sibling

A tiny, sharp wrapper around gettext with sprintf-style formatting and short, friendly helpers (l, ln, lp, ld, …). UTF‑8 only, by design.

The same API ships for Perl, Python, JavaScript, PHP, Go and Rust — so one set of .po files feeds your whole stack. Translate once, use everywhere.


Why locale-simple?

  • Same API across languages. A function called lnp in Python behaves identically in the other five runtimes. No mental context switch when jumping between server, CLI tool and frontend.
  • One .po file for everything. One scraper (locale-simple scrape) reads the sources of all six languages, so one translation bundle covers the whole stack.
  • sprintf baked in. Both classic (%s) and positional (%1$s) placeholders — exactly like Perl's sprintf and gettext's xgettext format strings expect, %b and the ' pad flag (%'*8s) included. Argument values go in as they are, so a % in them stays, and a placeholder without an argument stays as it is.
  • Boring and small. Single-file module, no surprises, no magic.

Install

pip install locale-simple

Quick start

from locale_simple import l, ln, lp, ld, l_dir, l_lang, ltd

l_dir('data/locale')   # where your .mo files live
ltd('myapp')           # default text domain
l_lang('de_DE')        # primary language

print(l("Hello"))
# → Hallo

print(ln("You have %d message", "You have %d messages", 1))
# → Du hast 1 Nachricht

print(ln("You have %d message", "You have %d messages", 5))
# → Du hast 5 Nachrichten

print(lp("button", "Open"))           # context-disambiguated
print(ld("emails", "Welcome, %s", name))  # explicit domain

Extracting msgids

locale-simple scrape walks your sources (.py next to the other five languages) and writes the .pot template to STDOUT. Get locale-simple as a standalone binary from the GitHub release (no Perl needed) or with cpanm Locale::Simple:

locale-simple scrape --ignore venv > messages.pot

API at a glance

Function gettext equivalent What it does
l(msgid, *args) gettext Translate, then sprintf
ln(msgid, msgid_plural, n, *args) ngettext Plural form for n
lp(ctxt, msgid, *args) pgettext With disambiguating context
lnp(ctxt, msgid, msgid_plural, n, *args) npgettext Plural + context
ld(domain, msgid, *args) dgettext Specific text domain, bound to the l_dir() directory on first use
ldn(domain, msgid, msgid_plural, n, *args) dngettext Domain + plural
ldp(domain, ctxt, msgid, *args) dpgettext Domain + context
ldnp(domain, ctxt, msgid, msgid_plural, n, *args) dnpgettext The full thing

Configuration helpers

Call Purpose
l_dir(path) Locale directory containing <lang>/LC_MESSAGES/<domain>.mo; a later call moves every bound domain to the new directory
l_lang(code) Set primary language (de_DE, pt_BR, …)
ltd(domain) Default text domain
l_dry(path) Dry run: append each call's msgid block (# domain:, msgctxt, msgid, msgid_plural) to path in UTF-8 with LF line ends, the same bytes the Perl module writes, and return the formatted msgid; l_dry(path, True) skips the file, l_dry(None) leaves dry mode
l_nolocales(bool) Lift the "set l_dir() first" check that ltd() and every helper make; without a directory the formatted msgid comes back (handy in tests)

Sibling packages

Runtime Package Repo path
Perl Locale::Simple on CPAN lib/Locale/Simple.pm
Python locale-simple on PyPI python/locale_simple.py
JavaScript locale-simple on npm js/src/index.js
PHP raudssus/locale-simple on Packagist php/src/Simple.php
Go github.com/Getty/locale-simple/go go/
Rust locale-simple on crates.io rust/

All six share the same source tree: https://github.com/Getty/locale-simple

License

MIT © Torsten Raudssus

Metadata

Release files for locale-simple 0.200

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

Source distribution (sdist)

Source distribution for locale-simple 0.200
File Size Uploaded Provenance Actions
locale_simple-0.200.tar.gz 7.9 kB Without provenance Details for locale_simple-0.200.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for locale-simple 0.200
File Interpreter ABI Platform Actions
locale_simple-0.200-py3-none-any.whl Python 3 none any Details for locale_simple-0.200-py3-none-any.whl

Total release size: 15.8 kB

Release files / locale_simple-0.200.tar.gz

Download URL locale_simple-0.200.tar.gz
Size 7.9 kB
Tags Source
SHA-256 checksum
How to use checksums
95bf2aab72577aab2de39ce33d3ef4df1ae6d41a12fb3c1cb68bd2c83b10e729
BLAKE2b-256 checksum
How to use checksums
ac623221dfa152bf3adeed681fa490c321369a79c4ea64577414a85d6ce84ac1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.5

Release files / locale_simple-0.200-py3-none-any.whl

Download URL locale_simple-0.200-py3-none-any.whl
Size 7.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
33deacb2de640aba1a257234151011eeda5b40f1e72efb794d5ad7dae3c005d8
BLAKE2b-256 checksum
How to use checksums
862141af491e42d563a2054f6c753aad65c123b71685349ef7a1bc330795ede7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.5

Security for locale-simple 0.200

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