Skip to main content

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

This version

0.1

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

Flask-TransAlchemy-0.1.tar.gz (3.4 kB view hashes)

Uploaded Source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page