# masonite-localization

<p align="center">
    <img src="https://banners.beyondco.de/Masonite%20Localization.png?theme=light&packageManager=pip+install&packageName=masonite-localization&pattern=charlieBrown&style=style_2&description=Add+multi-language+support+in+your application.&md=1&showWatermark=1&fontSize=100px&images=adjustments&widths=50&heights=50">
</p>

<p align="center">
  <a href="https://docs.masoniteproject.com">
    <img alt="Masonite Package" src="https://img.shields.io/static/v1?label=Masonite&message=package&labelColor=grey&color=blue&logo=data:image/png;base64, ">
  </a>
  <img alt="GitHub Workflow Status (branch)" src="https://img.shields.io/github/workflow/status/py-package/localization/Test%20Application">
  <img src="https://codecov.io/gh/py-package/localization/branch/main/graph/badge.svg?token="/>
  <img alt="PyPI" src="https://img.shields.io/pypi/v/localization">
  <img src="https://img.shields.io/badge/python-3.7+-blue.svg" alt="Python Version">
  <img alt="GitHub release (latest by date including pre-releases)" src="https://img.shields.io/github/v/release/py-package/localization?include_prereleases">
  <img alt="License" src="https://img.shields.io/github/license/py-package/localization">
  <a href="https://github.com/psf/black"><img alt="Code style: black" src="https://img.shields.io/badge/code%20style-black-000000.svg"></a>
</p>

## 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 and that's it.

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')
```

**Using in template**

Imagine you have following data in `en.json` locale file.
```json
{
  "message": "Hello, World!",
  "notification": {
    "message": "This is notification message."
  }
}
```

Then you can use above local as below in template.

```jinja
{{ __("message") }}
<!-- OR -->
{{ __("notification.message") }}
```

## Contributing
If you want to contribute then please feel free to `fork` and create `pr`. Also you can add your suggestions and issues.

## Maintainers

- [x] [Yubaraj Shrestha](https://www.github.com/py-package)

## License

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

