An open API service indexing awesome lists of open source software.

https://github.com/py-package/masonite-localization

Add multi-language support in your application.
https://github.com/py-package/masonite-localization

Last synced: 8 months ago
JSON representation

Add multi-language support in your application.

Awesome Lists containing this project

README

          

# masonite-localization





Masonite Package

GitHub Workflow Status (branch)

PyPI
Python Version
GitHub release (latest by date including pre-releases)
License
Code style: black

## Introduction

Add multi-language support in your application.

## Installation

```bash
pip install masonite-localization
```

## Configuration

Add LocalizationProvider to your project in `config/providers.py`:

```python
# config/providers.py
# ...
from localization import LocalizationProvider

# ...
PROVIDERS = [
# ...
# Third Party Providers
LocalizationProvider,
# ...
]
```

Then you can publish the package resources (if needed) by doing:

```bash
python craft package:publish localization
```

## Usage

The setup is very simple. Once you publish the package verify if there's a `lang` directory in the root of your project or not, if not then create one and then create a json file named `en.json` put in some values in it or you can copy/paste following contents.

```json
{
"message": "Hello"
}
```

By default you'll have `english` language setup in `config` which you can change. The locale can be changed on the fly as well.

You can add as many language json files as you wish. For eg; if need `Japanese` locale then, I will just copy everything from `en.json` file and then create new file named `jp.json` and then update the values in those json.

**Getting Current Locale**

```python
from localization.facades import Localization

# Returns current locale
Localization.current_locale()
```

**Changing Locale**

```python
from localization.facades import Localization

# Returns nothing
Localization.set_locale('jp')
```

**Checking if current local matches**

```python
from localization.facades import Localization

# returns either True or False
Localization.is_locale('jp')
```

**Retrieving Translations in Template**

Imagine you have following translations in `en.json` locale file.

```json
{
"message": "Hello, World!",
"notification": {
"message": "This is notification message."
}
}
```

Then you can retrieve translation strings as below in template.

```jinja
{{ __("message") }}

{{ __("notification.message") }}
```

## License

masonite-localization is open-sourced software licensed under the [MIT license](LICENSE).