Published on

Masonite Localization



Add multi-language support in your application.


pip install masonite-localization


Add LocalizationProvider to your project in config/

# config/
# ...
from localization import LocalizationProvider

# ...
    # ...
    # Third Party Providers
    # ...

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

python craft package:publish localization


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

Changing Locale

from localization.facades import Localization

# Returns nothing

Checking if current local matches

from localization.facades import Localization

# returns either True or False

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") }}
<!-- OR -->
{{ __("notification.message") }}