feat: add _ function
This commit is contained in:
65
app.py
65
app.py
@@ -8,6 +8,7 @@ from flask import (
|
|||||||
render_template,
|
render_template,
|
||||||
request,
|
request,
|
||||||
url_for,
|
url_for,
|
||||||
|
g,
|
||||||
Response
|
Response
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -22,37 +23,34 @@ app = Flask(__name__)
|
|||||||
|
|
||||||
|
|
||||||
def load_all_locales():
|
def load_all_locales():
|
||||||
cp = ConfigParser()
|
translations = {}
|
||||||
locales_list = []
|
|
||||||
locales_dict = {}
|
|
||||||
locales_dir = "locales"
|
locales_dir = "locales"
|
||||||
|
|
||||||
|
locales_code_default = ('en', 'ru', 'es')
|
||||||
|
locales_code_extra = []
|
||||||
|
locales_code = ()
|
||||||
|
|
||||||
for filename in listdir(locales_dir):
|
for filename in listdir(locales_dir):
|
||||||
if filename.endswith(".ini"):
|
if filename.endswith(".ini"):
|
||||||
|
cp = ConfigParser()
|
||||||
lang = path.splitext(filename)[0]
|
lang = path.splitext(filename)[0]
|
||||||
with open(path.join(locales_dir, filename), encoding="utf-8") as f:
|
with open(path.join(locales_dir, filename), encoding="utf-8") as f:
|
||||||
cp.read_file(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()
|
section: dict(cp[section]) for section in cp.sections()
|
||||||
}
|
}
|
||||||
|
|
||||||
for code, data in locales_dict.items():
|
locales_code = locales_code_default + tuple(sorted(locales_code_extra))
|
||||||
full_name = data.get("title", {}).get("language", code)
|
locales_name = {l: translations[l]['title']['language'] for l in locales_code}
|
||||||
locales_list.append({"code": code, "name": full_name})
|
|
||||||
|
|
||||||
priority = ["en", "ru", "es"]
|
return translations, locales_name, locales_code
|
||||||
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
|
|
||||||
|
|
||||||
|
|
||||||
locales_list, locales_dict, locales_code = load_all_locales()
|
translations, locales_name, locales_code = load_all_locales()
|
||||||
|
|
||||||
|
|
||||||
# ---------- HELPER FUNCTIONS ------------------------------------------------
|
# ---------- HELPER FUNCTIONS ------------------------------------------------
|
||||||
@@ -63,19 +61,40 @@ def get_best_lang():
|
|||||||
|
|
||||||
|
|
||||||
def render_localized_template(lang, template_name):
|
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 redirect(url_for("index", lang=get_best_lang()))
|
||||||
|
|
||||||
return render_template(
|
return render_template(
|
||||||
template_name,
|
template_name,
|
||||||
loc_list=locales_list,
|
|
||||||
locale=locales_dict[lang],
|
|
||||||
lang=lang,
|
|
||||||
year=date.today().year,
|
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 ------------------------------------------------------
|
# ---------- MAIN PAGES ------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
|
@@ -1,5 +1,5 @@
|
|||||||
[title]
|
[title]
|
||||||
language = Spanish
|
language = Español
|
||||||
index = KolibriOS
|
index = KolibriOS
|
||||||
download = KolibriOS - Descargar
|
download = KolibriOS - Descargar
|
||||||
|
|
||||||
|
@@ -10,7 +10,7 @@
|
|||||||
{% include 'tmpl/_menu.htm' %}
|
{% include 'tmpl/_menu.htm' %}
|
||||||
|
|
||||||
<div id="article">
|
<div id="article">
|
||||||
<h1>{{ locale['downloads']['header'] }}</h1>
|
<h1>{{ _('downloads:header') }}</h1>
|
||||||
<table>
|
<table>
|
||||||
{% for ext, alt in (
|
{% for ext, alt in (
|
||||||
('img', 'floppy'),
|
('img', 'floppy'),
|
||||||
@@ -23,7 +23,7 @@
|
|||||||
<img src="{{ url_for('static', filename='img/icons/i_%s.png' % alt) }}" alt="{{ alt }}">
|
<img src="{{ url_for('static', filename='img/icons/i_%s.png' % alt) }}" alt="{{ alt }}">
|
||||||
</td>
|
</td>
|
||||||
<td class="td-description">
|
<td class="td-description">
|
||||||
{{ locale['downloads']['%s-descr' % ext] }}
|
{{ _('downloads:%s-descr' % ext) }}
|
||||||
{% if ext == 'raw' %}
|
{% if ext == 'raw' %}
|
||||||
<span class="beta">BETA</span>
|
<span class="beta">BETA</span>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
@@ -51,28 +51,28 @@
|
|||||||
<tr class="tr-margin-top">
|
<tr class="tr-margin-top">
|
||||||
<td class="td-description" colspan="2">
|
<td class="td-description" colspan="2">
|
||||||
<div role="button" class="help-button"
|
<div role="button" class="help-button"
|
||||||
onclick="alert('{{ locale.downloads.download_help }}');">
|
onclick="alert('{{ _('downloads:download_help') }}');">
|
||||||
{{ locale['downloads']['download_choice'] }}
|
{{ _('downloads:download_choice') }}
|
||||||
</div>
|
</div>
|
||||||
<td class="td-date">
|
<td class="td-date">
|
||||||
<a href="//archive.kolibrios.org/ru/">{{ locale['downloads']['prev_rev'] }}</a>
|
<a href="//archive.kolibrios.org/ru/">{{ _('downloads:prev_rev') }}</a>
|
||||||
</td>
|
</td>
|
||||||
<td class="td-languages">
|
<td class="td-languages">
|
||||||
<a href="//builds.kolibrios.org/">{{ locale['downloads']['all_rev'] }}</a>
|
<a href="//builds.kolibrios.org/">{{ _('downloads:all_rev') }}</a>
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
</table>
|
</table>
|
||||||
|
|
||||||
<p>
|
<p>
|
||||||
{{ locale['downloads']['download_description[0]'] }}
|
{{ _('downloads:download_description[0]') }}
|
||||||
<a href='http://www.7-zip.org' target='_blank'>7zip</a>.
|
<a href='http://www.7-zip.org' target='_blank'>7zip</a>.
|
||||||
<b>{{ locale['title']['index'] }}</b> {{ locale['downloads']['download_description[1]'] }}
|
<b>{{ _('title:index') }}</b> {{ _('downloads:download_description[1]') }}
|
||||||
<a href='http://www.gnu.org/licenses/gpl-2.0.html' target='_blank'>GPLv2</a>,
|
<a href='http://www.gnu.org/licenses/gpl-2.0.html' target='_blank'>GPLv2</a>,
|
||||||
{{ locale['downloads']['download_description[2]'] }}
|
{{ _('downloads:download_description[2]') }}
|
||||||
<a href='https://git.kolibrios.org'>{{ locale['downloads']['download_description[3]'] }}</a>.
|
<a href='https://git.kolibrios.org'>{{ _('downloads:download_description[3]') }}</a>.
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
<h1>{{ locale['screenshots']['header'] }}</h1>
|
<h1>{{ _('screenshots:header') }}</h1>
|
||||||
|
|
||||||
<div id="screen" onclick="next()">
|
<div id="screen" onclick="next()">
|
||||||
<div id="show">
|
<div id="show">
|
||||||
@@ -81,7 +81,7 @@
|
|||||||
id="slide{{ i }}"
|
id="slide{{ i }}"
|
||||||
src="{{ url_for('static', filename='img/screenshots/%d.png' % i ) }}"
|
src="{{ url_for('static', filename='img/screenshots/%d.png' % i ) }}"
|
||||||
{% if i == 1 %}class="visible"{% endif %}
|
{% if i == 1 %}class="visible"{% endif %}
|
||||||
alt="{{ locale['screenshots']['%d' % i] }}"
|
alt="{{ _('screenshots:%d' % i) }}"
|
||||||
>
|
>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
</div>
|
</div>
|
||||||
|
@@ -1,43 +1,41 @@
|
|||||||
<p>
|
<p>
|
||||||
<b>{{ locale['menu']['kolibrios'] }}</b>
|
<b>{{ _('menu:kolibrios') }}</b>
|
||||||
{{ locale['article']['p1[0]'] }}
|
{{ _('article:p1[0]') }}
|
||||||
<a href="http://wiki.kolibrios.org/wiki/Hardware_Support">
|
<a href="http://wiki.kolibrios.org/wiki/Hardware_Support">
|
||||||
{{- locale['article']['p1[1]'] -}}
|
{{- _('article:p1[1]') -}}
|
||||||
</a>
|
</a>
|
||||||
{{ locale['article']['p1[2]'] }}
|
{{ _('article:p1[2]') }}
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
<iframe src="https://www.youtube.com/embed/IEi25wYyj20" allowfullscreen="true"></iframe>
|
<iframe src="https://www.youtube.com/embed/IEi25wYyj20" allowfullscreen="true"></iframe>
|
||||||
|
|
||||||
<p>
|
<p>
|
||||||
{{ locale['article']['p2[0]'] }}
|
{{ _('article:p2[0]') }}
|
||||||
<b>{{ locale['menu']['kolibrios'] }}</b>
|
<b>{{ _('menu:kolibrios') }}</b>
|
||||||
{{ locale['article']['p2[1]'] }}
|
{{ _('article:p2[1]') }}
|
||||||
<a href="http://www.flatassembler.net" target="_blank">FASM</a>
|
<a href="http://www.flatassembler.net" target="_blank">FASM</a>
|
||||||
{%- set extra = locale.article.get('p2[11]', '').strip() -%}
|
{{ _('article:p2[2]') }}
|
||||||
{%- if extra -%} {{ " " + extra }} {%- endif -%}!
|
<b>{{ _('menu:kolibrios') }}</b>
|
||||||
{{ locale.article['p2[2]'] }}
|
{{ _('article:p2[3]') }}
|
||||||
<b>{{ locale['menu']['kolibrios'] }}</b>
|
|
||||||
{{ locale['article']['p2[3]'] }}
|
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
<p>
|
<p>
|
||||||
<b>{{ locale['menu']['kolibrios'] }}</b>
|
<b>{{ _('menu:kolibrios') }}</b>
|
||||||
{{ locale['article']['p3[0]'] }}
|
{{ _('article:p3[0]') }}
|
||||||
<a href="http://board.kolibrios.org">
|
<a href="http://board.kolibrios.org">
|
||||||
{{- locale['article']['p3[1]'] -}}
|
{{- _('article:p3[1]') -}}
|
||||||
</a>
|
</a>
|
||||||
{{ locale['article']['p3[2]'] }}
|
{{ _('article:p3[2]') }}
|
||||||
<a href="https://git.kolibrios.org/KolibriOS/kolibrios">
|
<a href="https://git.kolibrios.org/KolibriOS/kolibrios">
|
||||||
{{- locale['article']['p3[3]'] -}}
|
{{- _('article:p3[3]') -}}
|
||||||
</a>
|
</a>
|
||||||
{{ locale['article']['p3[4]'] }}
|
{{ _('article:p3[4]') }}
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
<p class="p-subscription">
|
<p class="p-subscription">
|
||||||
<b>
|
<b>
|
||||||
{{ locale['article']['p_subscription[0]'] }}
|
{{ _('article:p_subscription[0]') }}
|
||||||
<br/>
|
<br/>
|
||||||
{{ locale['article']['p_subscription[1]'] }}
|
{{ _('article:p_subscription[1]') }}
|
||||||
</b>
|
</b>
|
||||||
</p>
|
</p>
|
||||||
|
@@ -2,6 +2,6 @@
|
|||||||
<img src="{{ url_for('static', filename='img/logo.png') }}" alt="KolibriOS">
|
<img src="{{ url_for('static', filename='img/logo.png') }}" alt="KolibriOS">
|
||||||
<p>
|
<p>
|
||||||
© 2004 – {{ year }} <br />
|
© 2004 – {{ year }} <br />
|
||||||
{{ locale['footer']['team'] }}
|
{{ _('footer:team') }}
|
||||||
</p>
|
</p>
|
||||||
</div>
|
</div>
|
@@ -5,8 +5,8 @@
|
|||||||
<img src="{{ url_for('static', filename='img/logo.png') }}" alt="KolibriOS">
|
<img src="{{ url_for('static', filename='img/logo.png') }}" alt="KolibriOS">
|
||||||
</td>
|
</td>
|
||||||
<td valign="top">
|
<td valign="top">
|
||||||
<h1>{{ locale['git']['header'] }}</h1>
|
<h1>{{ _('git:header') }}</h1>
|
||||||
<p>{{ locale['git']['text'] }}</p>
|
<p>{{ _('git:text') }}</p>
|
||||||
<p class="p-link">
|
<p class="p-link">
|
||||||
<a href="https://git.kolibrios.org">https://git.kolibrios.org</a>
|
<a href="https://git.kolibrios.org">https://git.kolibrios.org</a>
|
||||||
</p>
|
</p>
|
||||||
|
@@ -1,8 +1,8 @@
|
|||||||
<head>
|
<head>
|
||||||
<meta charset="utf-8">
|
<meta charset="utf-8">
|
||||||
<title>{{ locale['title'][request.url_rule.endpoint] }}</title>
|
<title>{{ _('title:%s' % request.url_rule.endpoint) }}</title>
|
||||||
<link rel="icon" type="image/x-icon" href="{{ url_for('static', filename='favicon.ico') }}">
|
<link rel="icon" type="image/x-icon" href="{{ url_for('static', filename='favicon.ico') }}">
|
||||||
<meta name="description" content="{{ locale['header'][request.url_rule.endpoint] }}">
|
<meta name="description" content="{{ _('header:%s' % request.url_rule.endpoint) }}">
|
||||||
<meta name="keywords"
|
<meta name="keywords"
|
||||||
content="kolibri, kolibrios, колибри, колибриос, colibri, operating system, assembler, калибри, fasm, alternate, open source">
|
content="kolibri, kolibrios, колибри, колибриос, colibri, operating system, assembler, калибри, fasm, alternate, open source">
|
||||||
<meta name="viewport" content="width=device-width">
|
<meta name="viewport" content="width=device-width">
|
||||||
|
@@ -1,8 +1,7 @@
|
|||||||
<div id="lang-dropdown">
|
<div id="lang-dropdown">
|
||||||
<div>
|
<div>
|
||||||
{%- for lang_item in loc_list %}
|
{%- for lang_code in g.locales_name.keys() %}
|
||||||
{% set lang_code = lang_item.code %}
|
{% set lang_name = g.locales_name[lang_code] %}
|
||||||
{% set lang_name = lang_item.name %}
|
|
||||||
{%- if request.view_args["lang"] == lang_code %}
|
{%- if request.view_args["lang"] == lang_code %}
|
||||||
<font bg=#FF9800>
|
<font bg=#FF9800>
|
||||||
<a class="a-sel" href="{{ url_for(request.url_rule.endpoint, lang=lang_code) }}">
|
<a class="a-sel" href="{{ url_for(request.url_rule.endpoint, lang=lang_code) }}">
|
||||||
|
@@ -1,25 +1,25 @@
|
|||||||
<nav id="menu">
|
<nav id="menu">
|
||||||
<a href="{{ url_for('index', lang=lang) }}" class="{% if current == 'index' %}a-sel{% endif %}">
|
<a href="{{ url_for('index', lang=g.locale) }}" class="{% if request.endpoint == 'index' %}a-sel{% endif %}">
|
||||||
{% if current == 'index' %}
|
{% if current == 'index' %}
|
||||||
<font bg="lightblue">{{ locale['menu']['kolibrios'] }}</font>
|
<font bg="lightblue">{{ _('menu:kolibrios') }}</font>
|
||||||
{% else %}
|
{% else %}
|
||||||
{{ locale['menu']['kolibrios'] }}
|
{{ _('menu:kolibrios') }}
|
||||||
{% endif %}
|
{% endif %}
|
||||||
</a>
|
</a>
|
||||||
|
|
||||||
<a href="{{ url_for('download', lang=lang) }}" class="{% if current == 'download' %}a-sel{% endif %}">
|
<a href="{{ url_for('download', lang=g.locale) }}" class="{% if request.endpoint == 'download' %}a-sel{% endif %}">
|
||||||
{% if current == 'download' %}
|
{% if current == 'download' %}
|
||||||
<font bg="lightblue">{{ locale['menu']['download'] }}</font>
|
<font bg="lightblue">{{ _('menu:download') }}</font>
|
||||||
{% else %}
|
{% else %}
|
||||||
{{ locale['menu']['download'] }}
|
{{ _('menu:download') }}
|
||||||
{% endif %}
|
{% endif %}
|
||||||
</a>
|
</a>
|
||||||
|
|
||||||
<a href="https://board.kolibrios.org">{{ locale['menu']['forum'] }}</a>
|
<a href="https://board.kolibrios.org">{{ _('menu:forum') }}</a>
|
||||||
<a href="https://wiki.kolibrios.org/wiki/Main_Page/{{ lang }}">{{ locale['menu']['wiki'] }}</a>
|
<a href="https://wiki.kolibrios.org/wiki/Main_Page/{{ g.locale }}">{{ _('menu:wiki') }}</a>
|
||||||
<a href="https://git.kolibrios.org">Git</a>
|
<a href="https://git.kolibrios.org">Git</a>
|
||||||
|
|
||||||
<button onclick="dropdown_show(this)" id="lang-butt">
|
<button onclick="dropdown_show(this)" id="lang-butt">
|
||||||
<img src="{{ url_for('static', filename='img/flags/%s.png' % lang) }}" alt="{{ lang }}">
|
<img src="{{ url_for('static', filename='img/flags/%s.png' % g.locale) }}" alt="{{ g.locale }}">
|
||||||
</button>
|
</button>
|
||||||
</nav>
|
</nav>
|
||||||
|
Reference in New Issue
Block a user