fix(i18n): enable translations in all templates and implement cookie-based language persistence
- Add _() translation function to add_tablet.html, add_user.html, loan_tablet.html - Add _() translation function to add_non_loanable_device.html, edit_non_loanable_device.html - Add _() translation function to history.html, non_loanable_devices.html, project_management.html - Update app.py to use cookie-based language persistence with 1-year expiry - Add /set_language endpoint to handle language switching with cookie + session - Update language switcher in base.html to use new endpoint - Set Spanish (es) as default language for internal app - Import make_response and session from Flask
This commit is contained in:
parent
8c1d12a5c4
commit
2248237c79
10 changed files with 133 additions and 107 deletions
36
app.py
36
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)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue