From 3b324f2b62b8ae605323a9649e6cb3a6e976390b Mon Sep 17 00:00:00 2001 From: Sunpy Date: Fri, 10 May 2019 08:36:45 +0100 Subject: [PATCH] Localization --- filters.py | 10 +++++----- locale/en/side_nav.json | 7 +++++++ locale/no/side_nav.json | 7 +++++++ objects/glob.py | 15 +++++++++++++++ templates/layout/dash.html | 4 +++- templates/layout/includes/side_nav.html | 12 ++++++------ templates/layout/includes/top_nav.html | 2 +- templates/pages/bills.html | 2 +- templates/pages/dashboard.html | 2 +- 9 files changed, 46 insertions(+), 15 deletions(-) create mode 100644 locale/en/side_nav.json create mode 100644 locale/no/side_nav.json diff --git a/filters.py b/filters.py index fb8cceb..65145aa 100644 --- a/filters.py +++ b/filters.py @@ -1,8 +1,8 @@ from flask import render_template from objects import glob -""" -@glob.app.template_filter('load_module') -def load_module(module_name): - return render_template("modules/%s.html" % module_name) -""" \ No newline at end of file +@glob.app.context_processor +def locale(): + def _locale(lang, key): + return glob.langs[lang][key] if key in glob.langs[lang] else key + return dict(locale = _locale) \ No newline at end of file diff --git a/locale/en/side_nav.json b/locale/en/side_nav.json new file mode 100644 index 0000000..4c93dde --- /dev/null +++ b/locale/en/side_nav.json @@ -0,0 +1,7 @@ +{ + "lnk_dashboard": "Dashboard", + "lnk_bills": "Bills", + "lnk_receipts": "Receipts", + "lnk_warranties": "Warranties", + "ttl_economical": "Economical" +} \ No newline at end of file diff --git a/locale/no/side_nav.json b/locale/no/side_nav.json new file mode 100644 index 0000000..35d28ab --- /dev/null +++ b/locale/no/side_nav.json @@ -0,0 +1,7 @@ +{ + "lnk_dashboard": "Dashbord", + "lnk_bills": "Regninger", + "lnk_receipts": "Kvitteringer", + "lnk_warranties": "Garantier", + "ttl_economical": "Økonomisk" +} \ No newline at end of file diff --git a/objects/glob.py b/objects/glob.py index 1400ea4..24e8d8e 100644 --- a/objects/glob.py +++ b/objects/glob.py @@ -10,6 +10,8 @@ import bcrypt app = None # main.py -> Flask App sql_conn = None +langs = {} + # ------------------------------------------------------------------------------ # Global variables that initializes on first load of module @@ -20,6 +22,19 @@ if not os.path.isfile("config.json"): with open("config.json", "r") as f: config = json.load(f) +path = "locale" +for l in os.listdir(path): + langs[l] = {} + _path = "%s/%s" % (path, l) + for f in os.listdir(_path): + _f = "%s/%s" % (_path, f) + with open(_f, "r", encoding = "UTF-8") as j: + pnd = json.load(j) + for k in pnd.keys(): + if k in langs[l]: + raise Exception("Duplicate localization entries found in file %s" % _f) + langs[l] = {**langs[l], **pnd} + def make_sql_connection(): return mysql.connector.connect(**config["mysql"]) diff --git a/templates/layout/dash.html b/templates/layout/dash.html index 241d047..8dd8046 100644 --- a/templates/layout/dash.html +++ b/templates/layout/dash.html @@ -1,10 +1,12 @@ +{% set LANG = "no" %} + {% include 'layout/includes/boot-head.html' %} {% if title %} - Husstanden - {{ title }} + Husstanden - {{ locale(LANG, title) }} {% else %} Husstanden {% endif %} diff --git a/templates/layout/includes/side_nav.html b/templates/layout/includes/side_nav.html index ef19eea..f819b34 100644 --- a/templates/layout/includes/side_nav.html +++ b/templates/layout/includes/side_nav.html @@ -105,29 +105,29 @@
- Dashboard + {{ locale(LANG, "lnk_dashboard") }}
-

Economical

+

{{ locale(LANG, "ttl_economical") }}

- Bills + {{ locale(LANG, "lnk_bills") }}
- Receipts + {{ locale(LANG, "lnk_receipts") }} diff --git a/templates/layout/includes/top_nav.html b/templates/layout/includes/top_nav.html index ea92df0..a7e6482 100644 --- a/templates/layout/includes/top_nav.html +++ b/templates/layout/includes/top_nav.html @@ -34,7 +34,7 @@