Simple translation support for Flask-SQLAlchemy based database tables.
Project description
Flask-TransAlchemy
Simple translation support for Flask-SQLAlchemy based database tables.
Usage
Initialize the TransAlchemy extension with the Flask and Flask-SQLAlchemy instances.
from flask import Flask
from flask_transalchemy import TransAlchemy
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
db = SQLAlchemy(app)
translations = TransAlchemy(app, db)
if __name__ == '__main__':
app.run()
To add translatable abilities to your models, use the
TranslatableMixin
class. The translatable columns should be defined in the
translatable_columns
class attribute.
from flask_transalchemy import TranslatableMixin
class SomeModel(db.Model, TranslatableMixin):
translatable_columns = ['text_field']
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
text_field = db.Column(db.String(40))
not_translatable_text_field = db.Column(db.String(10))
Translations not bind to any model can also be utilized with the set_label()
and get_label()
methods.
with app.app_context():
set_label('hello', 'Welcome {name}!', language='en')
set_label('hello', 'Willkommen {name}!', language='de')
set_label('hello', 'Bienvenue {name}!', language='fr')
set_label('hello', 'Bienvenido {name}!', language='es')
@app.route('/<name>')
def hello(name: str):
return get_label('hello').format(name=name)
If the label_route
parameter was specified at initialization, the label
translations are accessible over the provided route. Visiting
/<label_route>/hello?name=John
would return Welcome John!
in the previous
case. This might be useful for frontend or client application development.
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 Distribution
File details
Details for the file Flask-TransAlchemy-0.1.tar.gz
.
File metadata
- Download URL: Flask-TransAlchemy-0.1.tar.gz
- Upload date:
- Size: 3.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.38.0 CPython/3.7.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f807055ac7245e8f341cd5f76a6d584baf90f1143308ac0d0840115349423010 |
|
MD5 | ab3d5bc926984879cd471fd2ddcb06b8 |
|
BLAKE2b-256 | 98aedb25a4247c00fa0ecf307a4e7edc1528bd63c52e15759c2e1f2ce83c37ba |