Skip to main content

A simple internationalization (i18n) library for Python.

Project description

shiertier_i18n

english | 中文

Introduction

shiertier_i18n is a simple internationalization (i18n) library for Python, designed to help developers easily localize their applications into different languages. The library is based on the gettext module and supports automatic detection of language and localization directories from environment variables, providing a simple interface for translating strings.

Installation

You can install shiertier_i18n via pip:

pip install git+https://github.com/shiertier-utils/shiertier_i18n.git

Please note that this project is still under development.

Usage

Initialization

First, you need to initialize the I18n class:

from shiertier_i18n import I18n

# Using default language and localization directory
i18n = I18n()

# Or specify the language and localization directory
i18n = I18n(language_str='zh_CN', locales_dir='/path/to/locales')

Translating Strings

You can use the translate method to translate strings:

translated_str = i18n.translate("Hello, world!")
print(translated_str)

You can also pass a dictionary to replace placeholders in the translated string:

translated_str = i18n.translate("Hello, $$name$$!", replace_dict={'$$name$$': 'Alice'})
print(translated_str)

Shortcut

If you only need to quickly translate a string, you can use the easy_i18n shortcut:

from shiertier_i18n import easy_i18n

translated_str = easy_i18n("Hello, world!")
print(translated_str)

Configuration

Language

By default, the I18n class will get the language setting from the LANGUAGE environment variable. If LANGUAGE is not set, it defaults to en_US.

You can also specify the language when initializing the I18n class:

i18n = I18n(language_str='zh_CN')

Localization Directory

By default, the I18n class will get the localization directory from the SHIERTIER_LOCALES_DIR environment variable. If SHIERTIER_LOCALES_DIR is not set, it defaults to the .shiertier/locales directory under the user's home directory.

You can also specify the localization directory when initializing the I18n class:

i18n = I18n(locales_dir='/path/to/locales')

Dependencies

  • gettext

License

This project is released under the MIT License. See the LICENSE file for details.

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

shiertier_i18n-0.0.4.tar.gz (3.6 kB view details)

Uploaded Source

Built Distribution

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

shiertier_i18n-0.0.4-py3-none-any.whl (4.5 kB view details)

Uploaded Python 3

File details

Details for the file shiertier_i18n-0.0.4.tar.gz.

File metadata

  • Download URL: shiertier_i18n-0.0.4.tar.gz
  • Upload date:
  • Size: 3.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.9.20

File hashes

Hashes for shiertier_i18n-0.0.4.tar.gz
Algorithm Hash digest
SHA256 7b79962dcb9ab8700195022d56fb34eb9c1b3bf5e47f378b0e7f53502c50014e
MD5 65d5dc6e66c17c1c352e90e12d52b05f
BLAKE2b-256 76e7284221a12d7c9050b98642bf0ff0f9b975a4f89b53dd7b9d150ad5b29a4c

See more details on using hashes here.

File details

Details for the file shiertier_i18n-0.0.4-py3-none-any.whl.

File metadata

  • Download URL: shiertier_i18n-0.0.4-py3-none-any.whl
  • Upload date:
  • Size: 4.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.9.20

File hashes

Hashes for shiertier_i18n-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 c937b3c06a311519002d58bdd93611a98a7856cca0ed7a90edf5d1af2b3d2145
MD5 4e3ca2cdcdf9c1ce443d2e7beaffe65c
BLAKE2b-256 615425ff907019d4b4a9856f7cfdd75ce7b6d2a2f38e46bfb6585e6664adad5f

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