Skip to main content

Small and easy to use internationalizationlibrary inspired by Ruby i18n

Project description

PyI18n

Simple and easy to use internationalization library inspired by Ruby i18n.

Python version Code Climate Issue Count

Documentation available at https://sectasy0.github.io/pyi18n.

Installation

pip install pyi18n-v2

Using library in your application

A few motivating and useful examples of how pyi18n can be used.

In first step you have to create locales folder in your main application folder. Then you can create your own locale files in this folder.

for example:

$ mkdir -p my_app/locales
$ touch my_app/locales/en.yml
$ touch my_app/locales/pl.yml
$ touch my_app/locales/de.yml

Then create an instance of PyI18n class. For custom locales directory you can pass it as an argument (default is in app_root/locale/).

from pyi18n import PyI18n


# default load_path is locales/
# you can change this path by specifying load_path parameter
i18n = PyI18n(("en", "pl", "de", "jp"), load_path="translations/")
_ = i18n.gettext

print(_("en", "hello.hello_user", user="John"))
#> Hello John!

print(_("pl", "hello.hello_user", user="John"))
#> Witaj John!

print(_("de", "hello.hello_user", user="John"))
#> Hallo John!

print(_("jp", "hello.hello_user", user="ジョンさん"))
#> こんにちは、ジョンさん!

Creating custom loader class

To create custom locale loader you have to create a class which will inherit from PyI18nBaseLoader and override load method with all required parameters (see below). You can see an example of custom locale loader in examples/custom_xml_loader.py.

from pyi18n.loaders import PyI18nBaseLoader

class MyCustomLoader(PyI18nBaseLoader):

    def load(self, locales: tuple, load_path: str):
        # load_path is the path where your loader will look for locales files
        # locales is a tuple of locales which will be loaded
        # return a dictionary with locale data

        ...your custom loader logic...

        return {}

Then pass your custom loader to PyI18n class.

from pyi18n.loaders import PyI18nBaseLoader

class MyCustomLoader(PyI18nBaseLoader):

    def load(self, locales: tuple, load_path: str):
        # load_path is the path where your loader will look for locales files
        # locales is a tuple of locales which will be loaded

        ...your custom loader logic...

        # have to return a dictionary
        return {}

# don't use load_path in `PyI18n` constructor, if not using default yaml loader
if __name__ == "__main__":
    load_path = "locales/"
    loader = MyCustomLoader(load_path=load_path)
    i18n = PyI18n(("en",), loader=loader)
    _ = i18n.gettext

    print(_("en", "hello.hello_user", user="John"))
    #> Hello John!

Tasks

Tasks usage

$ pyi18n-tasks
usage: pyi18n-tasks [-h] [-p PATH] normalize
pyi18n-tasks: error: the following arguments are required: normalize

Normalization

Normalization process will sort locales alphabetically. The default normalization path is locales/, you can change it by passing -p argument.

$ pyi18n-tasks normalize 
$ pyi18n-tasks normalize -p my_app/locales/

Run tests

python3 tests/run_tests.py

For any questions and suggestions or bugs please create an issue.

TODO

  • Remove duplicates during normalization
  • Translation for pyi18n-tasks from multiple sources
  • Adding new locale from pyi18n-tasks

Release History

1.1.0 - unreleased

  • New features:
    • Added normalize task
    • Added ability to run normalize task from command line

1.0.0 - 2022-08-12

  • Initial release

Meta

Distributed under the MIT license. See LICENSE for more information.

https://github.com/sectasy0

Contributing

  1. Fork it (https://github.com/sectasy0/pyi18n)
  2. Create your feature branch (git checkout -b feature/fooBar)
  3. Commit your changes (git commit -am 'Add some fooBar')
  4. Push to the branch (git push origin feature/fooBar)
  5. Create a new Pull Request

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

pyi18n-v2-1.1.0.tar.gz (11.4 kB view hashes)

Uploaded Source

Built Distributions

pyi18n_v2-1.1.0-py3.8.egg (30.4 kB view hashes)

Uploaded Source

pyi18n_v2-1.1.0-py3-none-any.whl (13.0 kB view hashes)

Uploaded Python 3

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