Move as much script out of templates as possible, so that jshint can run, and gettext strings are reduced.

See #1918 for details.  Completing this PR will move us extremely close to publishing the latest .pot file to crowdin.

@aronasorman a quick look?