PyLangs
PyLangs is a Python library designed to simplify the management of multiple languages in projects that need to support internationalization. Whether you're building a website, app, or any multilingual system, PyLangs
makes it easy to store and retrieve messages in various languages with organized categories.
Features
- Easy to Use: Simple API for adding and retrieving language-specific messages.
- Category Support: Organize your messages by categories (e.g., general, support, etc.).
- Flexible: Add messages for any language, and easily switch between them.
- Fallback Support: Default fallback to a specified language if a message is missing in the current language.
- Customizable: Supports adding, retrieving, and printing all messages or language-specific ones.
Installation
pip install pylangs
Usage
Initialize the language management system.
from pylangs import Langs
langs = Langs()
Simple Example
You can easily insert and retrieve messages in multiple languages.
langs.insert("ar", "WELCOME_MESSAGE", "السلام عليكم ورحمة الله, وبركاته")
langs.insert("ar", "SUPPORT_WELCOME_MESSAGE", "اهلًا بك في الدعم الفني!")
langs.insert("en", "WELCOME_MESSAGE", "Hi, How are you?")
langs.insert("en", "SUPPORT_WELCOME_MESSAGE", "Hi, This is Support, How can I help you?")
print(langs.get("ar", "WELCOME_MESSAGE"))
print(langs.get("ar", "SUPPORT_WELCOME_MESSAGE"))
print(langs.get("en", "WELCOME_MESSAGE"))
print(langs.get("en", "SUPPORT_WELCOME_MESSAGE"))
Using Categories
To further organize messages, you can assign them to categories, making it easier to handle messages for different contexts (e.g., general, support, etc.).
langs.insert("ar", "WELCOME_MESSAGE", "السلام عليكم ورحمة الله, وبركاته", category="GENERAL")
langs.insert("ar", "WELCOME_MESSAGE", "اهلًا بك في الدعم الفني!", category="SUPPORT")
langs.insert("en", "WELCOME_MESSAGE", "Hi, How are you?", category="GENERAL")
langs.insert("en", "WELCOME_MESSAGE", "Hi, Welcome to our support team, How can I help you today?", category="SUPPORT")
print(langs.get("ar", "WELCOME_MESSAGE", category="GENERAL"))
print(langs.get("ar", "WELCOME_MESSAGE", category="SUPPORT"))
print(langs.get("en", "WELCOME_MESSAGE", category="GENERAL"))
print(langs.get("en", "WELCOME_MESSAGE", category="SUPPORT"))
Printing All Messages
To print all messages across all languages:
print(langs)
To print messages for a specific language:
print(langs.en)
print(langs.ar)
Example Output
Here’s an example of what the output might look like:
{
"lang_code": "en",
"categories": {
"GENERAL": {
"WELCOME_MESSAGE": "Hi, How are you?"
},
"SUPPORT": {
"WELCOME_MESSAGE": "Hi, Welcome to our support team, How can I help you today?"
}
}
}
LICENSE
- This project is licensed under the MIT License. See the
LICENSE
file for more information.