Skip to main content

Internalization tool for Python projects using `.toml` files for storing text content

Project description

i18n-toml

Python Version Package Version

Internationalization support library for Python projects. It expects a toml file format to parse localized strings hierarchically.

The library requires a Python language version >= 3.11, as it uses the built-in tomllib library.


Installation

Install with pip:

pip install i18n-toml

Install with uv:

uv add i18n-toml

Folder Structure

The library expects the folder structure for localization toml-files:

localizations/
└── locale_index/
    └── content_file.toml

For example, the structure of localizations of messages and button captions for Russian, English and Spanish locales could be:

localizations_folder/
├── en/
│   ├── messages.toml
│   └── buttons.toml
├── es/
│   ├── messages.toml
│   └── buttons.toml
└── ru/
    ├── messages.toml
    └── buttons.toml

Locale file structure

The standard toml format can be used in each locale file, including sections and dot-separated keys.

Example of locale file:

buttons.toml

# locales/en/buttons.toml
[auth]
login_btn = "Login"
logout_btn = "Logout"
[common]
ok_btn = "Ok"
cancel_btn = "Cancel"

Usage

Import the library:

import i18n_toml

Create I18nToml object, passing the localizations folder path and locale index to use:

from pathlib import Path

i18n = I18nToml(Path('./locales'), 'en')

Warning: the locale index is case sensitive!

Use get method of the object to obtain text value by key passed. The key is dot-separated string representing path to the value needed, starting by file name and navigating futher inside the file hierarchy (sections, keys).

caption = i18n.get("buttons.common.ok_btn")

Output: "Ok"

It's possible to use object's functor call, that is similar to using get method:

caption = i18n("buttons.common.ok_btn")

Added in version 0.2

The get method supports additional **kwargs arguments to set values for {...} placeholders in string values. For example, the string in toml file:

welcome = "Welcome, {username}!"

can be obtained with user name substituted by call:

msg = i18n("welcome", username="John")

Output: "Welcome, John!"

Dependencies

The library has no external dependencies, using only standard language tools. However, a Python version of at least 3.11 is required, since the tomllib library is used to work with toml files, which is built into the language starting with the specified version.

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

i18n_toml-0.2.tar.gz (3.0 kB view details)

Uploaded Source

Built Distribution

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

i18n_toml-0.2-py3-none-any.whl (3.9 kB view details)

Uploaded Python 3

File details

Details for the file i18n_toml-0.2.tar.gz.

File metadata

  • Download URL: i18n_toml-0.2.tar.gz
  • Upload date:
  • Size: 3.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.10 {"installer":{"name":"uv","version":"0.9.10"},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for i18n_toml-0.2.tar.gz
Algorithm Hash digest
SHA256 461bfa47a88e4c9b0165c13ff2cc56bc7efaf93182257ca892b7c38879bf29f1
MD5 109347abb74b6bf9ea86e740fbdc14d0
BLAKE2b-256 a9114e0371d2679b0643a4021fe3386cb57cb4d6e6a95287d1c99948ad9f1c1c

See more details on using hashes here.

File details

Details for the file i18n_toml-0.2-py3-none-any.whl.

File metadata

  • Download URL: i18n_toml-0.2-py3-none-any.whl
  • Upload date:
  • Size: 3.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.10 {"installer":{"name":"uv","version":"0.9.10"},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for i18n_toml-0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 a78f07bcd117a11e55fb3cf99839156e395ae51f8f8adce52ff7c66f29585195
MD5 6a45bc6de84a09703d82a3b38e06dacd
BLAKE2b-256 4f644c75d08a928dda55123f81d361d7c179b7b0cba838102fb3333c986c223d

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