python-babel / flask-babel

i18n and l10n support for Flask based on Babel and pytz
https://python-babel.github.io/flask-babel/
Other
432 stars 159 forks source link

how to get specific string translation (which is not currently the locale)? #172

Closed alnikolaj closed 3 years ago

alnikolaj commented 3 years ago

Let's say I have a string in English, locale="en", e.g. "computer" and its Spanish translation, locale="es" translation, would be "computadora".

If the website is currently set to English, how could still access that specific string but translated to Spanish without changing the global locale for the whole website?

Something like = gettext('computer', language='es') ?

Thanks

zgoda commented 3 years ago

Use stdlib gettext, like https://docs.python.org/3/library/gettext.html#changing-languages-on-the-fly

TkTech commented 3 years ago

You just use with force_locale('es'): and everything in the block will be changed.