Skip to main content

Flask extension handling client-side language cookie

Project description


PyPI Version PyPI License PyPI Versions Build Status Coverage Status Documentation Status

Flask-Language is a Flask extension providing a simple mechanism to handle a client-side language cookie.

It is somewhat loosely based on this snippet:


Install the extension with with pipenv (recommended):

$ pipenv install flask-language

Or with pip:

$ pip install flask-language


Initialize the Flask-Language extension (also compatible with the Application Factories pattern):

from flask import Flask, jsonify
from flask_language import Language, current_language

app = Flask(__name__)
lang = Language(app)

Define the language hooks:

def get_allowed_languages():
    return ['en', 'fr']

def get_default_language():
    return 'en'

Define the desired end-points to retrieve and manipulate the current language:

def get_language():
    return jsonify({
        'language': str(current_language),

@app.route('/api/language', methods=['POST'])
def set_language():
    req = request.get_json()
    language = req.get('language', None)


    return jsonify({
        'language': str(current_language),

Before each request, Flask-Language will automatically determine the current language in the following order:

  1. The language cookie (if any and matching the allowed languages)
  2. The Accept-Language HTTP header (if any and matching the allowed languages)
  3. The provided default language

During each request context, the current language can be accessed using current_language.

After each request, the current language will be stored in the language cookie.


Flask-Language is configurable via the following configuration variables:

  • LANGUAGE_COOKIE_NAME: name for the cookie language (default: 'lang')
  • LANGUAGE_COOKIE_TIMEOUT: validity duration of the cookie language (default: datetime.timedelta(days=365))
  • LANGUAGE_COOKIE_DOMAIN: domain for the cookie language (default: None)
  • LANGUAGE_COOKIE_SECURE: set secure option for the cookie language (default: False)
  • LANGUAGE_COOKIE_HTTPONLY: set HTTP-only for the cookie language (default: False)


The Sphinx-compiled documentation is available on ReadTheDocs.


The MIT License (MIT)

Copyright (c) 2018 Romain Clement

Release History

0.1.0 (2018-03-07)

  • Initial release of Flask-Language

Release history Release notifications

This version
History Node


Download files

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

Filename, size & hash SHA256 hash help File type Python version Upload date
Flask-Language-0.1.0.tar.gz (9.2 kB) Copy SHA256 hash SHA256 Source None

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN SignalFx SignalFx Supporter DigiCert DigiCert EV certificate StatusPage StatusPage Status page