feat(i18n): add Spanish localization support with Flask-Babel

- Add Flask-Babel for internationalization
- Configure app for Spanish (default) and English
- Add language switcher in navigation
- Wrap all UI text in templates with gettext()
- Create translation directory structure
- Add complete Spanish translations (58 strings)
- Add English translations (template)
- Compile .po to .mo files
- Add extract_translations.py script for future updates
- Add compile_translations.py script

Spanish is now the default language, with English available via ?lang=en

Translated strings include:
- Navigation (Home, Add Tablet, Add User, etc.)
- User Loans interface (Search, Filter, Status, etc.)
- Table headers (User, Tablet, Serial Number, etc.)
- Buttons (Loan, Return, Search, etc.)
- Messages (No users found, No active loans, etc.)
- All UI text across all templates
This commit is contained in:
ijuanes 2026-06-20 09:58:13 +01:00
parent db7e9591e1
commit 34dd3ca937
13 changed files with 1020 additions and 110 deletions

36
app.py
View file

@ -8,10 +8,46 @@ from flask import Flask, render_template, request, redirect, url_for, flash
import sqlite3
import os
from datetime import datetime
from flask_babel import Babel, gettext, lazy_gettext
app = Flask(__name__)
app.secret_key = 'your_secret_key_here'
# Babel configuration
babel = Babel(app)
# Configure supported languages
app.config['BABEL_DEFAULT_LOCALE'] = 'es'
app.config['LANGUAGES'] = {
'en': 'English',
'es': 'Español'
}
# Configure translation directory
app.config['BABEL_TRANSLATION_DIRECTORIES'] = 'translations'
@babel.localeselector
def get_locale():
# Try to get language from URL parameter
lang = request.args.get('lang')
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())
# Context processor to make language and config available in templates
@app.context_processor
def inject_global_variables():
from flask import request
lang = get_locale()
return {
'language': lang,
'config': app.config
}
# Database setup
DATABASE = 'tablets.db'