Flask-Translator is a quick and easy-to-use extension for Flask projects, enabling translation from YAML files.
Project description
Flask Translator
Flask Translator is a simple Flask extension for handling translations in your web applications. It provides easy
integration with Flask applications to support multiple languages.
Installation
You can install Flask Translator using pip:
pip install flask-translator
Usage
Example Project Directory Structure
your_project_directory/
|-- app.py
|-- translations/
| |-- messages.en.yaml
| |-- messages.tr.yaml
|-- templates/
| |-- index.html
|-- README.md
|-- requirements.txt
Translation File Examples
# translations/messages.en.yaml
request:
schema:
invalid_email: 'Please enter a valid email address!'
invalid_username: 'Please enter a valid username!'
invalid_or_email_username: 'Please enter a valid username or email address!'
# translations/messages.tr.yaml
request:
schema:
invalid_email: 'Lütfen geçerli bir e-posta adresi girin!'
invalid_username: 'Lütfen geçerli bir kullanıcı adı girin!'
invalid_or_email_username: 'Lütfen geçerli bir kullanıcı adı veya e-posta adresi girin!'
Make sure to place your translation files in the specified TRANSLATIONS_PATH
directory with the corresponding language codes following the ISO 639-1 standard.
App Factory Usage
# app.py
from flask import Flask, render_template
from flask_translator import Translator
def create_app():
app = Flask(__name__)
# Configure Flask Translator
app.config['DEFAULT_LANG'] = 'tr'
app.config['LANG_INITIALIZER'] = 'headers'
app.config['DEFAULT_MESSAGE'] = 'Process finished!'
app.config['TRANSLATIONS_PATH'] = 'translations'
# Initialize Flask Translator with the app
t = Translator()
t.init_app(app)
@app.route('/')
def home():
message = t.translate('request.schema.invalid_or_email_username')
return render_template('index.html', message=message)
return app
Normal Application Usage
# app.py
from flask import Flask, render_template
from flask_translator import Translator
app = Flask(__name__)
# Configure Flask Translator
app.config['DEFAULT_LANG'] = 'tr'
app.config['LANG_INITIALIZER'] = 'headers'
app.config['DEFAULT_MESSAGE'] = 'Process finished!'
app.config['TRANSLATIONS_PATH'] = 'translations'
# Initialize Flask Translator with the app
t = Translator(app)
@app.route('/')
def home():
message = t.translate('request.schema.invalid_or_email_username')
return render_template('index.html', message=message)
Configuration Options
DEFAULT_LANG
: The default language to use if no language is explicitly specified. Should be in ISO 639-1 code
standards. Default is 'en
' (English).
LANG_INITIALIZER
: Determines the method to initialize the language. Possible values are 'headers', 'cookies', '
sessions'. If none of these values are present, it falls back to the DEFAULT_LANG
value. Default is 'headers
'.
DEFAULT_MESSAGE
: The default message to display if a translation is not found for the given key. Default
is 'Process finished!
'.
TRANSLATIONS_PATH
: The path to the directory containing translation files. Default is 'translations
'.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distribution
File details
Details for the file Flask_Translator-0.1-py3-none-any.whl
.
File metadata
- Download URL: Flask_Translator-0.1-py3-none-any.whl
- Upload date:
- Size: 4.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5a5a41db867d48833354d333d12556e1e4645bafefad0e4e3f71e51e77967928 |
|
MD5 | fe5e3925405af9668df65ce1313bb25f |
|
BLAKE2b-256 | 1b2b8c8eec922c6a862377aa331764978883c46a402b3e1eeb4bad35483835b7 |