Commit 7d28dedc authored by Štěpán Henek's avatar Štěpán Henek 🌩

Languages are detected dynamically

to add a language you need to:
* add coresponding .po file
* add translated name + iso3 code to core.py
parent c9b5da7c
......@@ -16,6 +16,7 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# builtins
import collections
import gettext
import hashlib
import logging
......@@ -47,26 +48,40 @@ BASE_DIR = os.path.dirname(__file__)
# internationalization
i18n_defaults(bottle.SimpleTemplate, bottle.request)
DEFAULT_LANGUAGE = 'en'
translations = {
'cs': gettext.translation("messages", os.path.join(BASE_DIR, "locale"),
languages=['cs'], fallback=True),
'de': gettext.translation("messages", os.path.join(BASE_DIR, "locale"),
languages=['de'], fallback=True),
'en': gettext.translation("messages", os.path.join(BASE_DIR, "locale"),
languages=['en'], fallback=True),
'sk': gettext.translation("messages", os.path.join(BASE_DIR, "locale"),
languages=['sk'], fallback=True),
}
# read locale directory
locale_directory = os.path.join(BASE_DIR, "locale")
translations = sorted([
d for d in os.listdir(locale_directory) + [DEFAULT_LANGUAGE]
if os.path.isdir(os.path.join(locale_directory, d))
])
translations.insert(0, DEFAULT_LANGUAGE) # no folder for default language (en)
translations = collections.OrderedDict(
(e, gettext.translation("messages", locale_directory, languages=[e], fallback=True))
for e in translations
)
translation_names = {
'cs': "Česky",
'de': "Deutsch",
'en': "English",
'sk': "Slovenský",
'sk': "Slovensky",
}
iso2to3 = {
'cs': "cze",
'de': "deu",
'en': "eng",
'sk': "svk",
}
ugettext = lambda x: translations[bottle.request.app.lang].ugettext(x)
ungettext = lambda singular, plural, n: translations[bottle.request.app.lang].ungettext(singular, plural, n)
bottle.SimpleTemplate.defaults['trans'] = lambda msgid: ugettext(msgid) # workaround
bottle.SimpleTemplate.defaults['translation_names'] = translation_names
bottle.SimpleTemplate.defaults['translations'] = [e for e in translations]
bottle.SimpleTemplate.defaults['iso2to3'] = iso2to3
bottle.SimpleTemplate.defaults['ungettext'] = lambda singular, plural, n: ungettext(singular, plural, n)
bottle.SimpleTemplate.defaults['DEVICE_CUSTOMIZATION'] = DEVICE_CUSTOMIZATION
gettext_dummy = lambda x: x
......
......@@ -46,3 +46,9 @@
position: absolute
right: 0.3em
top: 0.3em
span
text-transform: uppercase
a
text-transform: uppercase
......@@ -44,6 +44,12 @@
top: -1.6em
right: 0
span
text-transform: uppercase
a
text-transform: uppercase
#wizard-header
border-bottom: 1px solid $stroke-color
padding: 1.6em 0 0.5em
......
{{ trans("Language") }}:
<span>{{ iso2to3.get(lang(), lang()) }}</span>
%for code in translations:
%if code != lang():
| <a href="{{ url("change_lang", lang=code, backlink=request.fullpath) }}">{{ iso2to3.get(code, code) }}</a>
%end
%end
......@@ -50,11 +50,11 @@
<a href="{{ url("logout") }}">{{ trans("Log out") }}</a>
</div>
<div id="language-switch">
<span>{{ translation_names[lang()] }}</span>
<span>{{ translation_names.get(lang(), lang()) }}</span>
<ul>
%for code, name in translation_names.iteritems():
%for code in translations:
%if code != lang():
<li><a href="{{ url("change_lang", lang=code, backlink=request.fullpath) }}">{{ name }}</a></li>
<li><a href="{{ url("change_lang", lang=code, backlink=request.fullpath) }}">{{ translation_names.get(code, code) }}</a></li>
%end
%end
</ul>
......
......@@ -17,11 +17,7 @@
%rebase("_layout.tpl", **locals())
<div id="login-page">
<div class="language-switch">
{{ trans("Language") }}:
<a href="{{ url("change_lang", lang="cs", backlink=request.fullpath) }}">CZE</a>
| <a href="{{ url("change_lang", lang="de", backlink=request.fullpath) }}">DEU</a>
| <a href="{{ url("change_lang", lang="en", backlink=request.fullpath) }}">ENG</a>
| <a href="{{ url("change_lang", lang="sk", backlink=request.fullpath) }}">SVK</a>
%include("_lang_flat", translations=translations, iso2to3=iso2to3)
</div>
<h1><img src="{{ static("img/logo-turris.svg") }}" alt="{{ trans("Project:Turris") }}" width="295"></h1>
......
<div id="wizard-language-switch">
{{ trans("Language") }}:
<a href="{{ url("change_lang", lang="cs", backlink=request.fullpath) }}">CZE</a>
| <a href="{{ url("change_lang", lang="de", backlink=request.fullpath) }}">DEU</a>
| <a href="{{ url("change_lang", lang="en", backlink=request.fullpath) }}">ENG</a>
| <a href="{{ url("change_lang", lang="sk", backlink=request.fullpath) }}">SVK</a>
%include("_lang_flat", translations=translations, iso2to3=iso2to3)
</div>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment