Skip to main content

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

Project description

LocoJSON

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

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 JSON Translations:

    Translations are in JSON 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 locojson

Alternatively, you can install it from the GitHub repository:

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

Sample Code

Basic Usage

main.py

from locojson import LocoJSON

# Set the default language to Japanese and the fallback language to English
loc = LocoJSON(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.json

{
    "greeting" : {
        "hello" : "こんにちは",
        "hello_to_user" : "こんにちは!{user}さん"
    },
    "sample" : "サンプル"
}

loc/en.json

{
    "greeting" : {
        "hello" : "Hello",
        "hello_to_user" : "Hello! {user}"
    },
    "sample" : "Sample"
}

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 JSON file name. (Even UwU.json 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 JSON (e.g., YAML)?

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

Contribution

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

Update Info

Click to show update information

v0.1.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

locojson-0.1.0.tar.gz (5.1 kB view details)

Uploaded Source

Built Distribution

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

locojson-0.1.0-py3-none-any.whl (5.7 kB view details)

Uploaded Python 3

File details

Details for the file locojson-0.1.0.tar.gz.

File metadata

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

File hashes

Hashes for locojson-0.1.0.tar.gz
Algorithm Hash digest
SHA256 d4f74b66a4aeeee303cdfc79027d25d5b950364f55902d9220080fd62aed0cae
MD5 f4b05e12d22acdd0489decd884fe6ee9
BLAKE2b-256 475cbf24e290d888376692066e2a3c4bfaf639f695d2d935fe90adb7e009a184

See more details on using hashes here.

File details

Details for the file locojson-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: locojson-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 5.7 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 locojson-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3b6eb51a85862cc50d0f3b0cf57ac64808e75e38caeba86109e02992ffa275a5
MD5 0d683f5511d5beceb9566771cd01c3dd
BLAKE2b-256 489af5685487cae6fffd15077d821a468c4142d47eb5b63c116a1f7d30b53233

See more details on using hashes here.

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