Python Localization Code Generator from TOML files, JSON, and YAML
Project description
pyl10nc
A Python library to convert TOML, JSON, and YAML localization files into Python classes for easy access to localized strings.
中文文档 | English
Background
Consider the traditional approach to internationalization in Python:
import gettext
# Create translation instance
t = gettext.translation('myapplication', '/path/to/my/language/directory')
_ = t.gettext
# Use translation function
print(_('This is a translatable string.'))
There are several pain points with this approach:
-
Tedious string typing: When using translation content, you have to type out each string character by character—including quotes and parentheses. Sometimes you're not even sure if these translations exist in the resource files.
-
Unused translations: Resource files may contain translation resources that are never actually used in the code!
-
Complex file management: Jumping between
en-us.jsonandzh-cn.jsonfiles or preparingxxx.mofiles is genuinely cumbersome!
That's where pyl10nc comes in. It converts language translation resource files represented in TOML, JSON, or YAML format into Python code!
Now you can use localization and access properties with . to get auto-completion in various IDEs. No more typing entire strings—perhaps when you type This, your IDE has already completed the input for you. I love auto-completion! Additionally, you can identify unused translations by checking usage, making it easy to clean them up.
Features
- Convert TOML, JSON, and YAML localization files to Python classes
- Support nested and flat structures for all formats
- Generate property-based access methods
- Automatic method name sanitization
- Optional YAML support via PyYAML
Installation
# Basic installation (TOML and JSON support)
pip install pyl10nc
# With YAML support
pip install pyl10nc[yaml]
Usage
Command Line
# TOML file
pyl10nc input.toml -o output.py
# JSON file
pyl10nc input.json -o output.py
# YAML file (requires PyYAML)
pyl10nc input.yaml -o output.py
Python API
import pyl10nc
# TOML file
pyl10nc.generate('input.toml', 'output.py')
# JSON file
pyl10nc.generate('input.json', 'output.py')
# YAML file (requires PyYAML)
pyl10nc.generate('input.yaml', 'output.py')
Supported Formats
TOML Format Example
# filename.toml
[test.hello]
zh-cn = "你好"
en-us = "Hello"
[test.hello_doc]
doc = "Use 'doc' to specify the documentation for the property."
zh-cn = "这是一个测试"
en-us = "This is a test"
[test.goodbye]
zh-cn = "再见"
en-us = "Goodbye"
JSON Format Example
Nested Structure
{
"test": {
"hello": {
"zh-cn": "你好",
"en-us": "Hello"
},
"goodbye": {
"zh-cn": "再见",
"en-us": "Goodbye"
}
}
}
Flat Structure
{
"test.hello": {
"zh-cn": "你好",
"en-us": "Hello"
},
"test.goodbye": {
"zh-cn": "再见",
"en-us": "Goodbye"
}
}
YAML Format Example
Nested Structure
# filename.yaml
test:
hello:
zh-cn: "你好"
en-us: "Hello"
goodbye:
zh-cn: "再见"
en-us: "Goodbye"
Flat Structure
# filename_flat.yaml
test.hello:
zh-cn: "你好"
en-us: "Hello"
test.goodbye:
zh-cn: "再见"
en-us: "Goodbye"
Generated Python Code Example
Generated code
# filename.py
import json
class Localization:
"""Automatically generated localization class."""
__normalized_data: dict[str, dict[str, str]] = None
lang: str = "zh-cn"
def __init__(self):
"""Initialize localization data."""
with open('filename.json', 'r', encoding='utf-8') as f:
self.__normalized_data = json.load(f)
def _get_translation(self, key: str) -> str:
"""
Get the translation value for the specified key.
:param key: Flattened translation key (e.g., test.group1.hello)
:return: Translation value for the target language, or key if not found
"""
resource = self.__normalized_data.get(key, {})
return resource.get(self.lang, key)
@property
def test_hello(self) -> str:
"""你好"""
return self._get_translation("test.hello")
@property
def test_group1_welcome(self) -> str:
"""欢迎"""
return self._get_translation("test.group1.welcome")
@property
def test_group1_farewell(self) -> str:
"""再见"""
return self._get_translation("test.group1.farewell")
@property
def test_group2_question(self) -> str:
"""你好吗?"""
return self._get_translation("test.group2.question")
@property
def test_group2_response(self) -> str:
"""The response to the question 'How are you?'"""
return self._get_translation("test.group2.response")
localization = Localization()
Usage
from filename import localization
localization.lang = "en-us" # Set desired locale
print(localization.test_hello)
# Output: "Hello"
# Switch to Chinese
localization.lang = "zh-cn"
print(localization.test_hello)
# Output: "你好"
License
MIT License
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 pyl10nc-1.3.4.tar.gz.
File metadata
- Download URL: pyl10nc-1.3.4.tar.gz
- Upload date:
- Size: 11.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
64971bfd1a52bc4fbe5f427f52d27b05ff2e6d4d07139df42d79905dc9dcd10e
|
|
| MD5 |
ad4ee3c4b5c7e414343456e36d0f7f72
|
|
| BLAKE2b-256 |
5f17f1c089ea4b3bbe869c582508d9cb3311c0bd23a7e59f257989ac3e221cc7
|
Provenance
The following attestation bundles were made for pyl10nc-1.3.4.tar.gz:
Publisher:
python-publish.yml on Shasnow/pyl10nc
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pyl10nc-1.3.4.tar.gz -
Subject digest:
64971bfd1a52bc4fbe5f427f52d27b05ff2e6d4d07139df42d79905dc9dcd10e - Sigstore transparency entry: 1224714657
- Sigstore integration time:
-
Permalink:
Shasnow/pyl10nc@67175bf38a361245f19a5aaa8450eee669fba826 -
Branch / Tag:
refs/tags/1.3.4 - Owner: https://github.com/Shasnow
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@67175bf38a361245f19a5aaa8450eee669fba826 -
Trigger Event:
release
-
Statement type:
File details
Details for the file pyl10nc-1.3.4-py3-none-any.whl.
File metadata
- Download URL: pyl10nc-1.3.4-py3-none-any.whl
- Upload date:
- Size: 8.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
367b12e111a5c45b21782efa347bfa917e549e21e16d7825af6a4e51952a1b07
|
|
| MD5 |
9b1760e04bd3b899816ca59ba1c03e4b
|
|
| BLAKE2b-256 |
074e96bc8badb95017a179438da794f8c2d4c720794cd5c008cdf6093d5c0454
|
Provenance
The following attestation bundles were made for pyl10nc-1.3.4-py3-none-any.whl:
Publisher:
python-publish.yml on Shasnow/pyl10nc
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pyl10nc-1.3.4-py3-none-any.whl -
Subject digest:
367b12e111a5c45b21782efa347bfa917e549e21e16d7825af6a4e51952a1b07 - Sigstore transparency entry: 1224714695
- Sigstore integration time:
-
Permalink:
Shasnow/pyl10nc@67175bf38a361245f19a5aaa8450eee669fba826 -
Branch / Tag:
refs/tags/1.3.4 - Owner: https://github.com/Shasnow
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@67175bf38a361245f19a5aaa8450eee669fba826 -
Trigger Event:
release
-
Statement type: