Metadata-Version: 2.1
Name: klocmod
Version: 0.3.1
Summary: A simple module providing facilities to localize small programs via textual files.
Home-page: https://kozalo.ru/#post-1541257200
License: MIT
Keywords: localization library
Author: Leonid Kozarin
Author-email: kozalo@sadbot.ru
Requires-Python: >=3.8,<4.0
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Natural Language :: English
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.8
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3
Classifier: Topic :: Software Development :: Internationalization
Classifier: Topic :: Software Development :: Libraries
Provides-Extra: yaml
Requires-Dist: pyyaml (>=6.0,<7.0) ; extra == "yaml"
Project-URL: Documentation, https://klocmod.readthedocs.io/en/latest/
Project-URL: Repository, https://github.com/kozalosev/klocmod
Description-Content-Type: text/markdown

klocmod -- Kozalo's Localization Module
======================================

*Screw you, gettext! I don't wanna bother of compiling strings into binary files!*

[![Build Status](https://github.com/kozalosev/klocmod/actions/workflows/ci-build.yml/badge.svg)](https://github.com/kozalosev/klocmod/actions/workflows/ci-build.yml)
[![Documentation Status](https://readthedocs.org/projects/klocmod/badge/?version=latest)](https://klocmod.readthedocs.io/en/latest/?badge=latest)

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
------------

```bash
# basic installation
pip install klocmod
# or with YAML files support enabled
pip install klocmod[YAML]
```


Examples of localization files
------------------------------

### JSON (language first)

```json
{
  "en": {
    "yes": "yes",
    "no": "no"
  },
  "ru-RU": {
    "yes": "да",
    "no": "нет"
  }
}
```

### JSON (phrase first)

```json
{
  "yes": {
    "en": "yes",
    "ru-RU": "да"
  },
  "no": {
    "en": "no",
    "ru-RU": "нет"
  }
}
```

### INI

```ini
[DEFAULT]
yes = yes
no = no

[ru-RU]
yes = да
no = нет
```

### YAML

Requires an extra dependency: *PyYAML*.

```yaml
# 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
------------

```python
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']
```

