Localization

This commit is contained in:
Emily 2019-05-10 08:36:45 +01:00
parent 7c4df07961
commit 3b324f2b62
9 changed files with 46 additions and 15 deletions

View File

@ -1,8 +1,8 @@
from flask import render_template from flask import render_template
from objects import glob from objects import glob
""" @glob.app.context_processor
@glob.app.template_filter('load_module') def locale():
def load_module(module_name): def _locale(lang, key):
return render_template("modules/%s.html" % module_name) return glob.langs[lang][key] if key in glob.langs[lang] else key
""" return dict(locale = _locale)

7
locale/en/side_nav.json Normal file
View File

@ -0,0 +1,7 @@
{
"lnk_dashboard": "Dashboard",
"lnk_bills": "Bills",
"lnk_receipts": "Receipts",
"lnk_warranties": "Warranties",
"ttl_economical": "Economical"
}

7
locale/no/side_nav.json Normal file
View File

@ -0,0 +1,7 @@
{
"lnk_dashboard": "Dashbord",
"lnk_bills": "Regninger",
"lnk_receipts": "Kvitteringer",
"lnk_warranties": "Garantier",
"ttl_economical": "Økonomisk"
}

View File

@ -10,6 +10,8 @@ import bcrypt
app = None # main.py -> Flask App app = None # main.py -> Flask App
sql_conn = None sql_conn = None
langs = {}
# ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------
# Global variables that initializes on first load of module # 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: with open("config.json", "r") as f:
config = json.load(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(): def make_sql_connection():
return mysql.connector.connect(**config["mysql"]) return mysql.connector.connect(**config["mysql"])

View File

@ -1,10 +1,12 @@
{% set LANG = "no" %} <!-- TODO: Dynamic lang swap -->
<!DOCTYPE html> <!DOCTYPE html>
<html lang="en"> <html lang="en">
<head> <head>
{% include 'layout/includes/boot-head.html' %} {% include 'layout/includes/boot-head.html' %}
<link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='css/custom.css') }}"> <link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='css/custom.css') }}">
{% if title %} {% if title %}
<title>Husstanden - {{ title }}</title> <title>Husstanden - {{ locale(LANG, title) }}</title>
{% else %} {% else %}
<title>Husstanden</title> <title>Husstanden</title>
{% endif %} {% endif %}

View File

@ -105,29 +105,29 @@
<div class="flex-column"> <div class="flex-column">
<div> <!-- Collection --> <div> <!-- Collection -->
<a class="item" href="{{ url_for('dashboard') }}"> <a class="item" href="{{ url_for('dashboard') }}">
<i class="far fa-calendar-alt"></i><span>Dashboard</span> <i class="far fa-calendar-alt"></i><span>{{ locale(LANG, "lnk_dashboard") }}</span>
</a> </a>
</div> </div>
<div> <div>
<h4>Economical</h4> <h4>{{ locale(LANG, "ttl_economical") }}</h4>
</div> </div>
<div> <div>
<a class="item" href="{{ url_for('bills') }}"> <a class="item" href="{{ url_for('bills') }}">
<i class="fas fa-money-check-alt"></i><span>Bills</span> <i class="fas fa-money-check-alt"></i><span>{{ locale(LANG, "lnk_bills") }}</span>
</a> </a>
</div> </div>
<div> <div>
<a class="item" onclick="toggleCategory(this)"> <a class="item" onclick="toggleCategory(this)">
<i class="far fa-list-alt"></i><span>Receipts</span> <i class="far fa-list-alt"></i><span>{{ locale(LANG, "lnk_receipts") }}</span>
<i class="fas fa-chevron-right"></i> <i class="fas fa-chevron-right"></i>
</a> </a>
<div class="downtab hidden"> <div class="downtab hidden">
<div class="flex-column"> <div class="flex-column">
<div class="page"> <div class="page">
<a href="{{ url_for('receipts') }}">Receipts</a> <a href="{{ url_for('receipts') }}">{{ locale(LANG, "lnk_receipts") }}</a>
</div> </div>
<div class="page"> <div class="page">
<a href="{{ url_for('warranties') }}">Warranties</a> <a href="{{ url_for('warranties') }}">{{ locale(LANG, "lnk_warranties") }}</a>
</div> </div>
</div> </div>
</div> </div>

View File

@ -34,7 +34,7 @@
</style> </style>
<nav class="navbar topnav" style="margin-bottom: 10px;"> <nav class="navbar topnav" style="margin-bottom: 10px;">
<h3>{{ title }}</h3> <h3>{{ locale(LANG, title) }}</h3>
<div> <div>
{% with messages = get_flashed_messages(with_categories = true) %} {% with messages = get_flashed_messages(with_categories = true) %}
{% if messages %} {% if messages %}

View File

@ -1,4 +1,4 @@
{% set title = "Bills" %} {% set title = "lnk_bills" %}
{% extends "layout/dash.html" %} {% extends "layout/dash.html" %}

View File

@ -1,4 +1,4 @@
{% set title = "Dashboard" %} {% set title = "lnk_dashboard" %}
{% extends "layout/dash.html" %} {% extends "layout/dash.html" %}