Skip to main content

Mini localization manager for Python using JSON/YAML files

Project description

Mini language Manager

Mini localization manager for Python using JSON/YAML files

Mini language Manager is a lightweight and developer-friendly localization tool for Python. It allows you to organize translations in plain JSON or YAML files and access them with nested keys. Supports fallback and placeholders.


📦 Installation

pip install m-lang-m

🧰 Features

  • Supports both .json and .yaml/.yml translation files
  • Automatic directory resolution relative to the caller
  • Nested key support (e.g., menu.settings.language)
  • String formatting with {placeholders}
  • Returns the current config if needed

📁 Translation File Example

locale/en.json

{
    "title": "Main",
    "menu": {
        "settings": {
            "language": "Language",
            "placeholders": "Many {one}, {two} and {thee}"
        },
        "back" : "Go back"
    }
}

locale/ua.yml

title: "Головна"
menu:
  settings:
    language: "Мова"
    placeholders: "Багато {one}, {two} і {thee}"
  back: "Повернутися"

🚀 Usage

Configure the localizer

from mlangm import configure, translate, get_config

config_info = configure(default_lang='en', translations_path='locale')
# config_info = {
#     'default_lang': 'en',
#     'path': 'locale',
#     'mode': False
# }

print(translate('menu.settings.language'))  # Output: "Language"
print(translate('menu.back')) # Output: "Go back"
print(translate('menu.settings.language', 'ua'))  # Output: "Мова"
print(translate('title', 'ua')) # Output: "Головна"

print(translate('menu.settings.placeholders', one='first', two='second', thee='third'))
# Output: "Many first, second and third"

🔧 API

configure(default_lang = 'en', translations_path = 'translations', strict_mode = False) -> dict

Initializes the localizer and loads translations.

  • default_lang(str): The default language code.
  • translations_path(str): Directory with .json / .yaml files.
  • strict_mode(bool): If True, disables fallbacks.

Returns the configuration dictionary.

Example:

config_info = configure(default_lang = 'en', translations_path = 'locale', strict_mode = False)
configure()

translate(key: str, lang: str = None, **kwargs) -> str

Retrieves a translated string.
Supports fallback and placeholders.

Example:

translate('hello', 'en', name='Alex') # Output: "Hello, Alex!" | if 'hello' key -> "Hello, {name}!"
translate()

get_config(key: str = None) -> str | bool | dict

Returns the current configuration or a single setting.

  • default_lang(str): Returns the default language code
  • path(str): Returns the path to the translations directory
  • mode(bool): Returns whether strict mode is enabled

Example:

get_config("path") # Output: "locale"
get_config()

_extra() -> Localizer

Technical action, under normal conditions, not to be used.

Access the internal Localizer instance directly.

Example:

from mlangm import configure, _extra

configure(default_lang='en', translations_path='locale')

print(_extra().config) # Output: {'default_lang': 'en', 'path': 'locale', 'mode': False}
print(_extra().default_lang) # Output: 'en'
print(_extra().translations) # Output: Dictionary with translations from the 'locale' folder
_extra()

📄 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

m_lang_m-0.0.1.tar.gz (5.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

m_lang_m-0.0.1-py3-none-any.whl (6.4 kB view details)

Uploaded Python 3

File details

Details for the file m_lang_m-0.0.1.tar.gz.

File metadata

  • Download URL: m_lang_m-0.0.1.tar.gz
  • Upload date:
  • Size: 5.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.3

File hashes

Hashes for m_lang_m-0.0.1.tar.gz
Algorithm Hash digest
SHA256 696dcc2866851d8f6528f25c1bfea8b5766d610af5fc84dbf450f3a9808c53dc
MD5 9d7f5bf6298f04cf4b41bde1f5043b60
BLAKE2b-256 abecf9ec7b7a414101a4346581a2780359d3c2c998b21d42fdde76081ce1b745

See more details on using hashes here.

File details

Details for the file m_lang_m-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: m_lang_m-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 6.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.3

File hashes

Hashes for m_lang_m-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 26bda756d878df74516f1df8b708661c9d86cb93595f3d3b4d56b3901e8e765d
MD5 ba61ce265230c6f3748dbf1d1c60c4d3
BLAKE2b-256 a51379df504310a5187ed42a29ffd63e12f2935ef2903f95c83bbb1c7dca7690

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page