Reputation: 123
I'm using Flask-Babel for translating string.
In some templates I'm reading the strings from the database(postgresql). How can I translate the strings from the database using Flask-Babel?
Upvotes: 10
Views: 2545
Reputation: 130
For any modern readers to this question from years ago, one recommendation I'd add is related to both of the other answers already here. Yes, the Babel translations are static, and don't read from the database. And yes, you can define translations ahead of time, and pull them into your presentation layer (JavaScript, or your templates, or whatever). One approach that may work for you (as it does for us) is to create a python script that can be configured to read the distinct values in certain database fields, and save them into a python file that calls gettext() or _() on them, and is scanned by your pybabel process. For example:
from flask_babel import gettext as _
list_Tablename_Fieldname = [ _('Value1'), _('Another') ... ]
Then the column values can be scanned by pybabel, put into your translations files, and used by your application. Also works nicely in combination with Flask-Babel-JS if you need to use the translations from JavaScript, without having to manually import and process your translations, as shown in the answer from Markus.
To maintain your translations, you'd want to periodically rerun your script to get updated database values, and then rescan with pybabel, update the translations, etc. If desired, you could automate some process that performs this when someone makes a change that adds different value, if you have a way to translate those on the fly (like an integration with Google Translate API, or asking the user for the relevant translations).
Another option that we investigated but didn't pursue was to set up a pybabel configuration that references a custom extraction method you've defined that looks directly in your database for translatable messages, as described in https://babel.pocoo.org/en/latest/messages.html
Upvotes: 0
Reputation: 3375
I would suggest having a engineering text in the database. And in your HTML file (or preferably a HTML you can include everywhere) you have a script with the translations:
<script>
translations = { 'WillBringOwnFood': {{ _('Guest will bring their own food')}},
'WantToShareBathroom': {{ _('Guest would like to share bathroom with stranger')}} };
</script>
Now when you receive the engineering string you just do a lookup in your translations
dictionary.
So the .js file would look something like this:
function receiveDBCallback(response) {
$('.guestWishes').text(translations[response]);
}
Then you can use babel as usual to extract your strings. And you will have all your translations in the same .po/mo file.
Upvotes: 1
Reputation: 1065
It's not possible to use Babel in database translations, as database content is dynamic and babel translations are static (they didn't change).
If you read the strings from the database you must save the translations on the database. You can create a translation table, something like (locale, source, destination), and get the translated values with a query.
Upvotes: 2