From f3eeff59394c4ca528b6e8e12c4d3aba8db99d79 Mon Sep 17 00:00:00 2001 From: Burer Date: Tue, 25 Mar 2025 14:45:21 +0200 Subject: [PATCH] feat/flask: Reworked locales system to use separate .ini file for each language --- app.py | 30 ++++-- locale.ini | 156 ------------------------------ locales/de.ini | 60 ++++++++++++ locales/en.ini | 58 +++++++++++ locales/ru.ini | 58 +++++++++++ templates/tmpl/_footer.htm | 2 +- templates/tmpl/_git.htm | 4 +- templates/tmpl/_header.htm | 4 +- templates/tmpl/_lang-dropdown.htm | 4 +- templates/tmpl/_menu.htm | 8 +- templates/tmpl/download.htm | 24 ++--- 11 files changed, 222 insertions(+), 186 deletions(-) delete mode 100644 locale.ini create mode 100644 locales/de.ini create mode 100644 locales/en.ini create mode 100644 locales/ru.ini diff --git a/app.py b/app.py index 9eb8b76..fa985ff 100644 --- a/app.py +++ b/app.py @@ -1,14 +1,28 @@ -from flask import Flask, render_template, request, url_for, redirect +import os from datetime import date from configparser import ConfigParser -locale = ConfigParser() -with open('locale.ini', encoding='utf-8') as f: - locale.read_file(f) +from flask import Flask, render_template, url_for, redirect + +cp = ConfigParser() app = Flask(__name__) -print(locale) + +def load_all_locales(): + locales = {} + locales_dir = 'locales' + for filename in os.listdir(locales_dir): + if filename.endswith('.ini'): + lang = os.path.splitext(filename)[0] + cp = ConfigParser() + with open(os.path.join(locales_dir, filename), encoding='utf-8') as f: + cp.read_file(f) + locales[lang] = {section: dict(cp[section]) for section in cp.sections()} + return locales + +locales = load_all_locales() + @app.route("/") def home(): @@ -16,11 +30,13 @@ def home(): @app.route("/") def index(lang): - return render_template(f'{lang}.html', locale=locale, lang=lang, year=date.today().year) # TODO: Check for vulnerability + return render_template(f'{lang}.html', locale=locales[lang], lang=lang, year=date.today().year) # TODO: Check for vulnerability @app.route("//download") def download_page(lang): - return render_template('tmpl/download.htm', locale=locale, lang=lang) + return render_template('tmpl/download.htm', locale=locales[lang], lang=lang, year=date.today().year) + if __name__ == "__main__": + app.run(host="0.0.0.0", debug=True) diff --git a/locale.ini b/locale.ini deleted file mode 100644 index 0dce579..0000000 --- a/locale.ini +++ /dev/null @@ -1,156 +0,0 @@ -[ru] -language = Русский - -kolibrios = КолибриОС -download = Скачать -downloading = Загрузка - -forum = Форум -wiki = Вики -description = Официальный сайт KolibriOS -team = Команда КолибриОС - -git[1] = КолибриОС перешла на Git! -git[2] = Ознакомьтесь с нашей новой, удобной для разработчиков инфраструктурой - -img-descr = Образ дискеты -iso-descr = Образ LiveCD -distr-descr = Универсальный образ Flash/Multi-boot -raw-descr = Гибридный UEFI/BIOS образ - -prev_rev = Предыдущие выпуски -all_rev = Все ночные сборки - -screenshots = Скриншоты - -download_choice = Какой выбрать? -download_help = Для новичка лучше всего подойдет LiveCD.\n \ - \n\ - По сравнению с LiveCD преимущество универсального образа в том, что вы можете сохранить изменения, сделанные в KolibriOS.\n\ - \n\ - Гибридный образ включает поддержку технологии UEFI, которая используется для загрузки системы на новых компьютерах и ноутбуках. -download_description[0] = На этой странице вы можете скачать ночные сборки дистрибутива. - Это означает, что они всегда содержат последние изменения в системе и, - следовательно, могут быть нестабильными. Все файлы сжаты с помощью -download_description[1] = распространяется под лицензией -download_description[2] = а исходный код доступен на нашем -download_description[3] = Git-сервере - - -[ru.title] -index = KolibriOS -download_page = KolibriOS - Загрузка - -[ru.screenshots] -1 = Рабочий стол KolibriOS -2 = Демки -3 = Файловые менеджеры -4 = Сетевые приложения -5 = Игры -6 = Инструменты разработчика - - - -[en] -language = English - -kolibrios = KolibriOS -download = Download -downloading = Downloads - -forum = Forum -wiki = Wiki -description = -team = KolibriOS Team - -git[1] = KolibriOS moved to Git! -git[2] = Check our new developers-friendly infrastructure - -img-descr = Floppy disk image -iso-descr = LiveCD image -distr-descr = Universal Flash/Multi-boot image -raw-descr = Hybrid UEFI/BIOS image - -prev_rev = Previous releases -all_rev = All nightly builds - -screenshots = Screenshots - -download_choice = Which to choose? -download_help = For a beginner, the LiveCD is best.\n\ - \n\ - Compared to a LiveCD, the advantage of a universal image is that you can save changes made in KolibriOS.\n\ - \n\ - Hybrid image includes support for UEFI technology, which is used to boot the system on new computers and laptops. -download_description[0] = On this page you can download the nightly builds distribution, which means - that they always contain the most recent changes in the system and can, - therefore, be unstable. All files are compressed with -download_description[1] = is distributed under -download_description[2] = license, its source code is available on our -download_description[3] = Git server - - -[en.title] -index = KolibriOS -download_page = KolibriOS - Download - -[en.screenshots] -1 = KolibriOS desktop -2 = Demos -3 = File managers -4 = Network programs -5 = Games -6 = Developer and debug tools - - - -[de] -language = Deutsch - -kolibrios = KolibriOS -download = Herunterladen -downloading = Herunterladen - -forum = Forum -wiki = Wiki -description = -team = KolibriOS Team - -git[1] = KolibriOS ist zu Git gewechselt! -git[2] = Schau dir unsere neue entwicklerfreundliche Infrastruktur an - -img-descr = Disketten-Image -iso-descr = LiveCD-Abbild -distr-descr = Universal Flash/Multi-Boot-Abbild -raw-descr = Hybrides UEFI/BIOS-Abbild - -prev_rev = Frühere Versionen -all_rev = Alle nächtlichen Builds - -screenshots = Bildschirmfotos - -download_choice = Was soll ich wählen? -download_help = For a beginner, the LiveCD is best.\n\ - \n\ - Compared to a LiveCD, the advantage of a universal image is that you can save changes made in KolibriOS.\n\ - \n\ - Hybrid image includes support for UEFI technology, which is used to boot the system on new computers and laptops. -download_description[0] = Auf dieser Seite können Sie die nächtlichen Build-Distributionen herunterladen, - was bedeutet, dass sie stets die neuesten Änderungen am System enthalten und - daher instabil sein können. Alle Dateien sind mit -download_description[1] = komprimiert. -download_description[2] = wird unter der -download_description[3] = - - -[de.title] -index = KolibriOS -download_page = KolibriOS - Herunterladen - -[de.screenshots] -1 = KolibriOS Arbeitsoberfläche -2 = Demos -3 = Datei-Manager -4 = Netzwerk-Programme -5 = Spiele -6 = Entwickler- und Debug-Tools diff --git a/locales/de.ini b/locales/de.ini new file mode 100644 index 0000000..e78073e --- /dev/null +++ b/locales/de.ini @@ -0,0 +1,60 @@ +[title] +language = Deutsch +index = KolibriOS +download_page = KolibriOS - Herunterladen + +[header] +index = KolibriOS offizielle seite +download = KolibriOS Herunterladen +description = KolibriOS offizielle seite + +[menu] +kolibrios = KolibriOS +download = Herunterladen +forum = Forum +wiki = Wiki +git = Git + +[git] +header = KolibriOS ist zu Git gewechselt! +text = Schau dir unsere neue entwicklerfreundliche Infrastruktur an + +[downloads] +header = Herunterladen + +img-descr = Disketten-Image +iso-descr = LiveCD-Abbild +distr-descr = Universal Flash/Multi-Boot-Abbild +raw-descr = Hybrides UEFI/BIOS-Abbild + +prev_rev = Frühere Versionen +all_rev = Alle nächtlichen Builds + +screenshots = Bildschirmfotos + +download_choice = Was soll ich wählen? +download_help = Für einen Einsteiger ist die LiveCD am besten geeignet.\n\ + \n\ + Im Vergleich zu einer LiveCD hat ein universelles Image den Vorteil, dass Sie die in KolibriOS vorgenommenen Änderungen speichern können.\n\ + \n\ + Das Hybrid-Image enthält Unterstützung für die UEFI-Technologie, die zum Booten des Systems auf neuen Computern und Laptops verwendet wird. + +download_description[0] = Auf dieser Seite können Sie die nächtlichen Build-Distributionen herunterladen, + was bedeutet, dass sie stets die neuesten Änderungen am System enthalten und + daher instabil sein können. Alle Dateien sind mit +download_description[1] = komprimiert. +download_description[2] = wird unter der +download_description[3] = Git-Server + +[screenshots] +header = Bildschirmfotos + +1 = KolibriOS Arbeitsoberfläche +2 = Demos +3 = Datei-Manager +4 = Netzwerk-Programme +5 = Spiele +6 = Entwickler- und Debug-Tools + +[footer] +team = KolibriOS-Team diff --git a/locales/en.ini b/locales/en.ini new file mode 100644 index 0000000..b1a2cba --- /dev/null +++ b/locales/en.ini @@ -0,0 +1,58 @@ +[title] +language = English +index = KolibriOS +download_page = KolibriOS - Download + +[header] +index = KolibriOS official site +download = KolibriOS downloads +description = KolibriOS official site + +[menu] +kolibrios = KolibriOS +download = Download +forum = Forum +wiki = Wiki +git = Git + +[git] +header = KolibriOS moved to Git! +text = Check our new developers-friendly infrastructure + +[downloads] +header = Downloads + +img-descr = Floppy disk image +iso-descr = LiveCD image +distr-descr = Universal Flash/Multi-boot image +raw-descr = Hybrid UEFI/BIOS image + +prev_rev = Previous releases +all_rev = All nightly builds + +download_choice = Which to choose? +download_help = For a beginner, the LiveCD is best.\n\ + \n\ + Compared to a LiveCD, the advantage of a universal image is that you can save changes made in KolibriOS.\n\ + \n\ + Hybrid image includes support for UEFI technology, which is used to boot the system on new computers and laptops. + +download_description[0] = On this page you can download the nightly builds distribution, which means + that they always contain the most recent changes in the system and can, + therefore, be unstable. All files are compressed with +download_description[1] = is distributed under +download_description[2] = license, its source code is available on our +download_description[3] = Git server + +[screenshots] +header = Screenshots + +1 = KolibriOS desktop +2 = Demos +3 = File managers +4 = Network programs +5 = Games +6 = Developer and debug tools + +[footer] +team = KolibriOS Team diff --git a/locales/ru.ini b/locales/ru.ini new file mode 100644 index 0000000..9e0f37e --- /dev/null +++ b/locales/ru.ini @@ -0,0 +1,58 @@ +[title] +language = Русский +index = КолибриОС +download_page = КолибриОС - Скачать + +[header] +index = Официальный сайт KolibriOS +download = Скачать КолибриОС +description = Официальный сайт KolibriOS + +[menu] +kolibrios = КолибриОС +download = Скачать +forum = Форум +wiki = Вики +git = Гит + +[git] +header = КолибриОС перешла на Гит! +text = Ознакомьтесь с нашей новой, удобной для разработчиков инфраструктурой + +[downloads] +header = Скачать + +img-descr = Образ дискеты +iso-descr = Образ LiveCD +distr-descr = Универсальный образ Flash/Multi-boot +raw-descr = Гибридный UEFI/BIOS образ + +prev_rev = Предыдущие выпуски +all_rev = Все ночные сборки + +download_choice = Какой выбрать? +download_help = Для новичка лучше всего подойдет LiveCD.\n \ + \n\ + По сравнению с LiveCD преимущество универсального образа в том, что вы можете сохранить изменения, сделанные в КолибриОС.\n\ + \n\ + Гибридный образ включает поддержку технологии UEFI, которая используется для загрузки системы на новых компьютерах и ноутбуках. + +download_description[0] = На этой странице вы можете скачать ночные сборки дистрибутива. + Это означает, что они всегда содержат последние изменения в системе и, + следовательно, могут быть нестабильными. Все файлы сжаты с помощью +download_description[1] = распространяется под лицензией +download_description[2] = а исходный код доступен на нашем +download_description[3] = Git-сервере + +[screenshots] +header = Скриншоты + +1 = Рабочий стол КолибриОС +2 = Демки +3 = Файловые менеджеры +4 = Сетевые приложения +5 = Игры +6 = Инструменты разработчика + +[footer] +team = Команда КолибриОС diff --git a/templates/tmpl/_footer.htm b/templates/tmpl/_footer.htm index 2cdf691..3162c3c 100644 --- a/templates/tmpl/_footer.htm +++ b/templates/tmpl/_footer.htm @@ -2,6 +2,6 @@

