prepare switching from babel to .json translations

This commit is contained in:
nocci 2025-05-02 15:55:38 +02:00
parent 4f23096002
commit 9fde7cd75e
1 changed files with 48 additions and 12 deletions

View File

@ -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/<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/<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 <<JSON_END > "$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 <<JSON_END > "$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"