feat: add _ function
This commit is contained in:
65
app.py
65
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 ------------------------------------------------------
|
||||
|
||||
|
||||
|
@@ -1,5 +1,5 @@
|
||||
[title]
|
||||
language = Spanish
|
||||
language = Español
|
||||
index = KolibriOS
|
||||
download = KolibriOS - Descargar
|
||||
|
||||
|
@@ -10,7 +10,7 @@
|
||||
{% include 'tmpl/_menu.htm' %}
|
||||
|
||||
<div id="article">
|
||||
<h1>{{ locale['downloads']['header'] }}</h1>
|
||||
<h1>{{ _('downloads:header') }}</h1>
|
||||
<table>
|
||||
{% for ext, alt in (
|
||||
('img', 'floppy'),
|
||||
@@ -23,7 +23,7 @@
|
||||
<img src="{{ url_for('static', filename='img/icons/i_%s.png' % alt) }}" alt="{{ alt }}">
|
||||
</td>
|
||||
<td class="td-description">
|
||||
{{ locale['downloads']['%s-descr' % ext] }}
|
||||
{{ _('downloads:%s-descr' % ext) }}
|
||||
{% if ext == 'raw' %}
|
||||
<span class="beta">BETA</span>
|
||||
{% endif %}
|
||||
@@ -51,28 +51,28 @@
|
||||
<tr class="tr-margin-top">
|
||||
<td class="td-description" colspan="2">
|
||||
<div role="button" class="help-button"
|
||||
onclick="alert('{{ locale.downloads.download_help }}');">
|
||||
{{ locale['downloads']['download_choice'] }}
|
||||
onclick="alert('{{ _('downloads:download_help') }}');">
|
||||
{{ _('downloads:download_choice') }}
|
||||
</div>
|
||||
<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 class="td-languages">
|
||||
<a href="//builds.kolibrios.org/">{{ locale['downloads']['all_rev'] }}</a>
|
||||
<a href="//builds.kolibrios.org/">{{ _('downloads:all_rev') }}</a>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
|
||||
<p>
|
||||
{{ locale['downloads']['download_description[0]'] }}
|
||||
{{ _('downloads:download_description[0]') }}
|
||||
<a href='http://www.7-zip.org' target='_blank'>7zip</a>.
|
||||
<b>{{ locale['title']['index'] }}</b> {{ locale['downloads']['download_description[1]'] }}
|
||||
<a href='http://www.gnu.org/licenses/gpl-2.0.html' target='_blank'>GPLv2</a>,
|
||||
{{ locale['downloads']['download_description[2]'] }}
|
||||
<a href='https://git.kolibrios.org'>{{ locale['downloads']['download_description[3]'] }}</a>.
|
||||
<b>{{ _('title:index') }}</b> {{ _('downloads:download_description[1]') }}
|
||||
<a href='http://www.gnu.org/licenses/gpl-2.0.html' target='_blank'>GPLv2</a>,
|
||||
{{ _('downloads:download_description[2]') }}
|
||||
<a href='https://git.kolibrios.org'>{{ _('downloads:download_description[3]') }}</a>.
|
||||
</p>
|
||||
|
||||
<h1>{{ locale['screenshots']['header'] }}</h1>
|
||||
<h1>{{ _('screenshots:header') }}</h1>
|
||||
|
||||
<div id="screen" onclick="next()">
|
||||
<div id="show">
|
||||
@@ -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 %}
|
||||
</div>
|
||||
|
@@ -1,43 +1,41 @@
|
||||
<p>
|
||||
<b>{{ locale['menu']['kolibrios'] }}</b>
|
||||
{{ locale['article']['p1[0]'] }}
|
||||
<b>{{ _('menu:kolibrios') }}</b>
|
||||
{{ _('article:p1[0]') }}
|
||||
<a href="http://wiki.kolibrios.org/wiki/Hardware_Support">
|
||||
{{- locale['article']['p1[1]'] -}}
|
||||
{{- _('article:p1[1]') -}}
|
||||
</a>
|
||||
{{ locale['article']['p1[2]'] }}
|
||||
{{ _('article:p1[2]') }}
|
||||
</p>
|
||||
|
||||
<iframe src="https://www.youtube.com/embed/IEi25wYyj20" allowfullscreen="true"></iframe>
|
||||
|
||||
<p>
|
||||
{{ locale['article']['p2[0]'] }}
|
||||
<b>{{ locale['menu']['kolibrios'] }}</b>
|
||||
{{ locale['article']['p2[1]'] }}
|
||||
{{ _('article:p2[0]') }}
|
||||
<b>{{ _('menu:kolibrios') }}</b>
|
||||
{{ _('article:p2[1]') }}
|
||||
<a href="http://www.flatassembler.net" target="_blank">FASM</a>
|
||||
{%- set extra = locale.article.get('p2[11]', '').strip() -%}
|
||||
{%- if extra -%} {{ " " + extra }} {%- endif -%}!
|
||||
{{ locale.article['p2[2]'] }}
|
||||
<b>{{ locale['menu']['kolibrios'] }}</b>
|
||||
{{ locale['article']['p2[3]'] }}
|
||||
{{ _('article:p2[2]') }}
|
||||
<b>{{ _('menu:kolibrios') }}</b>
|
||||
{{ _('article:p2[3]') }}
|
||||
</p>
|
||||
|
||||
<p>
|
||||
<b>{{ locale['menu']['kolibrios'] }}</b>
|
||||
{{ locale['article']['p3[0]'] }}
|
||||
<b>{{ _('menu:kolibrios') }}</b>
|
||||
{{ _('article:p3[0]') }}
|
||||
<a href="http://board.kolibrios.org">
|
||||
{{- locale['article']['p3[1]'] -}}
|
||||
{{- _('article:p3[1]') -}}
|
||||
</a>
|
||||
{{ locale['article']['p3[2]'] }}
|
||||
{{ _('article:p3[2]') }}
|
||||
<a href="https://git.kolibrios.org/KolibriOS/kolibrios">
|
||||
{{- locale['article']['p3[3]'] -}}
|
||||
{{- _('article:p3[3]') -}}
|
||||
</a>
|
||||
{{ locale['article']['p3[4]'] }}
|
||||
{{ _('article:p3[4]') }}
|
||||
</p>
|
||||
|
||||
<p class="p-subscription">
|
||||
<b>
|
||||
{{ locale['article']['p_subscription[0]'] }}
|
||||
{{ _('article:p_subscription[0]') }}
|
||||
<br/>
|
||||
{{ locale['article']['p_subscription[1]'] }}
|
||||
{{ _('article:p_subscription[1]') }}
|
||||
</b>
|
||||
</p>
|
||||
|
@@ -2,6 +2,6 @@
|
||||
<img src="{{ url_for('static', filename='img/logo.png') }}" alt="KolibriOS">
|
||||
<p>
|
||||
© 2004 – {{ year }} <br />
|
||||
{{ locale['footer']['team'] }}
|
||||
{{ _('footer:team') }}
|
||||
</p>
|
||||
</div>
|
@@ -5,8 +5,8 @@
|
||||
<img src="{{ url_for('static', filename='img/logo.png') }}" alt="KolibriOS">
|
||||
</td>
|
||||
<td valign="top">
|
||||
<h1>{{ locale['git']['header'] }}</h1>
|
||||
<p>{{ locale['git']['text'] }}</p>
|
||||
<h1>{{ _('git:header') }}</h1>
|
||||
<p>{{ _('git:text') }}</p>
|
||||
<p class="p-link">
|
||||
<a href="https://git.kolibrios.org">https://git.kolibrios.org</a>
|
||||
</p>
|
||||
|
@@ -1,8 +1,8 @@
|
||||
<head>
|
||||
<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') }}">
|
||||
<meta name="description" content="{{ locale['header'][request.url_rule.endpoint] }}">
|
||||
<meta name="description" content="{{ _('header:%s' % request.url_rule.endpoint) }}">
|
||||
<meta name="keywords"
|
||||
content="kolibri, kolibrios, колибри, колибриос, colibri, operating system, assembler, калибри, fasm, alternate, open source">
|
||||
<meta name="viewport" content="width=device-width">
|
||||
|
@@ -1,8 +1,7 @@
|
||||
<div id="lang-dropdown">
|
||||
<div>
|
||||
{%- for lang_item in loc_list %}
|
||||
{% set lang_code = lang_item.code %}
|
||||
{% set lang_name = lang_item.name %}
|
||||
{%- for lang_code in g.locales_name.keys() %}
|
||||
{% set lang_name = g.locales_name[lang_code] %}
|
||||
{%- if request.view_args["lang"] == lang_code %}
|
||||
<font bg=#FF9800>
|
||||
<a class="a-sel" href="{{ url_for(request.url_rule.endpoint, lang=lang_code) }}">
|
||||
|
@@ -1,25 +1,25 @@
|
||||
<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' %}
|
||||
<font bg="lightblue">{{ locale['menu']['kolibrios'] }}</font>
|
||||
<font bg="lightblue">{{ _('menu:kolibrios') }}</font>
|
||||
{% else %}
|
||||
{{ locale['menu']['kolibrios'] }}
|
||||
{{ _('menu:kolibrios') }}
|
||||
{% endif %}
|
||||
</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' %}
|
||||
<font bg="lightblue">{{ locale['menu']['download'] }}</font>
|
||||
<font bg="lightblue">{{ _('menu:download') }}</font>
|
||||
{% else %}
|
||||
{{ locale['menu']['download'] }}
|
||||
{{ _('menu:download') }}
|
||||
{% endif %}
|
||||
</a>
|
||||
|
||||
<a href="https://board.kolibrios.org">{{ locale['menu']['forum'] }}</a>
|
||||
<a href="https://wiki.kolibrios.org/wiki/Main_Page/{{ lang }}">{{ locale['menu']['wiki'] }}</a>
|
||||
<a href="https://board.kolibrios.org">{{ _('menu:forum') }}</a>
|
||||
<a href="https://wiki.kolibrios.org/wiki/Main_Page/{{ g.locale }}">{{ _('menu:wiki') }}</a>
|
||||
<a href="https://git.kolibrios.org">Git</a>
|
||||
|
||||
<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>
|
||||
</nav>
|
||||
|
Reference in New Issue
Block a user