© 2004 – {{ year }}
- {{ locale[lang]['team'] }} + {{ locale['footer']['team'] }}

diff --git a/templates/tmpl/_git.htm b/templates/tmpl/_git.htm index 4225727..a11a919 100644 --- a/templates/tmpl/_git.htm +++ b/templates/tmpl/_git.htm @@ -6,8 +6,8 @@ Banner Image -

{{ locale[lang]['git[1]'] }}

-

{{ locale[lang]['git[2]'] }}

+

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

+

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

diff --git a/templates/tmpl/_header.htm b/templates/tmpl/_header.htm index 5e48673..24304ee 100644 --- a/templates/tmpl/_header.htm +++ b/templates/tmpl/_header.htm @@ -1,8 +1,8 @@ - {{ locale['%s.title' % lang][request.url_rule.endpoint] }} + {{ locale['header'][request.url_rule.endpoint] }} - + diff --git a/templates/tmpl/_lang-dropdown.htm b/templates/tmpl/_lang-dropdown.htm index 1f01ef0..7e9e38a 100644 --- a/templates/tmpl/_lang-dropdown.htm +++ b/templates/tmpl/_lang-dropdown.htm @@ -11,13 +11,13 @@ {%- if request.view_args['lang'] == l %} - + {{ l }} {{ lang }} {%- else %} - de{{ lang }} + de{{ lang }} {%- endif %} {%- endfor %} diff --git a/templates/tmpl/_menu.htm b/templates/tmpl/_menu.htm index bdd6e75..ce105a6 100644 --- a/templates/tmpl/_menu.htm +++ b/templates/tmpl/_menu.htm @@ -1,10 +1,10 @@