From 6cf00562e4408d6a35e7887a6d4ebd36e70f309b Mon Sep 17 00:00:00 2001 From: vadik likholetov Date: Thu, 16 Nov 2023 10:59:39 +0200 Subject: [PATCH] Initial internationalisation implementation --- app.py | 22 +- babel.cfg | 3 + requirements.txt | 2 + templates/base.html | 34 +- templates/sar_details.html | 64 +- translations/ee/LC_MESSAGES/messages.po | 795 +++++++++++++++++++++++ translations/en/LC_MESSAGES/messages.po | 795 +++++++++++++++++++++++ translations/fi/LC_MESSAGES/messages.po | 795 +++++++++++++++++++++++ translations/lt/LC_MESSAGES/messages.po | 809 ++++++++++++++++++++++++ translations/lv/LC_MESSAGES/messages.po | 809 ++++++++++++++++++++++++ translations/ru/LC_MESSAGES/messages.po | 21 + translations/se/LC_MESSAGES/messages.po | 795 +++++++++++++++++++++++ 12 files changed, 4907 insertions(+), 37 deletions(-) create mode 100644 babel.cfg create mode 100644 translations/ee/LC_MESSAGES/messages.po create mode 100644 translations/en/LC_MESSAGES/messages.po create mode 100644 translations/fi/LC_MESSAGES/messages.po create mode 100644 translations/lt/LC_MESSAGES/messages.po create mode 100644 translations/lv/LC_MESSAGES/messages.po create mode 100644 translations/ru/LC_MESSAGES/messages.po create mode 100644 translations/se/LC_MESSAGES/messages.po diff --git a/app.py b/app.py index c9f7627..806c078 100644 --- a/app.py +++ b/app.py @@ -1,9 +1,11 @@ -from flask import Flask, redirect, url_for +from flask import Flask, redirect, url_for, request, session from flask_sqlalchemy import SQLAlchemy from flask_migrate import Migrate from flask_login import LoginManager +from flask_babel import Babel from sqlalchemy import MetaData + convention = { "ix": 'ix_%(column_0_label)s', "uq": "uq_%(table_name)s_%(column_0_name)s", @@ -15,14 +17,24 @@ convention = { metadata = MetaData(naming_convention=convention) +def get_locale(): + return session.get('language', request.accept_languages.best_match(['en', 'ru', 'et', 'lv', 'fi'])) + + + app = Flask(__name__) app.config['SECRET_KEY'] = 'secret_key' #app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///db.sqlite3' app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+mysqlconnector://sarbaseuser:password@localhost/sarbaseapp' +app.config['BABEL_DEFAULT_LOCALE'] = 'en' +babel= Babel(app) +babel.init_app(app, locale_selector=get_locale) db = SQLAlchemy(app, metadata=metadata) migrate = Migrate(app, db) login_manager = LoginManager(app) + + import models import admin import login @@ -35,5 +47,13 @@ def hello_world(): # put application's code here return redirect(url_for('dashboard')) +@app.route('/set_language/') +def set_language(lang_code): + session['language'] = lang_code + return redirect(request.referrer or url_for('dashboard')) + + + + if __name__ == '__main__': app.run() diff --git a/babel.cfg b/babel.cfg new file mode 100644 index 0000000..4dd1fa8 --- /dev/null +++ b/babel.cfg @@ -0,0 +1,3 @@ +[python: **.py] +[jinja2: **/templates/**.html] +;extensions=jinja2.ext.auto escape,jinja2.ext.with_ \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index 47abf6d..66d1c53 100644 --- a/requirements.txt +++ b/requirements.txt @@ -10,3 +10,5 @@ Werkzeug==3.0.1 mysql_connector_python==8.2.0 gpxpy==1.6.1 geopy==2.4.0 +flask_babel==4.0.0 +``` \ No newline at end of file diff --git a/templates/base.html b/templates/base.html index f05d6b7..4b59f57 100644 --- a/templates/base.html +++ b/templates/base.html @@ -2,14 +2,21 @@ + + + {% block title %}Default Title{% endblock %}