From d28883ae2cd5633e395d41089dd3fc03e061ebe7 Mon Sep 17 00:00:00 2001 From: David Bryan Date: Thu, 31 Oct 2019 10:40:35 +0000 Subject: [PATCH] Added babel --- config.py | 9 --- mysettings.cfg | 4 + proxmark3-web.py | 45 +++++------- proxmark3-web.service | 2 +- templates/buttons.html | 8 +- templates/cards.html | 2 +- templates/header.html | 5 +- templates/main.html | 16 ++-- templates/nav.html | 6 +- translations/de/LC_MESSAGES/messages.po | 74 ++++++++++++++++--- translations/ja/LC_MESSAGES/messages.po | 98 ++++++++++++++++++++++++- 11 files changed, 201 insertions(+), 68 deletions(-) delete mode 100644 config.py create mode 100644 mysettings.cfg diff --git a/config.py b/config.py deleted file mode 100644 index 8b1b0df..0000000 --- a/config.py +++ /dev/null @@ -1,9 +0,0 @@ -# -*- coding: utf-8 -*- -# ... -# available languages -LANGUAGES = { - 'en': 'English', - 'de': 'German', - 'ja': 'Japanese', - 'es': 'Español' -} diff --git a/mysettings.cfg b/mysettings.cfg new file mode 100644 index 0000000..9c03d97 --- /dev/null +++ b/mysettings.cfg @@ -0,0 +1,4 @@ +BABEL_DEFAULT_LOCALE = 'en' +BABEL_DEFAULT_TIMEZONE = 'UTC' +BABEL_TRANSLATION_DIRECTORIES = '/home/pi/proxmark3-web/translations' + diff --git a/proxmark3-web.py b/proxmark3-web.py index 64f1722..d2e8f69 100755 --- a/proxmark3-web.py +++ b/proxmark3-web.py @@ -6,11 +6,11 @@ import os, string, subprocess, sys, time, random from flask import Flask, flash, redirect, render_template, \ request, url_for -from flask_sqlalchemy import SQLAlchemy -from flask_babel import Babel from flask_babel import gettext, ngettext +from flask_babel import Babel +from flask_babel import force_locale as babel_force_locale +from flask_sqlalchemy import SQLAlchemy from datetime import datetime -from config import LANGUAGES debug=1 @@ -71,27 +71,18 @@ if(True): # create and configure the app app = Flask(__name__, instance_relative_config=True) + app.config.from_pyfile('/home/pi/proxmark3-web/mysettings.cfg') + babel = Babel(app) + babel.init_app(app) #Set up the Database for storing cards app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:////' + db_file app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False db = SQLAlchemy(app) - app.config['BABEL_DEFAULT_LOCALE'] = "de" - app.config['BABEL_DEFAULT_TIMEZONE'] = "UTC" - babel = Babel(app) - #@babel.localeselector - #def get_locale(): - # Basic method, can be used as a fallback if a user's profile does not specify a language, - # or a user hasn't yet registered. - #user = getattr(g, 'user', None) - #if user is not None: - #return user.locale + @babel.localeselector + def get_locale(): + return request.accept_languages.best_match(['ko','zh','ja', 'ja_JP', 'en']) - #return request.accept_languages.best_match(LANGUAGES.keys()) - - # will return language code (en/es/etc). -# return 'es' -# return result # Database Classes class card_tbl(db.Model): @@ -106,8 +97,6 @@ if(True): def __repr__(self): return ''.format(self.card_raw) - # return "" % ( - # self.id, self.time_stamp, self.card_raw, self.card_number, self.card_format, self.card_oem, self.card_facility_code) app.config.from_mapping( SECRET_KEY=str(random.getrandbits(64)) @@ -174,7 +163,7 @@ if(True): card_read=card_read ) else: - flash('ERROR: Could not read a card... please try again....') + flash(gettext('ERROR: Could not read a card... please try again....')) return redirect(url_for('index')) @app.route('/hid/sim') @@ -187,7 +176,7 @@ if(True): #write_hid = subprocess.run([proxmark3_rdv4_client, serial_port, '-c', 'lf hid sim ' + raw_cardnumber, '&' ], capture_output=True) write_hid = subprocess.Popen([proxmark3_rdv4_client, serial_port, '-c', 'lf hid sim ' + raw_cardnumber ]) - flash('Simulate Mode has been entered, please press the button on the PM3 to exit...') + flash(gettext('Simulate Mode has been entered, please press the button on the PM3 to exit...')) return redirect(url_for('index')) #if('Simulating HID tag with ID' in write_hid.stdout.decode('ASCII')): @@ -215,7 +204,7 @@ if(True): flash('Wrote ID to card.') return redirect(url_for('index')) else: - flash('ERROR: CARD DID NOT PROGRAM... TRY AGIAN.') + flash(gettext('ERROR: CARD DID NOT PROGRAM... TRY AGIAN.')) return redirect(url_for('index')) @app.route('/card/list') @@ -229,7 +218,7 @@ if(True): @app.route('/shutdown') def shutdown_os_now(): subprocess.run(['sudo', '/sbin/shutdown', '-P']) - flash('System shutdown in progress....') + flash(gettext('System shutdown in progress....')) return redirect(url_for('index')) return render_template('cards.html', card = card) @@ -241,7 +230,7 @@ if(True): db.session.commit() if debug: print('Deleted card id: '+ str(card_id)) - flash('Card '+card_id+' was deleted from the database...') + flash(gettext('Card ') +card_id+ gettext(' was deleted from the database...')) return redirect(url_for('card_list')) @app.route('/wipe_card') @@ -252,7 +241,7 @@ if(True): return redirect(url_for('index')) if(wipe_card.returncode == 0): - flash('t5577 card wipe complete') + flash(gettext('t5577 card wipe complete')) return redirect(url_for('index')) @app.route('/provision_card') @@ -266,10 +255,10 @@ if(True): if(new_hid_id.returncode == 0): if('HID Prox TAG ID: 1029a0f4d2' in new_hid_id.stdout.decode('ASCII')): - flash('Wrote default ID to card.') + flash(gettext('Wrote default ID to card.')) return redirect(url_for('index')) else: - flash('ERROR: CARD DID NOT PROGRAM... TRY AGIAN.') + flash(gettext('ERROR: CARD DID NOT PROGRAM... TRY AGIAN.')) return redirect(url_for('index')) diff --git a/proxmark3-web.service b/proxmark3-web.service index a58b344..8882b1d 100644 --- a/proxmark3-web.service +++ b/proxmark3-web.service @@ -7,7 +7,7 @@ WorkingDirectory=/home/pi/proxmark3-web/ #Type=simple Environment=FLASK_CONFIG=production Environment=FLASK_APP=/home/pi/proxmark3-web/proxmark3-web.py -ExecStart=/usr/bin/gunicorn3 --reload proxmark3-web:app -b:8080 +ExecStart=/usr/bin/gunicorn3 --threads 4 --reload proxmark3-web:app -b:8080 Restart=always [Install] diff --git a/templates/buttons.html b/templates/buttons.html index a4a7500..4c3d8c8 100644 --- a/templates/buttons.html +++ b/templates/buttons.html @@ -8,25 +8,25 @@ diff --git a/templates/cards.html b/templates/cards.html index fbfb654..784a8b0 100644 --- a/templates/cards.html +++ b/templates/cards.html @@ -10,7 +10,7 @@

