anthony-dandrea anthony-dandrea - 29 days ago 16
React JSX Question

How to handle python i18n translations with react

I'm looking into bringing React into my codebase(pyramid, jinja2). It almost looks like a good fit. I'm just confused as to how to handle translations from python. I'm using the

pyramid.i18n
library and I would normally just have
_()
function in context that translates strings like
{{_('Hello')}}
to "Hola" and whatnot. If I moved to React my markup would now be in JS. And I don't want to have Jinja render my JS so it remains static. Is there a good way to handle this?

Answer

Ended up using https://github.com/webpack/i18n-webpack-plugin and serving up the specific bundle file based on a saved language setting.

So on the backend we store in the database which language a user wants to use. We have json files of translations for all the languages & words we support. When compiling the bundle files with this plugin we get a bundle for each language that uses those json files to swap out the translated words. So something like bundle.en.js for english and bundle.es.js for spanish. When a user requests a page we serve up the bundle file associated with their selected language stored in the DB.