translating strings from database flask-babel

笑着哭i 提交于 2019-11-30 20:30:02

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!