{% with messages = get_flashed_messages() %} {% if messages %} - Status: + {{ gettext('Status') }}: {% for message in messages %} {{ message }} {% endfor %} diff --git a/templates/header.html b/templates/header.html index 511f5ef..f632738 100644 --- a/templates/header.html +++ b/templates/header.html @@ -2,10 +2,11 @@ {% include "nav.html" %} -

+
-
+
+
diff --git a/templates/main.html b/templates/main.html index a241e01..fd468ea 100644 --- a/templates/main.html +++ b/templates/main.html @@ -6,7 +6,7 @@ @@ -14,10 +14,10 @@

Card Info

- OEM Code: {{ oem }}
- Facility Code: {{ facility_code }}
- Card Number: {{ card_number }}
- Raw HID Formated Card: {{ raw_cardnumber }} + {{ gettext('OEM Code') }}: {{ oem }}
+ {{ gettext('Facility Code') }}: {{ facility_code }}
+ {{ gettext('Card Number') }}: {{ card_number }}
+ {{ gettext('Raw HID Formated Card') }}: {{ raw_cardnumber }}

@@ -29,11 +29,11 @@
- Card ID: + {{ gettext('Card ID') }}:
- +
@@ -46,7 +46,7 @@

{% with messages = get_flashed_messages() %} {% if messages %} - Status: + {{ gettext('Status') }}: {% for message in messages %} {{ message }} {% endfor %} diff --git a/templates/nav.html b/templates/nav.html index 01ae6d1..63af177 100644 --- a/templates/nav.html +++ b/templates/nav.html @@ -1,5 +1,5 @@