Make the web app select the language based on browser values

Language also can be selected using the `?l=langcode` query string.
This commit is contained in:
Gergely Polonkai 2018-06-29 14:55:57 +02:00
parent bc47310cfb
commit 448ae1bce0
4 changed files with 96 additions and 2 deletions

4
.gitignore vendored
View File

@ -1,2 +1,4 @@
__pycache__/
app/local.db
/app/local.db
/messages.pot
/app/translations/*/LC_MESSAGES/*.mo

View File

@ -17,10 +17,32 @@
import os
from flask import Flask, current_app, redirect, render_template, url_for
from flask_babel import Babel
from flask_babel import Babel, get_locale as babel_get_locale
from flask_security import SQLAlchemyUserDatastore, current_user
def get_locale():
"""Locale selector
Selects the best locale based on values sent by the browser.
"""
from flask import request
supported_languages = ['en', 'hu']
if 'l' in request.args and request.args['l'].lower() in supported_languages:
return request.args['l'].lower()
return request.accept_languages.best_match(supported_languages)
def template_vars():
return {
'lang': babel_get_locale().language,
}
class CalendarSocialApp(Flask):
def __init__(self, name, config=None):
from app.models import db, User, Role
@ -34,10 +56,13 @@ class CalendarSocialApp(Flask):
self.config['SECURITY_USER_IDENTITY_ATTRIBUTES'] = ('username', 'email')
db.init_app(self)
babel = Babel(app=self)
babel.localeselector(get_locale)
user_store = SQLAlchemyUserDatastore(db, User, Role)
security.init_app(self, datastore=user_store)
self.context_processor(template_vars)
app = CalendarSocialApp(__name__)

View File

@ -0,0 +1,61 @@
# Hungarian translations for PROJECT.
# Copyright (C) 2018 ORGANIZATION
# This file is distributed under the same license as the PROJECT project.
# FIRST AUTHOR <EMAIL@ADDRESS>, 2018.
#
msgid ""
msgstr ""
"Project-Id-Version: 0.1\n"
"Report-Msgid-Bugs-To: gergely@polonkai.eu\n"
"POT-Creation-Date: 2018-06-29 14:14+0200\n"
"PO-Revision-Date: 2018-06-29 14:26+0200\n"
"Last-Translator: Gergely Polonkai <gergely@polonkai.eu>\n"
"Language: hu\n"
"Language-Team: hu <gergely@polonkai.eu>\n"
"Plural-Forms: nplurals=1; plural=0\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=utf-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Generated-By: Babel 2.6.0\n"
#: app/forms.py:8
msgid "Username"
msgstr "Felhasználónév"
#: app/forms.py:9
msgid "Email address"
msgstr "E-mail cím"
#: app/forms.py:10
msgid "Password"
msgstr "Jelszó"
#: app/forms.py:11
msgid "Password, once more"
msgstr "Jelszó még egszer"
#: app/forms.py:15
msgid "The two passwords must match!"
msgstr "A két jelszónak egyeznie kell!"
#: app/templates/base.html:21
#, python-format
msgid "Logged in as %(username)s"
msgstr "Bejelentkezve %(username)s néven"
#: app/templates/base.html:25
msgid "Login"
msgstr "Bejelentkezés"
#: app/templates/base.html:27
msgid "Logout"
msgstr "Kijelentkezés"
#: app/templates/index.html:4
#, python-format
msgid "Welcome to Calendar.social, %(username)s!"
msgstr "Üdv a Calendar.social-ben, %(username)s!"
#: app/templates/registration.html:27
msgid "Register"
msgstr "Regisztráció"

6
babel.cfg Normal file
View File

@ -0,0 +1,6 @@
[python: **/*.py]
encoding=utf-8
[jinja2: **/templates/**.html]
encoding=utf-8
extensions=jinja2.ext.autoescape