Skip to main content

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

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 Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

Flask_Translator-0.1-py3-none-any.whl (4.7 kB view hashes)

Uploaded Python 3

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