From e768a6efe92fd418dfe29698825b3eac1b802d3a Mon Sep 17 00:00:00 2001 From: Sweetbread Date: Fri, 22 Aug 2025 15:40:56 +0300 Subject: [PATCH] feat: add _ function --- app.py | 65 ++++++++++++++++++++----------- locales/es.ini | 2 +- templates/download.html | 26 ++++++------- templates/tmpl/_article.htm | 38 +++++++++--------- templates/tmpl/_footer.htm | 2 +- templates/tmpl/_git.htm | 4 +- templates/tmpl/_header.htm | 4 +- templates/tmpl/_lang-dropdown.htm | 5 +-- templates/tmpl/_menu.htm | 18 ++++----- 9 files changed, 90 insertions(+), 74 deletions(-) diff --git a/app.py b/app.py index 33bd4b0..b55caa1 100644 --- a/app.py +++ b/app.py @@ -8,6 +8,7 @@ from flask import ( render_template, request, url_for, + g, Response ) @@ -22,37 +23,34 @@ app = Flask(__name__) def load_all_locales(): - cp = ConfigParser() - locales_list = [] - locales_dict = {} + translations = {} locales_dir = "locales" + locales_code_default = ('en', 'ru', 'es') + locales_code_extra = [] + locales_code = () + for filename in listdir(locales_dir): if filename.endswith(".ini"): + cp = ConfigParser() lang = path.splitext(filename)[0] with open(path.join(locales_dir, filename), encoding="utf-8") as f: cp.read_file(f) - locales_dict[lang] = { + + if lang not in locales_code_default: + locales_code_extra.append(lang) + + translations[lang] = { section: dict(cp[section]) for section in cp.sections() } - for code, data in locales_dict.items(): - full_name = data.get("title", {}).get("language", code) - locales_list.append({"code": code, "name": full_name}) + locales_code = locales_code_default + tuple(sorted(locales_code_extra)) + locales_name = {l: translations[l]['title']['language'] for l in locales_code} - priority = ["en", "ru", "es"] - locales_list.sort( - key=lambda loc: (0, priority.index(loc["code"])) - if loc["code"] in priority - else (1, loc["code"]) - ) - - locales_code = [loc["code"] for loc in locales_list] - - return locales_list, locales_dict, locales_code + return translations, locales_name, locales_code -locales_list, locales_dict, locales_code = load_all_locales() +translations, locales_name, locales_code = load_all_locales() # ---------- HELPER FUNCTIONS ------------------------------------------------ @@ -63,19 +61,40 @@ def get_best_lang(): def render_localized_template(lang, template_name): - if lang not in locales_dict: + if lang not in locales_code: return redirect(url_for("index", lang=get_best_lang())) return render_template( template_name, - loc_list=locales_list, - locale=locales_dict[lang], - lang=lang, year=date.today().year, - current=request.endpoint, ) +@app.before_request +def before_request(): + if args := request.view_args: + g.locale = args.get('lang', 'en') + g.translations = translations.get(g.locale, get_best_lang()) + g.locales_name = locales_name + + +@app.context_processor +def inject_translations(): + def translate(text, **kwargs): + section, key = text.split(":", 1) + + template = g.translations \ + .get(section, {}) \ + .get(key, f"${section}: {key}$") + + try: + return template.format(**kwargs) + except: + return template + + return {'_': translate} + + # ---------- MAIN PAGES ------------------------------------------------------ diff --git a/locales/es.ini b/locales/es.ini index 15f8d34..8b34dce 100644 --- a/locales/es.ini +++ b/locales/es.ini @@ -1,5 +1,5 @@ [title] -language = Spanish +language = Español index = KolibriOS download = KolibriOS - Descargar diff --git a/templates/download.html b/templates/download.html index e2e58f6..9b594cd 100644 --- a/templates/download.html +++ b/templates/download.html @@ -10,7 +10,7 @@ {% include 'tmpl/_menu.htm' %}
-

{{ locale['downloads']['header'] }}

+

{{ _('downloads:header') }}

{% for ext, alt in ( ('img', 'floppy'), @@ -23,7 +23,7 @@ {{ alt }}
- {{ locale['downloads']['%s-descr' % ext] }} + {{ _('downloads:%s-descr' % ext) }} {% if ext == 'raw' %} BETA {% endif %} @@ -51,28 +51,28 @@
- {{ locale['downloads']['download_choice'] }} + onclick="alert('{{ _('downloads:download_help') }}');"> + {{ _('downloads:download_choice') }}
- {{ locale['downloads']['prev_rev'] }} + {{ _('downloads:prev_rev') }} - {{ locale['downloads']['all_rev'] }} + {{ _('downloads:all_rev') }}

- {{ locale['downloads']['download_description[0]'] }} + {{ _('downloads:download_description[0]') }} 7zip. - {{ locale['title']['index'] }} {{ locale['downloads']['download_description[1]'] }} - GPLv2, - {{ locale['downloads']['download_description[2]'] }} - {{ locale['downloads']['download_description[3]'] }}. + {{ _('title:index') }} {{ _('downloads:download_description[1]') }} + GPLv2, + {{ _('downloads:download_description[2]') }} + {{ _('downloads:download_description[3]') }}.

-

{{ locale['screenshots']['header'] }}

+

{{ _('screenshots:header') }}

@@ -81,7 +81,7 @@ id="slide{{ i }}" src="{{ url_for('static', filename='img/screenshots/%d.png' % i ) }}" {% if i == 1 %}class="visible"{% endif %} - alt="{{ locale['screenshots']['%d' % i] }}" + alt="{{ _('screenshots:%d' % i) }}" > {% endfor %}
diff --git a/templates/tmpl/_article.htm b/templates/tmpl/_article.htm index 7e8333b..ae5e4a6 100644 --- a/templates/tmpl/_article.htm +++ b/templates/tmpl/_article.htm @@ -1,43 +1,41 @@

- {{ locale['menu']['kolibrios'] }} - {{ locale['article']['p1[0]'] }} + {{ _('menu:kolibrios') }} + {{ _('article:p1[0]') }} - {{- locale['article']['p1[1]'] -}} + {{- _('article:p1[1]') -}} - {{ locale['article']['p1[2]'] }} + {{ _('article:p1[2]') }}

- {{ locale['article']['p2[0]'] }} - {{ locale['menu']['kolibrios'] }} - {{ locale['article']['p2[1]'] }} + {{ _('article:p2[0]') }} + {{ _('menu:kolibrios') }} + {{ _('article:p2[1]') }} FASM - {%- set extra = locale.article.get('p2[11]', '').strip() -%} - {%- if extra -%} {{ " " + extra }} {%- endif -%}! - {{ locale.article['p2[2]'] }} - {{ locale['menu']['kolibrios'] }} - {{ locale['article']['p2[3]'] }} + {{ _('article:p2[2]') }} + {{ _('menu:kolibrios') }} + {{ _('article:p2[3]') }}

- {{ locale['menu']['kolibrios'] }} - {{ locale['article']['p3[0]'] }} + {{ _('menu:kolibrios') }} + {{ _('article:p3[0]') }} - {{- locale['article']['p3[1]'] -}} + {{- _('article:p3[1]') -}} - {{ locale['article']['p3[2]'] }} + {{ _('article:p3[2]') }} - {{- locale['article']['p3[3]'] -}} + {{- _('article:p3[3]') -}} - {{ locale['article']['p3[4]'] }} + {{ _('article:p3[4]') }}

- {{ locale['article']['p_subscription[0]'] }} + {{ _('article:p_subscription[0]') }}
- {{ locale['article']['p_subscription[1]'] }} + {{ _('article:p_subscription[1]') }}

diff --git a/templates/tmpl/_footer.htm b/templates/tmpl/_footer.htm index 4039ddd..a66f88f 100644 --- a/templates/tmpl/_footer.htm +++ b/templates/tmpl/_footer.htm @@ -2,6 +2,6 @@ KolibriOS

© 2004 – {{ year }}
- {{ locale['footer']['team'] }} + {{ _('footer:team') }}

\ No newline at end of file diff --git a/templates/tmpl/_git.htm b/templates/tmpl/_git.htm index 482505d..d244f6b 100644 --- a/templates/tmpl/_git.htm +++ b/templates/tmpl/_git.htm @@ -5,8 +5,8 @@ KolibriOS -

{{ locale['git']['header'] }}

-

{{ locale['git']['text'] }}

+

{{ _('git:header') }}

+

{{ _('git:text') }}

diff --git a/templates/tmpl/_header.htm b/templates/tmpl/_header.htm index ceea7f0..ea45c14 100644 --- a/templates/tmpl/_header.htm +++ b/templates/tmpl/_header.htm @@ -1,8 +1,8 @@ - {{ locale['title'][request.url_rule.endpoint] }} + {{ _('title:%s' % request.url_rule.endpoint) }} - + diff --git a/templates/tmpl/_lang-dropdown.htm b/templates/tmpl/_lang-dropdown.htm index 49d95b3..c8d046e 100644 --- a/templates/tmpl/_lang-dropdown.htm +++ b/templates/tmpl/_lang-dropdown.htm @@ -1,8 +1,7 @@