From 9fde7cd75e843ca60e7b66d777b5af59fdedac52 Mon Sep 17 00:00:00 2001 From: nocci Date: Fri, 2 May 2025 15:55:38 +0200 Subject: [PATCH] prepare switching from babel to .json translations --- setup.sh | 60 ++++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 48 insertions(+), 12 deletions(-) diff --git a/setup.sh b/setup.sh index 9c5cafe..0eb9bbb 100644 --- a/setup.sh +++ b/setup.sh @@ -81,8 +81,7 @@ DATA_DIR="$PWD/data" # 1. Create folders mkdir -p "$PROJECT_DIR"/{templates,static} -mkdir -p "$TRANSLATIONS_DIR"/de/LC_MESSAGES -mkdir -p "$TRANSLATIONS_DIR"/en/LC_MESSAGES +mkdir -p "$TRANSLATIONS_DIR" mkdir -p "$DATA_DIR" chmod -R a+rwX "$TRANSLATIONS_DIR" "$DATA_DIR" @@ -98,7 +97,6 @@ flask-migrate werkzeug python-dotenv flask-sqlalchemy -flask-babel jinja2<3.1.0 itsdangerous sqlalchemy @@ -122,10 +120,9 @@ SECRET_KEY="$SECRET_KEY" REDEEM_SECRET="$REDEEM_SECRET" WTF_CSRF_SECRET_KEY="$REDEEM_CSRF" -# locales -BABEL_DEFAULT_LOCALE="en" -BABEL_SUPPORTED_LOCALES="de,en" -BABEL_TRANSLATION_DIRECTORIES="translations" +# Language Settings +DEFAULT_LANGUAGE="en" +SUPPORTED_LANGUAGES="de,en" # Timezone TZ=Europe/Berlin @@ -201,6 +198,18 @@ logging.basicConfig() logging.getLogger('babel').setLevel(logging.DEBUG) app = Flask(__name__) +# Load Languages +TRANSLATIONS = {} +for lang in app.config['SUPPORTED_LANGUAGES'].split(','): + try: + with open(f'translations/{lang}.json') as f: + TRANSLATIONS[lang] = json.load(f) + except: + TRANSLATIONS[lang] = {} + +def translate(key, lang=app.config['DEFAULT_LANGUAGE'], **kwargs): + return TRANSLATIONS.get(lang, {}).get(key, key).format(**kwargs) + csrf = CSRFProtect(app) convention = { @@ -267,6 +276,12 @@ def reload_translations(): babel.reload() @app.context_processor +def inject_translations(): + def _(key, **kwargs): + lang = session.get('lang', app.config['DEFAULT_LANGUAGE']) + return translate(key, lang, **kwargs) + return {'_': _} + def inject_template_vars(): return dict( get_locale=get_locale, @@ -338,9 +353,9 @@ def index(): @app.route('/set-lang/') def set_lang(lang): - if lang in app.config['BABEL_SUPPORTED_LOCALES']: + if lang in app.config['SUPPORTED_LANGUAGES'].split(','): session['lang'] = lang - return redirect(request.referrer or url_for('index')) + return redirect(request.referrer) @app.route('/set-theme/') def set_theme(theme): @@ -875,7 +890,7 @@ services: - TZ=${TZ} volumes: - ../data:/app/data - - ../translations:/app/translations:rw + - ../translations:/app/translations - ../.env:/app/.env user: "${UID}:${GID}" restart: unless-stopped @@ -889,6 +904,25 @@ find ../data ../translations -type d -exec chmod 775 {} \; find ../data ../translations -type f -exec chmod 664 {} \; # 8. Translation and upgrade scripts +cat < "$TRANSLATIONS_DIR/de.json" +{ + "Game List": "Spieleliste", + "Add New Game": "Neues Spiel hinzufügen", + "Search": "Suche", + "Welcome %(username)s!": "Willkommen %(username)s!" +} +JSON_END + +cat < "$TRANSLATIONS_DIR/en.json" +{ + "Game List": "Game List", + "Add New Game": "Add New Game", + "Search": "Search", + "Welcome %(username)s!": "Welcome %(username)s!" +} +JSON_END + + cat <<'SCRIPT_END' > ../translate.sh #!/bin/bash set -e @@ -1816,8 +1850,10 @@ echo -e "nano .env" echo -e "\n\033[1;32m✅ After you are done start the system with:\033[0m" echo -e "cd steam-gift-manager" echo -e "docker-compose build --no-cache && docker-compose up -d" -echo -e "\nGenerate translations: ./translate.sh" -echo -e "You can edit them in translations/en/LC_MESSAGES/messages.po" +echo -e "\n${GREEN}✅ JSON-based translations!${NC}" +echo -e "you can edit them here:" +echo -e " - translations/de.json" +echo -e " - translations/en.json" echo -e "Enter your Apprise URLs in .env at APPRISE_URLS (e.g. for Pushover, Gotify, Matrix etc.)" echo -e "\nAfter any change in you configuration, .env or even translations:" echo -e "cd steam-gift-manager"