a5r0n / pyi18n

Small and easy to use internationalization library inspired by Ruby i18n

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

PyI18n

PyI18n is a simple and easy to use internationalization library for Python, inspired by Ruby i18n.

Python version Code Climate Issue Count

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

Installation

You can install PyI18n via pip:

pip install pyi18n-v2

Getting Started

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

To use PyI18n in your application, you will first need to create a locales folder in the root directory of your project. Within this folder, you can create locale files in the format of your choice (e.g. YAML, JSON).

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

You can then create an instance of the PyI18n class, passing in the desired languages and, optionally, a custom locales directory.

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/")
_: callable = 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="ジョンさん"))
#> こんにちは、ジョンさん!

Integrate pyi18n with Django project

To integrate pyi18n into your Django project, you will need to first add a locale field to your user model class. This field will store the user's preferred language, which will be used to retrieve the appropriate translations from the locales directory.

Next, you will need to configure pyi18n in your settings.py file by creating an instance of the PyI18n class and specifying the available languages. You can also create a gettext function for ease of use.

In your views, you can then use the gettext function to retrieve translations based on the user's preferred language. To use translations in templates, you will need to create a custom template tag that utilizes the gettext function.

settings.py

from pyi18n import PyI18n

i18n: PyI18n = PyI18n(['pl', 'en'])
_: callable = i18n.gettext

views.py

from mysite.settings import _

def index(request):
    translated: str = _(request.user.locale, 'hello', name="John")
    return HttpResponse(f"This is an example view. {translated}")

register template tag

from django import template
from mysite.settings import _

register = template.Library()

@register.filter(name='t')
def translate(locale: str, path: str, **kwargs):
    return _(locale, path, **kwargs)

That's it, you have now successfully installed and configured PyI18n for your project. You can now use the provided gettext function to easily retrieve translations based on the user's preferred language. Additionally, you can use the provided template tag to easily retrieve translations in your templates. And if you need to use custom loaders you can use the PyI18nBaseLoader to create your own loaders.


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: str = "locales/"
    loader: PyI18nBaseLoader = MyCustomLoader(load_path=load_path)
    i18n: PyI18n = PyI18n(("en",), loader=loader)
    _: callable = 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.

Limitations

  • Normalization task will not work for custom loader classes except xml, if you need that use one of build in loaders or user XML loader from example.

Roadmap

See issues, If I have enough time and come up with a good idea on how this package can be improved, I'll post it there, along with tip.

Release History

Release History available at https://sectasy0.github.io/pyi18n/home/release-history/.

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 'feat: Add some fooBar')
  4. Push to the branch (git push origin feature/fooBar)
  5. Create a new Pull Request

About

Small and easy to use internationalization library inspired by Ruby i18n

License:MIT License


Languages

Language:Python 100.0%