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.
- Host: GitHub
- URL: https://github.com/py-package/masonite-localization
- Owner: py-package
- License: mit
- Created: 2022-08-27T18:33:10.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2023-09-11T03:16:48.000Z (over 2 years ago)
- Last Synced: 2025-04-23T14:14:47.334Z (8 months ago)
- Language: Python
- Size: 112 KB
- Stars: 5
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# masonite-localization
## 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).