klocmod -- Kozalo's Localization Module
Screw you, gettext! I don't wanna bother of compiling strings into binary files!
This module provides a very simple, suboptimal way for localizing your scripts, bots or applications. The advantage is its simplicity: to supply some sets of different string literals for different languages, you just need a simple JSON, YAML or INI file (or even a dict) fed to the library. After that, the only thing you should take care of is to get an instance of the dictionary for a specific language and extract messages from it by key values.
All you mostly want is the LocalizationsContainer class. In particular, its static method
LocalizationsContainer.from_file() that reads a localization file and returns an instance of the factory. The factory
is supposed to produce instances of the LanguageDictionary class. Most likely, you will encounter instances of its
subclass -- the SpecificLanguageDictionary class (the base class is only used as a fallback that returns passed key
values back).
Installation
# basic installation
pip install klocmod
# or with YAML files support enabled
pip install klocmod[YAML]
Examples of localization files
JSON (language first)
{
"en": {
"yes": "yes",
"no": "no"
},
"ru-RU": {
"yes": "да",
"no": "нет"
}
}
JSON (phrase first)
{
"yes": {
"en": "yes",
"ru-RU": "да"
},
"no": {
"en": "no",
"ru-RU": "нет"
}
}
INI
[DEFAULT]
yes = yes
no = no
[ru-RU]
yes = да
no = нет
YAML
Requires an extra dependency: PyYAML.
# language first
en:
yes: yes
no: no
ru-RU:
yes: да
no: нет
---
# phrase first
yes:
en: yes
ru-RU: да
no:
en: no
ru-RU: нет
Code example
from klocmod import LocalizationsContainer
localizations = LocalizationsContainer.from_file("localization.json")
ru = localizations.get_lang("ru")
# or
en = localizations.get_lang() # get default language
# then
print(ru['yes']) # output: да
# alternative ways to get a specific phrase:
localizations.get_phrase("ru-RU", "no")
localizations['ru-RU']['no']
Metadata
Release files for klocmod 0.3.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| klocmod-0.3.1.tar.gz | 6.7 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| klocmod-0.3.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 16.6 kB
Release files / klocmod-0.3.1.tar.gz
| Download URL | klocmod-0.3.1.tar.gz |
|---|---|
| Size | 6.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
cc1889ae01ef8944e6bade472603103aec012ca3dae2ed4e8848eb7debf8e35e
|
|
BLAKE2b-256 checksum How to use checksums |
bf50521f7caffc5b708ecea861f8ee92631bfb25a841ebd329f23b6749268221
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.1 CPython/3.11.3
|
Release files / klocmod-0.3.1-py3-none-any.whl
| Download URL | klocmod-0.3.1-py3-none-any.whl |
|---|---|
| Size | 9.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
8a469534c55e87843ce1a04884919244c6c0a47593b14412a53e9c5de843bbb0
|
|
BLAKE2b-256 checksum How to use checksums |
9f58f9ff113fdc18acf9734525f5c80e348d859052f5b3d7a5cdce99034207f9
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.1 CPython/3.11.3
|