Profile statements i18n

Implements #2669 and closes #1659. Also refactors and cleans up a lot of stuff, including some of the JS code.
It doesn't matter what `langs` contains, `psycopg` handles the escaping.