Skip to main content

Tiny gettext wrapper with sprintf-style helpers (l, ln, lp, ld, …) — same API as the Perl and JavaScript siblings, so one .po file feeds all three runtimes.

Project description

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 in three runtimes — Perl, Python and JavaScript — 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 Perl and JavaScript. No mental context switch when jumping between server, CLI tool and frontend.
  • One .po file for everything. Scrape strings from Perl/Python/JS sources with the same scraper, ship one translation bundle.
  • sprintf baked in. Both classic (%s) and positional (%1$s) placeholders — exactly like Perl's sprintf and gettext's xgettext format strings expect.
  • 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

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
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
l_lang(code) Set primary language (de_DE, pt_BR, …)
ltd(domain) Default text domain
l_dry(path) Append every encountered string to a .po-ish file (scrape mode)
l_nolocales(bool) Skip lookup, return the formatted msgid (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

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

License

MIT © Torsten Raudssus

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

locale_simple-0.106.tar.gz (4.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

locale_simple-0.106-py3-none-any.whl (4.5 kB view details)

Uploaded Python 3

File details

Details for the file locale_simple-0.106.tar.gz.

File metadata

  • Download URL: locale_simple-0.106.tar.gz
  • Upload date:
  • Size: 4.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.11.2

File hashes

Hashes for locale_simple-0.106.tar.gz
Algorithm Hash digest
SHA256 5b0715d3e931b1fb9c9e8b0c8707141868e9e87904d07d5d983e601c13e1d313
MD5 d14f8c17fc67a7ffa00924a5a522d0ce
BLAKE2b-256 3da53a86d9752d174369d28197d48580770a3820122e0bd1af8c9e7bdfe2c2ce

See more details on using hashes here.

File details

Details for the file locale_simple-0.106-py3-none-any.whl.

File metadata

  • Download URL: locale_simple-0.106-py3-none-any.whl
  • Upload date:
  • Size: 4.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.11.2

File hashes

Hashes for locale_simple-0.106-py3-none-any.whl
Algorithm Hash digest
SHA256 006145655b615c8e29587eb5c5f27f6ccfa8d59640672359c70941a9c39ddc24
MD5 c8a6c94ca29f3a9113d710801b4792a5
BLAKE2b-256 9766aad56476a0a2720c357078c6a62311c15057b6980f0835a80e4543ba1308

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page