from os import system as console from configparser import ConfigParser from flask import ( Flask, g, request, render_template, ) # from flask_babel import Babel, _ translations_cache = {} def load_translations(lang): if lang not in translations_cache: try: config = ConfigParser() config.read(f'translations/{lang}.ini') translations_cache[lang] = dict(config['messages']) except: translations_cache[lang] = {} return translations_cache[lang] def get_locale(): return request.accept_languages.best_match(('en', 'ru'), 'en') app = Flask(__name__) # app.config['BABEL_DEFAULT_LOCALE'] = 'en' # app.config['BABEL_SUPPORTED_LOCALES'] = ['en', 'ru'] # app.jinja_env.add_extension("jinja2.ext.i18n") # app.jinja_env.globals.update(_=lambda x: load_translations("ru")[x]) # babel = Babel(app, locale_selector=get_locale) @app.before_request def before_request(): g.locale = get_locale() g.translations = load_translations(g.locale) @app.context_processor def inject_translations(): def translate(text, **kwargs): template = g.translations.get(text, f"${text}$") try: return template.format(**kwargs) except: return template return {'_': translate} if app.debug: console("sass static/style/main.scss static/style/main.css") @app.route("/") def index(): return render_template('index.html') @app.route("/host") def host(): return render_template('host.html') @app.route("/us") def us(): return render_template('us.html')