A flexible XML/JSON language loader
Project description
Accetra - Multilingual Language Loader
Accetra is a lightweight and efficient language loader that supports loading translations from XML and JSON files. It allows for automatic fallback, variable replacements, and has an organized structure for easy extensibility. This library is ideal for applications that need to support multiple languages.
🚀 Features
- Multi-format Support: Load translations from XML and JSON files.
- Fallback Mechanism: Automatically fall back to another language if a key is missing.
- Variable Replacement: Replace variables in strings (e.g.,
{username}becomesAlice). - Metadata Extraction: Retrieve metadata like language code, authors, and version.
- Extensibility: Easily add support for more formats or customization.
- Clear Structure: Organize translations into sections (e.g., windows, errors, etc.).
- CLI: Create and Validate language files.
🧑💻 Installation
1. Install via PyPI (Recommended)
You can install Accetra directly from PyPI:
pip install accetra
2. Install from Source
If you want to contribute or modify the library, you can install it from the source in editable mode:
git clone https://github.com/yourusername/accetra.git
cd accetra
pip install -e .
🎯 Usage
CLI
Validate File:
accetra validate <path>
Create template:
accetra template <path (.)> <json/xml>
Accetra supports loading languages from both XML and JSON formats. Below are examples of how to use both formats.
XML Example
<language>
<meta>
<code>en</code>
<name>English</name>
<authors><author>John Doe</author></authors>
<version>1.0</version>
<description>English Language Pack</description>
</meta>
<general>
<entry id="app.welcome">
<text>Welcome, {username}!</text>
</entry>
</general>
</language>
Load and use an XML language file:
from accetra.xml_loader import XmlLanguageLoader
# Initialize loader with primary and fallback languages
lang = XmlLanguageLoader("languages/en.xml", "languages/de.xml")
# Get translated text with variable replacement
text = lang.get_text("general.app.welcome", username="Alice")
print(text) # Output: "Welcome, Alice!"
JSON Example
{
"meta": {
"code": "en",
"name": "English",
"authors": ["John Doe"],
"version": "1.0",
"description": "English Language Pack"
},
"general": {
"app.welcome": {
"text": "Welcome, {username}!"
}
}
}
Load and use a JSON language file:
from accetra.json_loader import JsonLanguageLoader
# Initialize loader with primary and fallback languages
lang = JsonLanguageLoader("languages/en.json", "languages/de.json")
# Get translated text with variable replacement
text = lang.get_text("general.app.welcome", username="Alice")
print(text) # Output: "Welcome, Alice!"
🧑💼 Functions and Methods
Base Class: BaseLanguageLoader
The base class provides the core functionality for loading translations and handling variable replacement.
-
__init__(self, primary_lang_file, fallback_lang_file=None):
Initialize the loader with primary and optional fallback language files. -
get_text(self, entry_id, **variables):
Retrieve the translated text for a specific entry, applying any provided variable replacements. -
get_tooltip(self, entry_id, **variables):
Retrieve the tooltip text for a specific entry, applying any provided variable replacements.
XML Loader: XmlLanguageLoader
Inherits from BaseLanguageLoader, designed for loading XML-based translation files.
-
load_language(self, lang_file, is_fallback=False):
Load the specified XML language file. -
_load_metadata(self, meta):
Extract metadata from the XML file (e.g., code, name, authors). -
_load_entries(self, section, prefix="", is_fallback=False):
Load translation entries from a specific section in the XML.
JSON Loader: JsonLanguageLoader
Inherits from BaseLanguageLoader, designed for loading JSON-based translation files.
-
load_language(self, lang_file, is_fallback=False):
Load the specified JSON language file. -
_load_metadata(self, meta):
Extract metadata from the JSON file (e.g., code, name, authors). -
_load_entries(self, section, prefix="", is_fallback=False):
Load translation entries from a specific section in the JSON.
🤝 Contributing
We welcome contributions to Accetra! If you would like to contribute, please follow these steps:
-
Fork the repository:
Click the "Fork" button at the top of the page to create your own copy of the repository. -
Create a new branch:
In your forked repository, create a new branch for your changes:git checkout -b feature/my-new-feature
-
Make your changes:
Make your changes to the code. Add tests for new features or bug fixes. -
Commit your changes:
Once you're happy with your changes, commit them:git commit -am 'Add new feature'
-
Push your changes:
Push your changes to your forked repository:git push origin feature/my-new-feature
-
Create a pull request:
Go to the original repository and create a pull request. We'll review it and merge it into the main branch if everything looks good.
📝 License
This project is licensed under the MIT License - see the LICENSE file for details.
📢 Contact
If you have any questions or suggestions, feel free to open an issue or contact the maintainers:
- AK (TRCLoop): GitHub Profile
Happy coding! 😊
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
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 accetra-0.3.0.tar.gz.
File metadata
- Download URL: accetra-0.3.0.tar.gz
- Upload date:
- Size: 6.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.9.21
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
09f50b123bef528f5776a8ee2ece972384dcd83330555ba4fdd313e6e1dc1800
|
|
| MD5 |
6a5c5157e4daa1e7a6318f1368729c62
|
|
| BLAKE2b-256 |
4cedb5081418709baeb819987976bb782fa60f20f3aef3b2429619d9c9bc60b6
|
File details
Details for the file accetra-0.3.0-py3-none-any.whl.
File metadata
- Download URL: accetra-0.3.0-py3-none-any.whl
- Upload date:
- Size: 8.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.9.21
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9bdd8361d796d757f5b54fa9606594f08e53156b4779511cbb1d456a970b74e3
|
|
| MD5 |
e57c41ffc70c17274f3a3ace004232c3
|
|
| BLAKE2b-256 |
923444aecc949c6bed130040aeae97b9ddcc21f8af037bdc920d8810986a6710
|