Skip to main content

A Python3 library without dependencies that provides static translations across your app

Project description

pylocale

A Python 3 library that allows adding static files with translations to the application, and then apply them to dynamically change text in the application.

Installing

Python 3.6.9 or higher is recommended

To install the library, you can just run the following command:

# Linux/macOS
python3 -m pip install pylocale

# Windows
pip install pylocale

Translation files

You must provide a folder in your project directory where your translation files will be located.

your-app-root
|--translations
   |----en-us
   |----de-de
   |----zh-cn
   ...
...

The struture can be any, but the translation files must be accessible from the program. Translation files must be text files of .env-like format <KEY> = <TRANSLATION> (spaces are required or you will get a parsing error):

# en-us file example
HELP = Get help
SETTINGS = Settings
EXIT = Exit
# de-de file example
HELP = Hilfe bekommen
SETTINGS = Einstellungen
EXIT = Ausfahrt

It's recommended for the translation files to have the same keys, but it's not necessary.

Usage

You can use pylocal as follows:

from pylocal import PyLocal

translator = PyLocal(
    at='translations/',  # Your translations directory path.
    root='en',  # Default locale.
    silent=True  # Tell PyLocal not to raise exceptions in case of errors.
)

To get translation:

translator['HELP']  # >>> Help

To switch to another locale:

translator.switch('de-de')
translator['HELP']  # >>> Hilfe bekommen

Any translation you switch to covers the root translation file. That means If there was no help key, it would be taken from the root translation file:

# en-us file example
HELP = Get help
SETTINGS = Settings
EXIT = Exit
COLOR = Color
# en-gb file example
COLOR = Colour
translator = PyLocale(at='transaltions/', root='en-us')
tanslator['COLOR']  # >>> Color
translator.switch('en-gb')
tanslator['COLOR']  # >>> Colour
# SETTINGS is not defined in en-gb. It will be taken from en-us, which is the root translation file.
tanslator['SETTINGS']  # >>> Settings

Raising exceptions

If the silent mode is turned off (default) your app will break down with exceptions in case of errors. If you want the translator to try to return something, you may pass a True to silent parameter as shown above. In that case, the translator will return an empty string.

translator = PyLocale(
    at='transaltions/',
    root='blablabla',
    silent=True
)
tanslator['SETTINGS']  # >>> ''

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

pylocale-0.0.1.tar.gz (3.7 kB view details)

Uploaded Source

File details

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

File metadata

  • Download URL: pylocale-0.0.1.tar.gz
  • Upload date:
  • Size: 3.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.3.1 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.6.9

File hashes

Hashes for pylocale-0.0.1.tar.gz
Algorithm Hash digest
SHA256 c18d913bdfaf0c4d33ca91f009c7aaeda66c6a5ca7e570ea797e0fbb55221405
MD5 170ca4fef7277eae62610d709cfcaf3a
BLAKE2b-256 a497e8f13729fadfaa3cefbf32d7d19f7f941bb9d36f1f115eb052a6d3be8d99

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