LocoCore is a library that provides a base class for multilingual applications.
Project description
LocoCore
LocoCore is a library that provides a base class for multilingual applications. By inheriting this class and implementing your own data loading logic, you can flexibly handle translation data from any data source such as JSON files or databases. If you want to use an already implemented subclass, please use LocoJSON or LocoTOML.
このライブラリの使い方
By inheriting the LocoCore base class and implementing the _load_translations method yourself, you can obtain translation data from your own files and achieve flexible implementation.
As long as you can load each translation into self.translations[locale] (where locale is the specified language) in a nested dictionary format (such as Dict[str, Dict[str, Any]]), theoretically any file format is possible.
サンプルコード
Below is the code for the LocoJSON subclass library, which is an extension library.
import json
import os
from lococore import LocoCore
class LocoJSON(LocoCore):
def _load_translations(self, locale: str) -> None:
if locale in self.cache:
self.translations[locale] = self.cache[locale]
return
locale_file = os.path.join(self.locale_dir, f"{locale}.json")
if os.path.exists(locale_file):
try:
with open(locale_file, "r", encoding="utf-8") as f:
self.translations[locale] = json.load(f)
self.cache[locale] = self.translations[locale]
self.logger.info(f"Loaded JSON file for locale {locale}")
except json.JSONDecodeError as e:
self.logger.error(f"Failed to load JSON file for locale {locale}: {e}")
-
Checking the Cache
If the data is already loaded in self.cache, it avoids reloading and uses it directly.
-
Loading JSON Files
It loads the file corresponding to the specified language from self.locale_dir and registers it as nested dictionary translation data.
-
Error Handling
It logs errors if the file format is invalid or if loading fails.
You do not need to implement all of these features, but having these features will make it more user-friendly.
Also, to actually use this class, you can do the following:
loc = LocoJSON("ja")
python(loc.example1())
By applying this, you can easily implement very flexible translation functionality using LocoCore.
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 lococore-0.2.0.tar.gz.
File metadata
- Download URL: lococore-0.2.0.tar.gz
- Upload date:
- Size: 4.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.3 CPython/3.11.9 Windows/10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9987f54883806b064ed26e61cc7d91f909b5fdb43a39ec6884454aa12a19fb3e
|
|
| MD5 |
25d50efa0d9b1da656d87cc5f9f5d234
|
|
| BLAKE2b-256 |
095a5110ce6fb849b7689ef8f4b0d42fd273165d32a4a94f46d50177b71a4e32
|
File details
Details for the file lococore-0.2.0-py3-none-any.whl.
File metadata
- Download URL: lococore-0.2.0-py3-none-any.whl
- Upload date:
- Size: 5.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.3 CPython/3.11.9 Windows/10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
57749cf3f6430619f721ae847adcb1ef4297c3635f28e1eb9e34317a5c4a147e
|
|
| MD5 |
b182698a122b810b7d2125250434f03c
|
|
| BLAKE2b-256 |
65909b228053ede11bab5cc0a942e5e41630c5e31a94123cfbe1a22ed6fccf67
|