Skip to main content

LocoTOML is a lightweight Python library for multi-language support, using hierarchical TOML-based translations with fallback and detailed logging for missing keys.

Project description

LocoTOML

LocoTOML is a brand new library for multilingual support in Python programs, designed to be the lightest possible. It uses only the standard library and has no external dependencies.

However, LocoTOML depends on a core library called LocoCore. LocoCore provides common multilingual support features, and LocoTOML adds TOML-based translation functionality on top of it.

Other Language LEADME (GitHub) Japanese

Features

  • Easy Calls:

    You can easily call translations by writing loc.key1.key2.key3(), improving code readability. For more details, please refer to the documentation.

  • Flexible Calls:

    If you want to output in English just this time, you can temporarily change the language by writing loc.key1("en").

  • Easy-to-understand TOML Translations:

    Translations are in TOML format, making it easy to load files corresponding to each language.

  • Hierarchical Translation Structure:

    You can handle multiple keys hierarchically, such as key1.key2.key3, making it comfortable to use even when the data grows.

  • Detailed Log Output:

    When translation keys are incomplete or errors occur, detailed logs including the file name and line number of the relevant part are output, making troubleshooting easy.

  • Fallback:

    If a translation corresponding to the language is not found, it automatically falls back to the default language set. If there is no translation at all, the translation ID is output as is, clearly notifying the developer.

Installation

Install from PyPI

pip install locotoml

Alternatively, you can install it from the GitHub repository:

python -m pip install git+https://github.com/drago-suzuki58/LocoTOML

Sample Code

Basic Usage

main.py

from locotoml import LocoTOML

# Set the default language to Japanese and the fallback language to English
loc = LocoTOML(locale="ja", fallback_locale="en", locale_dir="loc")

# こんにちは
print(loc.greeting.hello())

# こんにちは!太郎さん
print(loc.greeting.hello_to_user(user="太郎"))

Temporarily Specify Language for Translation

main.py

# Hello
print(loc.greeting.hello("en"))

# Hello! John
print(loc.greeting.hello_to_user("en", user="John"))

Fallback Example t notifies the relevant line and file name where it was called, supporting development.

main.py

# Log: 2024-11-17 20:23:03 | WARNING    | main.py:18 - Missing translation: greeting.hello in: fr, return key name
# Hello
print(loc.greeting.hello("fr")) # Non-existent translation language

# Log: 2024-11-17 20:23:03 | WARNING    | main.py:23 - Missing translation: greeting.goodbye in: ja, falling back to en
# Log: 2024-11-17 20:23:03 | WARNING    | main.py:25 - Missing translation: greeting.goodbye in: en, return key name
# greeting.goodbye
print(loc.greeting.goodbye())

Unused Arguments Example It notifies the relevant line and file name in the log, similar to fallback.

main.py

# Log: 2024-11-17 20:23:03 | WARNING    | main.py:29 - Unused keys: {'message': 'hogehoge'}
# こんにちは
print(loc.greeting.hello(message="hogehoge"))

Translation Files Used in main.py

loc/ja.toml

sample = "サンプル"

[greeting]
hello = "こんにちは"
hello_to_user = "こんにちは!{user}さん"

loc/en.toml

sample = "Sample"

[greeting]
hello = "Hello"
hello_to_user = "Hello! {user}"

In addition to the above sample code, practical examples are available in the example folder. Please take a look.

Q&A

What language codes should I use for translations?

Basically, you are free to use any language code as long as it matches the TOML file name. (Even UwU.toml will be recognized correctly!) However, for readability, we recommend using standard formats such as ISO 639-1 (en, etc.) or ISO-3166 (US).

Are default and fallback languages required?

The default language is required, but the fallback language is not. If not set, en will be automatically set as the fallback language.

Can I use formats other than TOML (e.g., YAML)?

No, currently only TOML is supported. If there is high demand, we may develop a separate library like LocoYAML.

Contribution

LocoTOML is an open-source project! Bug reports and feature suggestions are welcome.

Update Info

Click to show update information

v0.2.0

  • Initial release

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

locotoml-0.2.0.tar.gz (3.4 kB view details)

Uploaded Source

Built Distribution

locotoml-0.2.0-py3-none-any.whl (3.9 kB view details)

Uploaded Python 3

File details

Details for the file locotoml-0.2.0.tar.gz.

File metadata

  • Download URL: locotoml-0.2.0.tar.gz
  • Upload date:
  • Size: 3.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.11.9 Windows/10

File hashes

Hashes for locotoml-0.2.0.tar.gz
Algorithm Hash digest
SHA256 4420d1e8b9359a034252e777fac6ef17d3cab67e76b173cc2cbbb4fee474b8c6
MD5 50292d2f237539213c51ce68b4945fc9
BLAKE2b-256 c29237404798016e48b676102d7317d2ae7b97c2c8f38657e99c5df54630c959

See more details on using hashes here.

File details

Details for the file locotoml-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: locotoml-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 3.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.11.9 Windows/10

File hashes

Hashes for locotoml-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 1eacc53f3a3b62971014471007c83696cd59ab3d2d03de9f2460574da7fa1c8c
MD5 5c27b680ca45ae5333c90e3e72bc3c43
BLAKE2b-256 61becbab8f9b3366868c5491f1c83d1ec2f108010d04a9b04131e3897825fe88

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page