Giter VIP home page Giter VIP logo

flask-language's Introduction

Flask-Language

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: http://flask.pocoo.org/snippets/128

Installation

Install the extension with with pipenv (recommended):

$ pipenv install flask-language

Or with pip:

$ pip install flask-language

Usage

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:

@lang.allowed_languages
def get_allowed_languages():
    return ['en', 'fr']

@lang.default_language
def get_default_language():
    return 'en'

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

@app.route('/api/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)

    lang.change_language(language)

    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.

Configuration

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)

Documentation

The Sphinx-compiled documentation is available on ReadTheDocs.

License

The MIT License (MIT)

Copyright (c) 2018 Romain Clement

flask-language's People

Contributors

rclement avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.