Skip to main content

Siba

Siba (coming from Sibawayh) is a Django translation tool that uses json files instead of gettext (po, mo).

Features:

  • Support for nested keys
  • Reads current locale from Django translation module
  • Simple parameter injection to values
  • Support for caching locales (loading into memory) to improve performance if needed
  • Pluralization: none, one, some, many
  • Multiple prefixes (you can have separate translation files for different contexts)
  • Lazy loading translations

What it doesn't do:

  • Right now there is no support to use this library in Django templates. Rest framework should be supported well.

Requirements

Tested with Python 3.9, but 3.6+ should be supported as well. Django >= 4.2.

Usage

You can create files with format {prefix}.{locale}.json under a directory of your choice so Siba scans them. For example application.en.json should contain en locale data for prefix application (default). You can change default prefix and directory of locales in settings (read below).

Example json content:

{
  "phrase": {
    "welcomeMessage": "Welcome to application, {username}"
  }
}

You can now read this translation using:

from siba.translation import translate, translate_lazy

translate("phrase.welcomeMessage")
translate_lazy("phrase.welcomeMessage")

You can directly pass a locale as well:

from siba.translation import translate, translate_lazy

translate("phrase.welcomeMessage", locale="de")
translate_lazy("phrase.welcomeMessage", locale="de")

Or change the prefix (reads from a different file):

from siba.translation import translate

# looks in messages.de.json file
translate("phrase.welcomeMessage", prefix="messages", locale="de")  

If you don't pass a locale it goes for active translation in Django. So you don't have to worry if you already change active translation in your middlewares.

from siba.translation import translate
from django.utils import translation

translation.activate("de")
translate("phrase.welcomeMessage")  # looks for de locale

And you can inject parameter values

from siba.translation import translate

translate("phrase.welcomeMessage", parameters={"username": "siba"})

Pluralization

Siba supports pluralization. To use it you need at least these two keys defined in your json object for a parent key you want to pluralize: one, many.

Through the settings you can enable support for some, and it supports none by default too.

Example:

{
  "catCounter": {
    "none": "You have no cats! Start to pet one.",
    "one": "You only have a single cat! Get more pets.",
    "some": "Glad to see you have some cats.",
    "many": "I've never seen so many cats bein pets of a single person!"
  }
}

Using key argument p_count you can determine which translation should be used.

from siba.translation import translate

translate("catCounter")  # You only have a single cat! Get more pets.
translate("catCounter", p_count=0)  # You have no cats! Start to pet one.
translate("catCounter", p_count=1)  # You only have a single cat! Get more pets.
translate("catCounter", p_count=3)  # Glad to see you have some cats.

# Dynamical example:
cats = []
translate("catCounter", p_count=len(cats))

Settings

You can change the settings by using code below in your settings.py.

from siba import set_setting


set_setting(key, value)

Setting keys are:

  • error_on_unknown_key: expects (bool), determines if error should rise when a translation key is missing.
  • error_on_missing_locale_file: expects (bool), determines if error should rise when a locale file is missing.
  • key_split_delimiter: expects (str), delimiter for keys.
  • locales_path: expects (str), directory to find locale files in .
  • cache_locales: expects (bool), determines if locale data should be cached (loaded into memory).
  • prefixes: expects (list[str]), list of supported locale file prefixes.
  • default_prefix: expects (str), default prefix (currently set to application).
  • locales: expects (list[str]), list of supported locales in the application.
  • default_locale: expects (str), the default locale of the application (currently set to en).
  • missing_parameter_handler: expects (function(key:str)), callable function to call when a parameter is missing. By default the parameter name is returned.
  • pluralization: expects (dict), with keys below:
    • some_enabled: expects (bool), determines if "some" should be supported
    • some_limit: expects (int), determines value which plural will be known as "some" if count is between this number or 1.

Metadata

Release files for django-boot-siba 1.0.0

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-boot-siba 1.0.0
File Size Uploaded Provenance Actions
django-boot-siba-1.0.0.tar.gz 6.8 kB Without provenance Details for django-boot-siba-1.0.0.tar.gz

Release files / django-boot-siba-1.0.0.tar.gz

Download URL django-boot-siba-1.0.0.tar.gz
Size 6.8 kB
Tags Source
SHA-256 checksum
How to use checksums
8d05ceb90c9d306b1a1e23d35b5758aa15b7c70f22752cf058ea48abef0b6405
BLAKE2b-256 checksum
How to use checksums
e6c0239df23410ea053a03655097fc945da74868c31ce66385ea77339ab99378
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.18

Security for django-boot-siba 1.0.0

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.

Release history Release notifications | RSS feed

This release

1.0.0 This release

1 release file No provenance

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