From 448ae1bce0a4e2eff4608f410c6aebf4da44271b Mon Sep 17 00:00:00 2001 From: Gergely Polonkai Date: Fri, 29 Jun 2018 14:55:57 +0200 Subject: [PATCH] Make the web app select the language based on browser values Language also can be selected using the `?l=langcode` query string. --- .gitignore | 4 +- app/__init__.py | 27 ++++++++- app/translations/hu/LC_MESSAGES/messages.po | 61 +++++++++++++++++++++ babel.cfg | 6 ++ 4 files changed, 96 insertions(+), 2 deletions(-) create mode 100644 app/translations/hu/LC_MESSAGES/messages.po create mode 100644 babel.cfg diff --git a/.gitignore b/.gitignore index 04b7699..382c831 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ __pycache__/ -app/local.db +/app/local.db +/messages.pot +/app/translations/*/LC_MESSAGES/*.mo diff --git a/app/__init__.py b/app/__init__.py index d22d209..2af7eef 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -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__) diff --git a/app/translations/hu/LC_MESSAGES/messages.po b/app/translations/hu/LC_MESSAGES/messages.po new file mode 100644 index 0000000..c7653a6 --- /dev/null +++ b/app/translations/hu/LC_MESSAGES/messages.po @@ -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 , 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 \n" +"Language: hu\n" +"Language-Team: hu \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ó" diff --git a/babel.cfg b/babel.cfg new file mode 100644 index 0000000..a61fc78 --- /dev/null +++ b/babel.cfg @@ -0,0 +1,6 @@ +[python: **/*.py] +encoding=utf-8 +[jinja2: **/templates/**.html] +encoding=utf-8 +extensions=jinja2.ext.autoescape +