Urgent Fix for Translations

This commit is contained in:
nocci 2025-04-29 18:02:22 +02:00
parent fb6a2799ce
commit b1aee9e5b8
6 changed files with 133 additions and 122 deletions

View file

@ -1,13 +1,13 @@
#!/bin/bash
set -e
# Color definitions
# Colors
RED='\033[1;31m'
GREEN='\033[1;32m'
YELLOW='\033[1;33m'
NC='\033[0m'
# 1. Docker check (including Arch Linux)
# 1. Docker check (incl. Arch Linux)
if ! command -v docker &>/dev/null; then
echo -e "${RED}❗ Docker is not installed.${NC}"
read -p "Would you like to install Docker automatically now? [y/N]: " install_docker
@ -23,7 +23,7 @@ if ! command -v docker &>/dev/null; then
rm get-docker.sh
fi
# Check Docker group membership
# Docker group membership prüfen
if ! groups | grep -q '\bdocker\b'; then
echo -e "${YELLOW}⚠️ Your user is not in the docker group. Adding now...${NC}"
sudo usermod -aG docker $USER
@ -37,7 +37,7 @@ if ! command -v docker &>/dev/null; then
fi
fi
# 2. Docker Compose check (V1 and V2 plugin, with Arch support)
# 2. Check Docker compose (V1 und V2 Plugin, incl. Arch Support)
DOCKER_COMPOSE_CMD=""
if command -v docker-compose &>/dev/null; then
DOCKER_COMPOSE_CMD="docker-compose"
@ -74,13 +74,12 @@ else
fi
fi
# Configuration
PROJECT_DIR="steam-gift-manager"
TRANSLATIONS_DIR="../translations"
DATA_DIR="../data"
TRANSLATIONS_DIR="$PWD/translations"
DATA_DIR="$PWD/data"
# 1. Create project folder & translations folder
# 1. Create folders
mkdir -p "$PROJECT_DIR"/{templates,static}
mkdir -p "$TRANSLATIONS_DIR"/de/LC_MESSAGES
mkdir -p "$TRANSLATIONS_DIR"/en/LC_MESSAGES
@ -111,7 +110,7 @@ pillow
gunicorn
EOL
# 3. Create .env file in parent directory
# 3. .env Datei in Parent-VFolder
cd ..
SECRET_KEY=$(python3 -c 'import secrets; print(secrets.token_hex(24))')
REDEEM_SECRET=$(python3 -c 'import secrets; print(secrets.token_hex(16))')
@ -154,13 +153,11 @@ MATRIX_ACCESS_TOKEN=""
MATRIX_ROOM_ID=""
EOL
# Back to project directory
cd $PROJECT_DIR
# 4. app.py (the main app)
cat <<'PYTHON_END' > app.py
import os
import logging
import warnings
from sqlalchemy.exc import LegacyAPIWarning
warnings.simplefilter("ignore", category=LegacyAPIWarning)
@ -202,8 +199,11 @@ from reportlab.lib.utils import ImageReader
from reportlab.lib.units import cm, inch, mm
from io import BytesIO
import reportlab.lib
import logging
logging.basicConfig()
logging.getLogger('babel').setLevel(logging.DEBUG)
app = Flask(__name__)
csrf = CSRFProtect(app)
convention = {
@ -223,14 +223,19 @@ load_dotenv(override=True)
# App-Configuration
app.config.update(
SECRET_KEY=os.getenv('SECRET_KEY'),
SQLALCHEMY_DATABASE_URI=('sqlite:////app/data/games.db'),
SQLALCHEMY_DATABASE_URI='sqlite:////app/data/games.db',
SQLALCHEMY_TRACK_MODIFICATIONS=False,
BABEL_DEFAULT_LOCALE=os.getenv('BABEL_DEFAULT_LOCALE'),
BABEL_SUPPORTED_LOCALES=os.getenv('BABEL_SUPPORTED_LOCALES').split(','),
BABEL_TRANSLATION_DIRECTORIES=os.getenv('BABEL_TRANSLATION_DIRECTORIES'),
SESSION_COOKIE_SECURE=os.getenv('SESSION_COOKIE_SECURE') == 'True',
WTF_CSRF_ENABLED=os.getenv('CSRF_ENABLED') == 'True',
REGISTRATION_ENABLED=os.getenv('REGISTRATION_ENABLED', 'True').lower() == 'true'
BABEL_DEFAULT_LOCALE=os.getenv('BABEL_DEFAULT_LOCALE', 'en'),
BABEL_SUPPORTED_LOCALES=os.getenv('BABEL_SUPPORTED_LOCALES', 'de,en').split(','),
BABEL_TRANSLATION_DIRECTORIES=os.path.join(app.root_path, 'translations'),
SESSION_COOKIE_SECURE=os.getenv('SESSION_COOKIE_SECURE', 'False') == 'True',
SESSION_COOKIE_SAMESITE='Lax',
PERMANENT_SESSION_LIFETIME=timedelta(days=30),
SESSION_REFRESH_EACH_REQUEST=False,
WTF_CSRF_ENABLED=os.getenv('CSRF_ENABLED', 'True') == 'True',
REGISTRATION_ENABLED=os.getenv('REGISTRATION_ENABLED', 'True').lower() == 'true',
SEND_FILE_MAX_AGE_DEFAULT=int(os.getenv('SEND_FILE_MAX_AGE_DEFAULT', 0)),
TEMPLATES_AUTO_RELOAD=os.getenv('TEMPLATES_AUTO_RELOAD', 'True') == 'True'
)
interval_hours = int(os.getenv('CHECK_EXPIRING_KEYS_INTERVAL_HOURS', 12))
@ -252,6 +257,11 @@ def get_locale():
return session['lang']
return request.accept_languages.best_match(app.config['BABEL_SUPPORTED_LOCALES'])
@app.before_request
def reload_translations():
if app.config['DEBUG']:
babel.reload()
@app.context_processor
def inject_template_vars():
return dict(
@ -718,6 +728,13 @@ def redeem_page(token):
redeem_token=redeem_token,
platform_link='https://store.steampowered.com/account/registerkey?key=' if game.steam_appid else 'https://www.gog.com/redeem')
@app.route('/debug-session')
def debug_session():
return jsonify({
'session_lang': session.get('lang'),
'config_locales': app.config['BABEL_SUPPORTED_LOCALES']
})
# Benachrichtigungsfunktionen
def send_pushover_notification(user, game):
"""Sendet Pushover-Benachrichtigung für ablaufenden Key"""
@ -897,46 +914,44 @@ services:
- TZ=${TZ}
volumes:
- ../data:/app/data
- ../translations:/app/translations
- ../translations:/app/translations:rw
- ../.env:/app/.env
user: "1000:1000"
user: "${UID}:${GID}"
restart: unless-stopped
COMPOSE_END
# 7. Directories and permissions
mkdir -p ../data ../translations
chmod -R a+rwX ../data ../translations
find ../data ../translations -type d -exec chmod 775 {} \;
find ../data ../translations -type f -exec chmod 664 {} \;
# 8. Translation and upgrade scripts
cat <<'SCRIPT_END' > ../translate.sh
#!/bin/bash
set -e
# 0.1 Change to the project directory (where docker-compose.yml is located)
cd "$(dirname "$0")/steam-gift-manager"
declare -A locales=(
["de"]="de"
["en"]="en"
)
declare -a locales=("de" "en")
# create POT-file
docker-compose exec steam-manager pybabel extract -F babel.cfg -o translations/messages.pot .
# 1. POT-Datei aktualisieren
docker-compose run --rm steam-manager pybabel extract -F babel.cfg -o translations/messages.pot .
# Check for each language and initialize if necessary
for lang in "${!locales[@]}"; do
if [ ! -f "translations/${locales[$lang]}/LC_MESSAGES/messages.po" ]; then
docker-compose exec steam-manager pybabel init \
-i translations/messages.pot \
-d translations \
-l "${locales[$lang]}"
fi
# 2. PO files for each language
for lang in "${locales[@]}"; do
docker-compose run --rm steam-manager pybabel update \
-i translations/messages.pot \
-d translations \
-l $lang --previous
done
# Update and compile translations
docker-compose exec steam-manager pybabel update -i translations/messages.pot -d translations
docker-compose exec steam-manager pybabel compile -d translations
# 3. Compile MO files (without fuzzy entries)
docker-compose run --rm steam-manager pybabel compile -d translations
echo "✅ Translations updated!"
echo "✅ Translations successfully updated!"
SCRIPT_END
chmod +x ../translate.sh
@ -1005,6 +1020,8 @@ cat <<HTML_END > templates/base.html
<label class="form-check-label" for="darkModeSwitch">{{ _('Dark Mode') }}</label>
</div>
<div class="dropdown ms-3">
<!-- DEBUG: Current locale {{ get_locale() }} -->
<div hidden id="locale-debug" data-locale="{{ get_locale() }}"></div>
<button class="btn btn-outline-secondary dropdown-toggle" type="button" data-bs-toggle="dropdown" aria-expanded="false">
{% if get_locale() == 'de' %} Deutsch {% elif get_locale() == 'en' %} English {% else %} Sprache {% endif %}
</button>