Hetman Kit Localize provides thread-safe locale management using context variables. It enables concurrent execution with different locale settings per context.
Project description
Hetman Kit Localize provides thread-safe locale management using context variables. It enables concurrent execution with different locale settings per context.
Installation
pip install hetman-kit-localize
Why use this?
Managing locale settings in concurrent applications can be challenging. Hetman Kit Localize allows you to:
- Thread-Safe Locale Management: Use context variables to ensure each execution context has its own locale.
- Simple API: Easy-to-use class methods for getting and setting locales.
- Base Locale Support: Define a default locale that serves as a fallback.
- 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 hetman_kit_localize import Localize
# Set the base (default) locale
Localize.set_base_locale("en")
Getting and Setting Locales
from hetman_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 hetman_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 hetman_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
contextvarsfor 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 Hetman 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 hetman_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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file hetman_kit_localize-1.0.0.tar.gz.
File metadata
- Download URL: hetman_kit_localize-1.0.0.tar.gz
- Upload date:
- Size: 7.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d60bf192ea43c8051e4fdcc7b4338c46a942990781f111a35fb8b515304892bd
|
|
| MD5 |
649709efe686c9135710a2789dff0f95
|
|
| BLAKE2b-256 |
a5060f74a8dec3e691525b9361db080df48888c98d87a1345bf9e143e58f6d72
|
Provenance
The following attestation bundles were made for hetman_kit_localize-1.0.0.tar.gz:
Publisher:
python-publish.yml on hetman-app/hetman-kit-localize
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
hetman_kit_localize-1.0.0.tar.gz -
Subject digest:
d60bf192ea43c8051e4fdcc7b4338c46a942990781f111a35fb8b515304892bd - Sigstore transparency entry: 835754334
- Sigstore integration time:
-
Permalink:
hetman-app/hetman-kit-localize@4db7ad98e85ae4d9f1a5e9a8ac999f5c3b730a6c -
Branch / Tag:
refs/tags/v1.0.0 - Owner: https://github.com/hetman-app
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@4db7ad98e85ae4d9f1a5e9a8ac999f5c3b730a6c -
Trigger Event:
release
-
Statement type:
File details
Details for the file hetman_kit_localize-1.0.0-py3-none-any.whl.
File metadata
- Download URL: hetman_kit_localize-1.0.0-py3-none-any.whl
- Upload date:
- Size: 8.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ce89936eefa7c934b50b01ee88898a1e5098027526bb3a59a95c57713ed6b109
|
|
| MD5 |
416cc1c6347f328149dc1debfe6cd6b4
|
|
| BLAKE2b-256 |
afbb19a6bfa973c884914fab27a1fb982dba1a08d66036f620e5eeec59c1e8e5
|
Provenance
The following attestation bundles were made for hetman_kit_localize-1.0.0-py3-none-any.whl:
Publisher:
python-publish.yml on hetman-app/hetman-kit-localize
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
hetman_kit_localize-1.0.0-py3-none-any.whl -
Subject digest:
ce89936eefa7c934b50b01ee88898a1e5098027526bb3a59a95c57713ed6b109 - Sigstore transparency entry: 835754335
- Sigstore integration time:
-
Permalink:
hetman-app/hetman-kit-localize@4db7ad98e85ae4d9f1a5e9a8ac999f5c3b730a6c -
Branch / Tag:
refs/tags/v1.0.0 - Owner: https://github.com/hetman-app
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@4db7ad98e85ae4d9f1a5e9a8ac999f5c3b730a6c -
Trigger Event:
release
-
Statement type: