Skip to main content

A custom i18n implementation that is built to be easily used.

Project description

ez-i18n

An easy implementation of i18n into Python. Github


Setting up

Start by downloading the package via pip.

python3 -m pip install ezi18n

After that, you can just import it into your file.


Examples

import ezi18n
import random
from typing import Literal

_ = ezi18n.Translator() # if you don't provide anything in the brackets, it will look for the file's name's language JSON file. If the main file is main.py, it will look for main_lang.json - unless you provide a base filename (in this case: main) and/or a suffix (by default: _lang)
dice = random.randint(1, 6)
language = input("What's your native language? / Was ist Deine Muttersprache? (en/de) >>> ")

print(_("dice_result", language, dice=dice))
{
   "en": {
       "dice_result": "Your dice rolled {dice}!"
   },
   "de": {
       "dice_result": "Dein Würfel hat eine {dice} gewürfelt!"
   }
}

Working with plurals

import ezi18n
import locale

_ = ezi18n.Translator("main", "_plurals") # this will look for main_plurals.json as the language JSON file
apples = int(input("How many apples do you have? >>> "))
language = locale.getdefaultlocale()[:2] # this will return the default OS language, such as en, hu, de, fr, es etc.

print(_.one("apples", language, apples=apples))
{
   "en": {
       "apples": ["You only have one apple! What a loser...", "You have {apples} apples!"]
   },
   "hu": {
       "apples": ["{apple} almád van!"]
   }
}

This example does a great job at presenting how the .one() function works. Here's the explanation for the code: The code will look for the default OS language, and will tell you how many apples you have with plurals in mind. I always get angry when I see "you have 1 apples"-ish text on a website, and it's an easy fix, but for some reason, many developers don't pay attention to it. Hungarian doesn't make a difference between plurals, so the list for "hu"/"apples" only has 1 item. This won't be a problem though - the library isn't hardcoded to only look for the first and second elements in the returned list, it will look for the first and the last. Which means that if a list only has 1 item, it will only return that item.

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

ezi18n-0.0.1.tar.gz (3.4 kB view details)

Uploaded Source

Built Distribution

ezi18n-0.0.1-py3-none-any.whl (3.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: ezi18n-0.0.1.tar.gz
  • Upload date:
  • Size: 3.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.6

File hashes

Hashes for ezi18n-0.0.1.tar.gz
Algorithm Hash digest
SHA256 c924800abcab024a9f89d4b9867de01312c71c9470e994434fc7902e17017aa4
MD5 0824c5c8fb5696df254a598b04cba4b8
BLAKE2b-256 32c04845a3b8f7c281cbb893bfe8519cf434145eefb4d911b2790b9611730335

See more details on using hashes here.

Provenance

File details

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

File metadata

  • Download URL: ezi18n-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 3.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.6

File hashes

Hashes for ezi18n-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 4ae882c5e7461a423926d916514145e0077e013be8be87e9fe1a47172479e48e
MD5 88d1c998bce90d81180e155965920dd5
BLAKE2b-256 3fee5e0201e5bb3f85ed329db40a978f47dd3c4b4373b2349a3706df2ba20886

See more details on using hashes here.

Provenance

Supported by

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