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.6.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.6-py3-none-any.whl (4.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: shiertier_i18n-0.0.6.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.6.tar.gz
Algorithm Hash digest
SHA256 962ad1e18378dd5333133b47c198c4ff6bd1516445c387946afeabdb25cd7e0b
MD5 3dcbbd366026c83c508a14679e29330a
BLAKE2b-256 48506d7ce0b440b5296bf633f05d569a658fd638310f8dddabd4eb8f8a41ef2b

See more details on using hashes here.

File details

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

File metadata

  • Download URL: shiertier_i18n-0.0.6-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.6-py3-none-any.whl
Algorithm Hash digest
SHA256 7abed9f79a23f5be4e1c21b5863232ed805b46c75b633cbe27e5369039509fab
MD5 82ae2dd31b94fcb10461666d994fa923
BLAKE2b-256 2d090a14ddc81d3b2429e775411e72b65be378c5f24aaadceb191a6973631594

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