Example #1
    def test_translate(self):
        app = Flask(__name__)
        app.config["DEFAULT_LOCALE"] = "zh_CN"

        with app.test_request_context():
            assert translate("I love you") == to_unicode("我爱你")
Example #2
# -*- coding: utf-8 -*-

from flask import Flask, request, render_template, g, session, redirect, current_app
from flask_locale import Locale, _

app = Flask(__name__)
# DEFAULT_LOCALE is the language used for keys ins translation files:
app.config["DEFAULT_LOCALE"] = "tr_TR"
app.config["LOCALE_PATH"] = "translations"
app.config["SECRET_KEY"] = "translations****"

locale = Locale(app)

def get_locale():
    # if a user is logged in, use the locale from the session
    # define a default value instead of None to set it to specific locale if not setting is found.
    locale_code = session.get("locale", None)
    if locale_code is not None:
        current_app.logger.info("Locale is: %s" % locale_code)
        return locale_code

    # otherwise try to guess the language from the user accept
    # header the browser transmits.  We support tr/fr/en in this
    # example.  The best match wins.
    locale_code = request.accept_languages.best_match(["tr_TR", "fr_FR", "en_US"])
    current_app.logger.info("Locale match: %s" % locale_code)
    return locale_code