Skip to main content

A Python internationalization (i18n) package

Project description

Pi18n

Pi18n is an internationalization library for Python

Installation

Install using pip

pip install pi18n

Getting started

Pi18n lets you internationalize your Python app in a simple way. It works like this:

  • Provide Pi18n with as many JSON files as languages your app is going to support.

    • Each file will be named with the locale of the language it represents.

    • The content that is going to be placed in the app will be in each of these files, translated into the language that

    the file represents and associated with a code.

    • The way a translation is associated with a code is by a JSON object: each key of the object is the code, and the value

    is the translation associated to the code for every language.

    • Every JSON object of every file must have the same keys.

    • Translations can contain named parameters (placeholders) that will be replaced by a certain value on translation-time. Provide

    as many as needed with this syntax: {param-code}.

  • Instantiate an object of TranslationService class. Its constructor must receive:

    1. The path with the translation files.

    2. The default locale.

  • Call the get method of the TranslationService instance for getting a translation:

    1. Provide the translation code to the get method as the first argument.

    2. Optionally, provide parameters to the translation either as a dict or as keyword arguments.

Usage

1. Place some translation files in your project

resources/es.json

{

  "HELLO_WORLD": "Hola mundo!",

  "WITH_PARAMS": "Los parámetros son: {fruit1}, {fruit2} and {fruit3}"

}

resources/en.json

{

  "HELLO_WORLD": "Hello world!",

  "WITH_PARAMS": "Params are: {fruit1}, {fruit2} and {fruit3}"

}

2. Create an instance of TranslationService

main.py

from pi18n import TranslationService



translation_service = TranslationService('resources', 'es')

3. Use the TranslationService

Get a translation:

translation = translation_service.get("HELLO_WORLD")

print(f"Message in Spanish: {translation}")



>>> "Message in Spanish: Hola mundo!"

Change locale at runtime:

translation_service.change_locale("en")

translation = translation_service.get("HELLO_WORLD")

print(f"Message in English: {translation}")



>>> "Message in English: Hello world!"

Get a translation that receives some params as a dict:

params = {"fruit1": "apple", "fruit2": "orange", "fruit3": "banana"}

translation = translation_service.get("WITH_PARAMS", params)

print(f"Message with params: {translation}")



>>> "Message with params: Params are: apple, orange and banana"

Get a translation that receives some params as kerword arguments:

translation = translation_service.get("WITH_PARAMS", fruit1="apple", fruit2="orange", fruit3="banana")

print(f"Message with params: {translation}")



>>> "Message with params: Params are: apple, orange and banana"

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

pi18n-0.1.0.tar.gz (3.3 kB view details)

Uploaded Source

Built Distribution

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

pi18n-0.1.0-py3-none-any.whl (3.8 kB view details)

Uploaded Python 3

File details

Details for the file pi18n-0.1.0.tar.gz.

File metadata

  • Download URL: pi18n-0.1.0.tar.gz
  • Upload date:
  • Size: 3.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.8

File hashes

Hashes for pi18n-0.1.0.tar.gz
Algorithm Hash digest
SHA256 d0bbd8e8fe2cd228dba06785b268e4b82c453d4a25053c360f8e8f97e357dc4c
MD5 cf6e66fddf88d7d1a0cdc7bf13b11275
BLAKE2b-256 073fafe3cbf00e7af0da55159263161a89d44551fd4db1cc4017ed0fe7f6a9f7

See more details on using hashes here.

File details

Details for the file pi18n-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: pi18n-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 3.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.8

File hashes

Hashes for pi18n-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3b2d776debd3418547bf53ca0636a3cca08a6f317cabd5d12f181fa6dcd746d6
MD5 9ce30f0ad518f2c8b82f693a7f35027d
BLAKE2b-256 aa0c3591467600f598b1ce15611c5a5464f243ef66c5943a71280ce254a3976f

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