Skip to main content

Krywok Kit Localize provides thread-safe locale management using context variables. It enables concurrent execution with different locale settings per context.

Project description

Krywok Logo

Krywok Kit Localize provides thread-safe locale management using context variables. It enables concurrent execution with different locale settings per context.

Installation

pip install krywok-kit-localize

Why use this?

Managing locale settings in concurrent applications can be challenging. Krywok Kit Localize allows you to:

  1. Thread-Safe Locale Management: Use context variables to ensure each execution context has its own locale.
  2. Simple API: Easy-to-use class methods for getting and setting locales.
  3. Base Locale Support: Define a default locale that serves as a fallback.
  4. Concurrent Execution: Perfect for async applications where different requests need different locales.

Usage Example

Setting Up Locales

Set the base locale once during application initialization:

from krywok_kit_localize import Localize

# Set the base (default) locale
Localize.set_base_locale("en")

Getting and Setting Locales

from krywok_kit_localize import Localize

# Set up the base locale first
Localize.set_base_locale("en")

# Get the current locale (returns base locale if not set)
print(Localize.get_locale())  # Output: en

# Set a different locale for the current context
Localize.set_locale("pl")
print(Localize.get_locale())  # Output: pl

# Get the base locale
print(Localize.get_base_locale())  # Output: en

Context-Aware Example

The locale management is context-aware, making it perfect for web applications where each request might need a different locale:

from krywok_kit_localize import Localize
import asyncio


async def handle_request(locale: str):
    # Each request can have its own locale
    Localize.set_locale(locale)

    # This will return the locale for this specific context
    current = Localize.get_locale()
    print(f"Handling request with locale: {current}")


# Set the base locale
Localize.set_base_locale("en")


async def main():
    await asyncio.gather(
        handle_request("pl"),
        handle_request("en"),
        handle_request("de"),
    )


# Simulate concurrent requests with different locales
asyncio.run(main())

Resolving Translations

The resolve_translation() method helps you retrieve the correct translation from a dictionary based on the current locale:

from krywok_kit_localize import Localize

# Set up locales
Localize.set_base_locale("en")

# Define translations
messages = {
    "en": "Hello, World!",
    "pl": "Witaj, Świecie!",
    "de": "Hallo, Welt!"
}

# Get translation for current locale (falls back to base locale)
Localize.set_locale("pl")
print(Localize.resolve_translation(messages))  # Output: Witaj, Świecie!

Localize.set_locale("de")
print(Localize.resolve_translation(messages))  # Output: Hallo, Welt!

# If locale is not found, falls back to base locale
Localize.set_locale("fr")
print(Localize.resolve_translation(messages))  # Output: Hello, World!

# Use strict=False to return None instead of raising KeyError when neither locale nor base locale exists
partial_messages = {"pl": "Witaj!"}
Localize.set_locale("en")
print(Localize.resolve_translation(partial_messages, strict=False))  # Output: None

Core Features

  • Thread-Safe: Uses Python's contextvars for safe concurrent locale management.
  • Simple API: Just four class methods: set_base_locale(), get_base_locale(), set_locale(), get_locale().
  • Context Variables: Each execution context maintains its own locale state.

Why "Kit"?

This package is called Krywok Kit Localize because it's designed to be a building block ("kit") for your own i18n solutions. Other classes can inherit from Localize to easily add locale management to their functionality:

from krywok_kit_localize import Localize

class MyI18nClass(Localize):
    MESSAGES = {
        "welcome": {
            "en": "Welcome!",
            "pl": "Witaj!",
            "de": "Willkommen!"
        },
        "goodbye": {
            "en": "Goodbye!",
            "pl": "Do widzenia!",
            "de": "Auf Wiedersehen!"
        }
    }

    @classmethod
    def get_message(cls, key: str) -> str:
        # Use resolve_translation to get the right message
        return cls.resolve_translation(cls.MESSAGES[key])

# Inherits all locale management methods
MyI18nClass.set_base_locale("en")
MyI18nClass.set_locale("pl")
print(MyI18nClass.get_locale())  # Output: pl
print(MyI18nClass.get_message("welcome"))  # Output: Witaj!

MyI18nClass.set_locale("de")
print(MyI18nClass.get_message("goodbye"))  # Output: Auf Wiedersehen!

By inheriting from Localize, your classes automatically get thread-safe locale management without any additional setup!

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

krywok_kit_localize-1.0.0.tar.gz (8.0 kB view details)

Uploaded Source

Built Distribution

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

krywok_kit_localize-1.0.0-py3-none-any.whl (8.8 kB view details)

Uploaded Python 3

File details

Details for the file krywok_kit_localize-1.0.0.tar.gz.

File metadata

  • Download URL: krywok_kit_localize-1.0.0.tar.gz
  • Upload date:
  • Size: 8.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for krywok_kit_localize-1.0.0.tar.gz
Algorithm Hash digest
SHA256 0a6b8dbecec84ad52dc636395c28456b332c1ef0faa8ad7a7f8e41b64fbd7ff4
MD5 8fff7435ddb196752baed42cfc920624
BLAKE2b-256 0d3f898d4bd3a2b3e7e499d9560f97f76c889a34f48d782f769b73bc73d7e1a5

See more details on using hashes here.

Provenance

The following attestation bundles were made for krywok_kit_localize-1.0.0.tar.gz:

Publisher: python-publish.yml on Krywok/krywok-kit-localize

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

File details

Details for the file krywok_kit_localize-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for krywok_kit_localize-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 fe2fae0e3cf4627ec769747ea95cdf5f761f8746fd21074f2e688c29c3b05e30
MD5 6e791d1195feffc434f4c8d7877cca8d
BLAKE2b-256 efdd43412d8d7f0e501799f37f507a63e5ba251787d7e03d0ba7d08de42f2a0a

See more details on using hashes here.

Provenance

The following attestation bundles were made for krywok_kit_localize-1.0.0-py3-none-any.whl:

Publisher: python-publish.yml on Krywok/krywok-kit-localize

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