From 0fa100b0b790386d20ec95ae4dacc94735af697c Mon Sep 17 00:00:00 2001 From: Sweetbread Date: Sat, 9 Aug 2025 22:30:18 +0300 Subject: [PATCH] l10n: add sections --- app.py | 26 ++++++++++++++++---------- templates/index.html | 12 ++++++------ translations/en.ini | 26 ++++++++++++++------------ translations/ru.ini | 26 ++++++++++++++------------ 4 files changed, 50 insertions(+), 40 deletions(-) diff --git a/app.py b/app.py index 24732cb..d4f0181 100644 --- a/app.py +++ b/app.py @@ -6,19 +6,22 @@ from flask import ( request, render_template, ) -# from flask_babel import Babel, _ translations_cache = {} def load_translations(lang): if lang not in translations_cache: + translations_cache[lang] = {} + try: config = ConfigParser() config.read(f'translations/{lang}.ini') - translations_cache[lang] = dict(config['messages']) + for section in config.sections(): + translations_cache[lang][section] = dict(config.items(section)) except: - translations_cache[lang] = {} + pass + return translations_cache[lang] @@ -26,12 +29,6 @@ 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 @@ -43,11 +40,20 @@ def before_request(): @app.context_processor def inject_translations(): def translate(text, **kwargs): - template = g.translations.get(text, f"${text}$") + if ":" in text: + section, key = text.split(":", 1) + else: + section, key = "messages", text + + template = g.translations \ + .get(section, {}) \ + .get(key, f"${section}: {key}$") + try: return template.format(**kwargs) except: return template + return {'_': translate} diff --git a/templates/index.html b/templates/index.html index ac92fcf..aea7517 100644 --- a/templates/index.html +++ b/templates/index.html @@ -11,20 +11,20 @@ Sharkey -

{{ _('sharkey_descr') }}

+

{{ _('index.descr:sharkey') }}

Gitea
-

{{ _('gitea_descr') }}

+

{{ _('index.descr:gitea') }}

-

Matrix — {{ _('matrix_descr') }}

-

Copyparty — {{ _('copyparty_descr') }}

-

4get — {{ _('4get_descr') }}

+

Matrix — {{ _('index.descr:matrix') }}

+

Copyparty — {{ _('index.descr:copyparty') }}

+

4get — {{ _('index.descr:4get') }}

@@ -44,7 +44,7 @@
{{ - _('bottom_text', + _('index:bottom_text', glitchtip='GlitchTip', baikal='Baikal', freshrss='FreshRSS', diff --git a/translations/en.ini b/translations/en.ini index d383e0b..7fdc490 100644 --- a/translations/en.ini +++ b/translations/en.ini @@ -5,17 +5,19 @@ contact us = Contact us about us = About us about host = About host -hoster = Hoster - -hoster_descr = Not bad, not expencive. There were network troubles in the past - -specifications = Specifications - - -sharkey_descr = Federative microbloging service under ActivityPub protocol -gitea_descr = Opensource selfhosted git repository hosting -matrix_descr = federative messenger -copyparty_descr = cloud file storage -4get_descr = proxy search engine +[index] bottom_text = Also we have {glitchtip}, {baikal} and {freshrss} for members of our squad! + +[index.descr] +sharkey = Federative microbloging service under ActivityPub protocol +gitea = Opensource selfhosted git repository hosting +matrix = federative messenger +copyparty = cloud file storage +4get = proxy search engine + + +[host] +hoster = Hoster +specifications = Specifications +hoster_descr = Not bad, not expencive. There were network troubles in the past diff --git a/translations/ru.ini b/translations/ru.ini index e2780d7..97f777e 100644 --- a/translations/ru.ini +++ b/translations/ru.ini @@ -5,17 +5,19 @@ contact us = Для связи about us = О нас about host = О хосте -hoster = Хостер - -hoster_descr = Неплохой, недорогой. В прошлом были проблемы с сетью - -specifications = Характеристики - - -sharkey_descr = Федеративная микроблогинговая система поверх протокола ActivityPub -gitea_descr = Selfhosted хранилище git-репозиториев со свободным исходным кодом -matrix_descr = федеративный мессенджер -copyparty_descr = облачное хранилище файлов -4get_descr = прокси-поисковик +[index] bottom_text = Ещё у нас есть {glitchtip}, {baikal} и {freshrss} для участников нашей группы! + +[index.descr] +sharkey = Федеративная микроблогинговая система поверх протокола ActivityPub +gitea = Selfhosted хранилище git-репозиториев со свободным исходным кодом +matrix = федеративный мессенджер +copyparty = облачное хранилище файлов +4get = прокси-поисковик + + +[host] +hoster = Хостер +specifications = Характеристики +hoster_descr = Неплохой, недорогой. В прошлом были проблемы с сетью