A lightweight i18n localization package for Python
Project description
Localify
Localify is a lightweight, easy-to-use internationalization (i18n) package for Python. It provides a simple and efficient way to manage translations for applications, supporting JSON and YAML formats.
Features
- Easy-to-use syntax: Build localized strings with chainable methods.
- Customizable missing key behavior: Choose between showing the key, a default value, or logging a warning.
- JSON and YAML support: Load translations from both formats.
- Dynamic configuration: Adjust settings like language, logging levels, and missing key behavior on the fly.
Installation
From PyPI
To install Localify from PyPI:
pip install localify
From Source
To Install the package locally:
- Clone the repository:
git clone https://github.com/felicedev/localify.git
cd localify
- Install in editable mode:
pip install -e .
Usage
Here are some examples to help you get started with Localify.
1. Setting up translations
Make sure you have a locales/ directory in your project root with JSON or YAML files for your translations.
Example: /locales/en.json
{
"hello": "Hello",
"world": "World",
"greeting": "Greetings"
}
Example: /locales/it.json
{
"hello": "Ciao",
"world": "Mondo",
"greeting": "Saluti"
}
2. Basic usage
from localify import l, config
# Configure the package
config(
language='en', # Set the language
missing_key_behavior='key', # Show the key if a translation is missing
logging_level=30 # Set logging to WARNING
)
# Create localized strings
message = l("hello").space().l("world").exclamation()
print(message) # Output: Hello World!
# Switch to Italian
config(language='it')
print(l("hello").space().l("world").exclamation()) # Output: Ciao Mondo!
Methods
The l function returns a LocalifyString object that lets you chain methods to build localized strings.
Methods for Building Strings
| Method | Description | Example |
|---|---|---|
l(key) |
Adds a localized translation for the given key. | l("hello").space().l("world") |
space() |
Adds a space. | l("hello").space() |
colon() |
Adds a colon :. |
l("key").colon() |
comma() |
Adds a comma ,. |
l("key").comma() |
period() |
Adds a period .. |
l("key").period() |
exclamation() |
Adds an exclamation mark !. |
l("key").exclamation() |
question() |
Adds a question mark ?. |
l("key").question() |
Configuration Methods
| Function | Description |
|---|---|
config() |
Configure language, logging, and missing key behavior. |
set_language(lang) |
Set the current language. |
set_missing_key_behavior(behavior, default_value) |
Set how missing keys are handled ('key', 'default', or 'log'). |
enable_logging(level) |
Enable logging with a specific level (DEBUG, INFO, etc.). |
Missing Key Behavior
Localify allows you to configure how missing translation keys are handled. Use the set_missing_key_behavior() function or the config() function to customize this.
Options
'key'(Default): Show the missing key as-is.'default': Show a default value if provided.'log': Log a warning message and show the missing key.
Example
from localify import config, l
# Show a default value for missing keys
config(missing_key_behavior='default', default_missing_value='[NOT FOUND]')
print(l("missing_key")) # Output: [NOT FOUND]
# Log a warning for missing keys
config(missing_key_behavior='log')
print(l("missing_key")) # Output: missing_key (and logs a warning)
Advanced Configuration
Loading Custom Translations
You can load translations from a custom directory using load_translations():
from localify import load_translations, set_language, l
# Load translations from a custom directory
load_translations('path/to/custom_locales')
set_language('en')
print(l("custom_key")) # Output depends on your custom translations
Logging Configuration
Localify integrates with Python’s logging module. You can enable and configure logging as follows:
from localify import enable_logging, DEBUG
# Enable debug logging
enable_logging(level=DEBUG)
Roadmap
Completed
- JSON support for translations.
- Chainable methods for building localized strings.
- Dynamic configuration of language and behavior.
- Basic logging integration.
- Unit tests for core functionality.
In Progress
- Support for YAML translation files.
- Enhanced error handling for missing translations.
Planned
- Add support for
.poand.motranslation files. - Implement dynamic locale reloading without restarting the application.
- Create a CLI tool for managing translations.
- Translation validation tool to check for missing keys across locales.
- Add documentation generation for translation files.
License
This project is licensed under the MIT License. See the LICENSE file for details.
Contributing
Contributions are welcome! Feel free to submit issues or pull requests on GitHub.
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 localify-0.1.0.tar.gz.
File metadata
- Download URL: localify-0.1.0.tar.gz
- Upload date:
- Size: 8.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b21d351f914e12b628920278e1bcf6ce9ce53228c7bfef027f5133bdc55095b7
|
|
| MD5 |
f01d245af8a927f875c1c2b949c46f6d
|
|
| BLAKE2b-256 |
a8e9bf7c7ddfc626dc08f2737bbd728c532134151fc3ac9f9f717e246ce26a56
|
File details
Details for the file localify-0.1.0-py3-none-any.whl.
File metadata
- Download URL: localify-0.1.0-py3-none-any.whl
- Upload date:
- Size: 9.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d917d3f6b3378555fe17498d86f42e6f71fbe78dd05f0f4c1c4d9c37f781e227
|
|
| MD5 |
f1a7d620e8a83625065007c4c53d1de6
|
|
| BLAKE2b-256 |
5d9c6a29337133a44a1adb8d0ac04f9d432b2597a537baca6e39016f85b1c87a
|