Add multi-language support in your application.
Project description
masonite-localization
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") }}
<!-- OR -->
{{ __("notification.message") }}
License
masonite-localization is open-sourced software licensed under the MIT license.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file masonite-localization-0.1.3.tar.gz
.
File metadata
- Download URL: masonite-localization-0.1.3.tar.gz
- Upload date:
- Size: 10.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.10.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f3ba3feb194e6cff866aecfb8c9790596e39a9ad8bf6e41e7ad0bdea2510b3c0 |
|
MD5 | 40a65d4c64358b2e128d31406abf68f0 |
|
BLAKE2b-256 | c4d75a74bc53fddda0fb4bb876e8400fd37cd464b636c77e1bf2747d0fb17cc4 |
File details
Details for the file masonite_localization-0.1.3-py3-none-any.whl
.
File metadata
- Download URL: masonite_localization-0.1.3-py3-none-any.whl
- Upload date:
- Size: 7.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.10.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 97c0da41ec8a2f9576d48e15d6e01d452a42c6b24e64800f35dc06f0b97294a8 |
|
MD5 | 03806874d1e8725628b90c1d94f0f9dd |
|
BLAKE2b-256 | 38069c7bd7f380a8d8338d75470e6d523cac76c4e7ea508b398e85d7ad0b0d1f |