{"library":"flask-babel","code":"from flask import Flask, render_template, request\nfrom flask_babel import Babel, gettext\n\napp = Flask(__name__)\napp.config['BABEL_DEFAULT_LOCALE'] = 'en'\napp.config['LANGUAGES'] = {'en': 'English', 'de': 'Deutsch'}\n\nbabel = Babel(app)\n\n@babel.localeselector\ndef get_locale():\n    # Try to guess the language from the user's browser accept header\n    # or use a default/configured locale.\n    return request.accept_languages.best_match(list(app.config['LANGUAGES'].keys()))\n\n@app.route('/')\ndef index():\n    return f\"<h1>{gettext('Hello, World!')}</h1>\"\n\n# To run this example, you would also need a `messages.po` file and compile it.\n# 1. Create a `babel.cfg` file:\n#    [python: **.py]\n#    [jinja2: **/templates/**.html]\n# 2. Extract messages:\n#    pybabel extract -F babel.cfg -o messages.pot .\n# 3. Initialize translation for a language (e.g., German):\n#    pybabel init -i messages.pot -d translations -l de\n# 4. Translate strings in `translations/de/LC_MESSAGES/messages.po`\n# 5. Compile translations:\n#    pybabel compile -d translations\n\nif __name__ == '__main__':\n    app.run(debug=True)","lang":"python","description":"This quickstart demonstrates how to initialize Flask-Babel with a Flask application, set up default locales and available languages, and define a `localeselector` function to determine the user's preferred language. It also includes basic usage of `gettext` for string internationalization and outlines the necessary `pybabel` commands for translation file management.","tag":null,"tag_description":null,"last_tested":"2026-04-24","results":[{"runtime":"python:3.10-alpine","exit_code":1},{"runtime":"python:3.10-slim","exit_code":1},{"runtime":"python:3.11-alpine","exit_code":1},{"runtime":"python:3.11-slim","exit_code":1},{"runtime":"python:3.12-alpine","exit_code":1},{"runtime":"python:3.12-slim","exit_code":1},{"runtime":"python:3.13-alpine","exit_code":1},{"runtime":"python:3.13-slim","exit_code":1},{"runtime":"python:3.9-alpine","exit_code":1},{"runtime":"python:3.9-slim","exit_code":1}]}