diff --git a/app.py b/app.py index 807be11..cc962ca 100644 --- a/app.py +++ b/app.py @@ -4,7 +4,7 @@ Simple Tablet Lending and Return Management Web Application with SQLite backend """ -from flask import Flask, render_template, request, redirect, url_for, flash +from flask import Flask, render_template, request, redirect, url_for, flash, make_response, session import sqlite3 import os from datetime import datetime @@ -25,15 +25,41 @@ app.config['BABEL_TRANSLATION_DIRECTORIES'] = 'translations' # Locale selector function def get_locale(): - # Try to get language from URL parameter - lang = request.args.get('lang') + # Try to get language from cookie + lang = request.cookies.get('language') if lang and lang in app.config['LANGUAGES']: return lang # Try to get from session if hasattr(request, 'session') and 'language' in request.session: return request.session['language'] - # Try to get from browser preferences - return request.accept_languages.best_match(app.config['LANGUAGES'].keys()) + # Default to Spanish (es) for internal app + return 'es' + + +# Before request handler to set language from URL param and persist to cookie/session +@app.before_request +def handle_language(): + lang = request.args.get('lang') + if lang and lang in app.config['LANGUAGES']: + # Store in session + session['language'] = lang + # Set cookie for longer persistence (1 year) + # Note: We need to use response.set_cookie, but before_request can't modify response + # So we'll handle this in a separate route + return None + + +# Language switcher endpoint +@app.route('/set_language') +def set_language(): + lang = request.args.get('lang') + if lang and lang in app.config['LANGUAGES']: + session['language'] = lang + # Set cookie + resp = make_response(redirect(request.referrer or '/')) + resp.set_cookie('language', lang, max_age=31536000) # 1 year + return resp + return redirect(request.referrer or '/') # Initialize Babel with locale selector babel = Babel(app, locale_selector=get_locale) diff --git a/templates/add_non_loanable_device.html b/templates/add_non_loanable_device.html index ae0489e..52ec344 100644 --- a/templates/add_non_loanable_device.html +++ b/templates/add_non_loanable_device.html @@ -1,63 +1,63 @@ {% extends "base.html" %} {% block content %} -

Add Non-Loanable Device

-

Add a device that will be tracked in inventory but cannot be loaned to users (e.g., projectors, monitors, etc.)

+

{{ _('Add Non-Loanable Device') }}

+

{{ _('Add a device that will be tracked in inventory but cannot be loaned to users (e.g., projectors, monitors, etc.)') }}

- +
- +
- +
- +
- +
- +
- +
- +
- - Cancel + + {{ _('Cancel') }}
{% endblock %} diff --git a/templates/add_tablet.html b/templates/add_tablet.html index 8199daf..bc1fa4b 100644 --- a/templates/add_tablet.html +++ b/templates/add_tablet.html @@ -1,30 +1,30 @@ {% extends "base.html" %} {% block content %} -

Add New Tablet

+

{{ _('Add New Tablet') }}

- +
- +
- +
- +
- +
-{% endblock %} \ No newline at end of file +{% endblock %} diff --git a/templates/add_user.html b/templates/add_user.html index 2bcd10b..78439d9 100644 --- a/templates/add_user.html +++ b/templates/add_user.html @@ -1,30 +1,30 @@ {% extends "base.html" %} {% block content %} -

Add New User

+

{{ _('Add New User') }}

- +
- +
- +
- +
- +
-{% endblock %} \ No newline at end of file +{% endblock %} diff --git a/templates/base.html b/templates/base.html index de6ba8f..50ff3ff 100644 --- a/templates/base.html +++ b/templates/base.html @@ -122,7 +122,7 @@ {{ _('Language') }}: {% for lang_code, lang_name in config['LANGUAGES'].items() %} diff --git a/templates/edit_non_loanable_device.html b/templates/edit_non_loanable_device.html index 80c90b4..339a252 100644 --- a/templates/edit_non_loanable_device.html +++ b/templates/edit_non_loanable_device.html @@ -1,71 +1,71 @@ {% extends "base.html" %} {% block content %} -

Edit Non-Loanable Device

+

{{ _('Edit Non-Loanable Device') }}

- +
- +
- +
- +
- +
- +
- +
- +
- +
- - Cancel + + {{ _('Cancel') }}
{% endblock %} diff --git a/templates/history.html b/templates/history.html index 061ffbd..b5018e9 100644 --- a/templates/history.html +++ b/templates/history.html @@ -2,18 +2,18 @@ {% block content %}
-

Loan History

+

{{ _('Loan History') }}

{% if loans %}
- - - - - - + + + + + + @@ -31,7 +31,7 @@
TabletSerial NumberBorrowerLoan DateReturn DateStatus{{ _('Tablet') }}{{ _('Serial Number') }}{{ _('Borrower') }}{{ _('Loan Date') }}{{ _('Return Date') }}{{ _('Status') }}
{% else %} -

No loan history available.

+

{{ _('No loan history available.') }}

{% endif %}
-{% endblock %} \ No newline at end of file +{% endblock %} diff --git a/templates/loan_tablet.html b/templates/loan_tablet.html index b9f2527..ce13733 100644 --- a/templates/loan_tablet.html +++ b/templates/loan_tablet.html @@ -1,16 +1,16 @@ {% extends "base.html" %} {% block content %} -

Loan Tablet

+

{{ _('Loan Tablet') }}

- + + placeholder="{{ _('Write your markdown notes here...') }}">
-

Preview

+

{{ _('Preview') }}

@@ -54,13 +54,13 @@ .then(content => { document.getElementById('markdown-editor').value = content; document.getElementById('markdown-preview').innerHTML = marked.parse(content); - document.getElementById('status').textContent = 'Loaded successfully'; + document.getElementById('status').textContent = '{{ _("Loaded successfully") }}'; setTimeout(() => { document.getElementById('status').textContent = ''; }, 2000); }) .catch(error => { - document.getElementById('status').textContent = 'Using default content'; + document.getElementById('status').textContent = '{{ _("Using default content") }}'; setTimeout(() => { document.getElementById('status').textContent = ''; }, 2000); @@ -80,10 +80,10 @@ }) .then(response => { if (response.ok) { - statusEl.textContent = 'Saved successfully!'; + statusEl.textContent = '{{ _("Saved successfully!") }}'; statusEl.style.color = '#4CAF50'; } else { - statusEl.textContent = 'Error saving'; + statusEl.textContent = '{{ _("Error saving") }}'; statusEl.style.color = '#f44336'; } setTimeout(() => { @@ -92,7 +92,7 @@ }, 2000); }) .catch(error => { - statusEl.textContent = 'Error: ' + error.message; + statusEl.textContent = '{{ _("Error") }}: ' + error.message; statusEl.style.color = '#f44336'; setTimeout(() => { statusEl.textContent = '';