❤️ Support Masonite

masonite-localization

Community V0.1.3
5
1
MIT License

pip install masonite-localization

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

pip install masonite-localization

Configuration

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

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

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

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

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.

{
  "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

from localization.facades import Localization

# Returns current locale
Localization.current_locale()

Changing Locale

from localization.facades import Localization

# Returns nothing
Localization.set_locale('jp')

Checking if current local matches

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.

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

Then you can retrieve translation strings as below in template.

{{ __("message") }}

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

License

masonite-localization is open-sourced software licensed under the MIT license.

Last update: