Skip to main content

PumpWood I8n

This package provides internationalization for Pumpwood systems. It can translate sentences through a remote microservice backend or a local model class that implements a translate method.


Pumpwood is a native Brazilian tree which has a symbiotic relation with ants (Murabei)

Quick start

The main class in the package is PumpwoodI8n. It translates sentences through the t method.

Remote backend (microservice)

from pumpwood_i8n.translate import PumpwoodI8n
from pumpwood_communication.microservices import PumpWoodMicroService


# Instantiate a microservice object to call the backend for translation.
microservice = PumpWoodMicroService(
    server_url='http://localhost/',
    username="pumpwood", password="is a nice system")

pumpwood_i8n = PumpwoodI8n(microservice=microservice)
translated_str = pumpwood_i8n.t(
    sentence="Translate this sequence please?",
    # Tags differentiate the same sentence in different contexts.
    tag="login",
    # Flag to indicate whether the translation is plural.
    plural=False,
    # Reference language for the translation.
    language="",
    # Optional user context for differentiated translations.
    user_type="")

It is also possible to instantiate an empty object and configure it later.

from pumpwood_i8n.translate import PumpwoodI8n
from pumpwood_communication.microservices import PumpWoodMicroService


microservice = PumpWoodMicroService(
    server_url='http://localhost/',
    username="pumpwood", password="is a nice system")

pumpwood_i8n = PumpwoodI8n()
pumpwood_i8n.init(microservice=microservice)

Local backend (Django model)

When running inside a Django application, configure a local model instead of a microservice. Only one backend may be active at a time.

from pumpwood_i8n.translate import PumpwoodI8n


def django_apps_ready():
    try:
        from django.apps import apps
        return apps.ready
    except Exception:
        return False


pumpwood_i8n = PumpwoodI8n(
    i8n_model='pumpwood_djangoauth.i8n.models.PumpwoodI8nTranslation',
    app_ready_check=django_apps_ready)
translated_str = pumpwood_i8n.t(
    sentence="Translate this sequence please?", tag="login")

The model path can also be set through the PUMPWOOD_I8N__I8N_MODEL environment variable. Pass app_ready_check when using a Django model so translation is skipped until django.apps.apps.ready is True.

Singleton

Use the shared instance from singletons and initialize it at startup.

from pumpwood_i8n.singletons import pumpwood_i8n
from pumpwood_communication.microservices import PumpWoodMicroService


microservice = PumpWoodMicroService(
    server_url='http://localhost/',
    username="pumpwood", password="is a nice system")

pumpwood_i8n.init(microservice=microservice)

Cache

Pumpwood I8n can cache translations locally to avoid too many calls to the backend. Cache entries expire after PUMPWOOD_AUTH__I8N_CACHE_EXPIRATION seconds.

Environment parameters

PUMPWOOD_AUTH__I8N_CACHE_EXPIRATION [int]: Expiry time in seconds for locally cached translations. After this period the sentence is translated again and the local cache is renewed. Default is 300.

PUMPWOOD_I8N__I8N_MODEL [str]: Dotted path to the model class used for local translation (lazy load). Example: pumpwood_djangoauth.i8n.models.PumpwoodI8nTranslation.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pumpwood_i8n-1.0.6.tar.gz (7.6 kB view details)

Uploaded Source

Built Distribution

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

pumpwood_i8n-1.0.6-py3-none-any.whl (9.1 kB view details)

Uploaded Python 3

File details

Details for the file pumpwood_i8n-1.0.6.tar.gz.

File metadata

  • Download URL: pumpwood_i8n-1.0.6.tar.gz
  • Upload date:
  • Size: 7.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.4.1 CPython/3.12.13 Linux/6.17.0-1020-azure

File hashes

Hashes for pumpwood_i8n-1.0.6.tar.gz
Algorithm Hash digest
SHA256 6ddb7ff54f87814c28a869d84773576520ff01d4f6cddae260c469141056c1c9
MD5 47a1c5257baff16ded532410b7f6a506
BLAKE2b-256 d5ae1cdb0f729c67620d8959c78e4ee9b2329586026bc9ddef5f002960a3f999

See more details on using hashes here.

File details

Details for the file pumpwood_i8n-1.0.6-py3-none-any.whl.

File metadata

  • Download URL: pumpwood_i8n-1.0.6-py3-none-any.whl
  • Upload date:
  • Size: 9.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.4.1 CPython/3.12.13 Linux/6.17.0-1020-azure

File hashes

Hashes for pumpwood_i8n-1.0.6-py3-none-any.whl
Algorithm Hash digest
SHA256 381a156475b15a7e00a8e465ba3b0db1c56aa137162d91a3f0df50fdaa91ce25
MD5 dfe1366e90b969decfb3bfd873faed1b
BLAKE2b-256 be391536b951c91cc06e17490d1ec37db329d279cb7c716eb52c21e190eee82e

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