August 11, 2021

How to Surround Highlighted Text With a Custom Snippet

In order to mark strings as “translatable” in Flask via Flask-WTF, you need to apply a special syntax. This means, e.g. in a Jinja template a string like Conference has to be transformed into {{ _('Conference') }}. This is a very tedious work, so I created a shortcut for it. Add the following lines to your keybindings.json: "key": "ctrl+shift+alt+0", "command": "editor.action.insertSnippet", "when": "editorHasSelection || editorHasMultipleSelections", "args": { "snippet": "{{ _('${TM_SELECTED_TEXT}') }}" } I used “ctrl+shift+alt+0” as on keyboard with a German layout the closing curly brace is on the same key as the 0....

September 4, 2020

How Long Does Flask Keep User Generated Data

Easy! Either Flask persists the data into a database or a file, or it does not. Wait, not so fast. There is also session scoped data (e.g. storing data in a cookie or similar) app scoped data (e.g. store data on the app object) The caveats session scoped data is only available for one client, and as long the session lasts app scoped data is only available within a single app - usually, e....

August 14, 2020

How Can You Translate the Login Message of Flask-Login

When you use Flask-Login, the default login message comes from the plugin. This means - you do no set it, you cannot translate it. solution Set the very same (or a different) message! login = LoginManager(app) login.login_view = 'login' login.login_message = _l('Please log in to access this page.') via https://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-xiii-i18n-and-l10n

August 13, 2020

How Can You Inject the Language as String Into a Jinja Template

I created a multilingual project using Flask with the help of Flask-Babel. While the translation mechanism via GNU gettext itself is pretty straightforward, I did not know how to inject the actual language into the Jinja template, so I can use it to set <html lang="xxx">. While it is odd that this is not mentioned in the documentation, once again StackOverflow offered solutions. solution one Just translate the language! <html lang="{{ _('en') }}"> That was too simple to think of :-)...