Skip to main content

Internationalization helpers on top of stdlib gettext: locale negotiation, plural rules, RTL, lazy strings.

Project description

   ┌──────────────────────────────────────────────────┐
   │  c o d e c h u — i 1 8 n                         │
   │   🌐  Hello   Merhaba   مرحبا   你好   שלום       │
   │  ····translate · negotiate · pluralize · RTL···· │
   └──────────────────────────────────────────────────┘

Internationalization helpers on top of stdlib gettext.

codechu-i18n

Locale negotiation (RFC 4647), CLDR-derived plural rules, RTL detection, ICU-style message formatting, and a lazy Translator that defers .mo loading until first use. Pure stdlib. Python 3.10+.

pip install codechu-i18n

What it gives you

  • Translator — lazy gettext wrapper with gettext, ngettext, pgettext, npgettext, and ICU-style format("Hello {name}!", name=…).
  • negotiate_locale(requested, available) — RFC 4647 lookup match.
  • detect_locale(env)LC_ALL > LC_MESSAGES > LANG > LANGUAGE, caller passes the env dict (no implicit os.environ).
  • is_rtl(locale) — Arabic, Hebrew, Persian, Urdu, Pashto, …
  • plural_form(locale, n) — CLDR plural category index (handles Slavic three-form, Polish, Arabic six-form, Welsh, French zero-as-one, Asian no-plural).
  • lazy_gettext(msg) — module-level strings that resolve at render time, not import time.

Quick examples

Translator

from codechu_i18n import Translator

t = Translator("myapp", "locale", languages=["tr"], fallback="en")

t.gettext("Hello")                            # "Merhaba"
t.ngettext("{n} file", "{n} files", 3)        # "3 dosya"
t.pgettext("menu", "Open")                    # context-disambiguated
t.format("Hello {name}!", name="Ada")         # "Merhaba Ada!"

Locale negotiation

from codechu_i18n import negotiate_locale

available = ["tr-TR", "en-US", "de-DE"]
negotiate_locale(["fr", "tr"], available)        # "tr-TR"
negotiate_locale(["zh-Hant-HK"], ["zh-Hant"])    # "zh-Hant"
negotiate_locale(["xx"], available, fallback="en")  # "en"

Detect from env

import os
from codechu_i18n import detect_locale

detect_locale(dict(os.environ))   # "tr-TR" on a Turkish machine
detect_locale({})                 # ""  (no implicit ambient read)

RTL and plural rules

from codechu_i18n import is_rtl, plural_form

is_rtl("ar")          # True
is_rtl("tr-TR")       # False

plural_form("en", 1)  # 0 (one)
plural_form("en", 2)  # 1 (other)
plural_form("pl", 3)  # 1 (few)
plural_form("pl", 5)  # 2 (many)
plural_form("ar", 0)  # 0 (zero)
plural_form("tr", 5)  # 0 (Turkish has no plural distinction)

Lazy strings

from codechu_i18n import lazy_gettext as _

# Resolved every time str() is called — locale switches take effect.
GREETING = _("Hello")
print(str(GREETING))   # uses current installed translator

Library policy

This package is a thin layer over gettext. It does not ship .po/.mo catalogs — that is the consuming application's job. See the Codechu standards on library i18n for the rationale.

License

MIT — see LICENSE.

Part of Codechu.

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

codechu_i18n-0.1.0.tar.gz (14.6 kB view details)

Uploaded Source

Built Distribution

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

codechu_i18n-0.1.0-py3-none-any.whl (11.7 kB view details)

Uploaded Python 3

File details

Details for the file codechu_i18n-0.1.0.tar.gz.

File metadata

  • Download URL: codechu_i18n-0.1.0.tar.gz
  • Upload date:
  • Size: 14.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for codechu_i18n-0.1.0.tar.gz
Algorithm Hash digest
SHA256 d7d0ee05790be5826ce1e9944d8e5b3a5418dcf831eae89dd3fc59d80ae13aeb
MD5 0218375d4682f9045216d38b5c113272
BLAKE2b-256 97838c5ff60f134b012e21d0055bea113505921d1027a9ff0386574b4bef6c6e

See more details on using hashes here.

Provenance

The following attestation bundles were made for codechu_i18n-0.1.0.tar.gz:

Publisher: release.yml on codechu/i18n-py

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file codechu_i18n-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: codechu_i18n-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 11.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for codechu_i18n-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6baac23bdb964897cdf3b694f500167fc2bd9c2a8426a3919cf92eeb7c7b95a8
MD5 f2204fdbd75d15c131374525930eed06
BLAKE2b-256 2568a82d4208ed00fe87e6b41f94c61e0e9fc9af68b07c99582ee444d1dce931

See more details on using hashes here.

Provenance

The following attestation bundles were made for codechu_i18n-0.1.0-py3-none-any.whl:

Publisher: release.yml on codechu/i18n-py

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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