Verify that we're actually using a UTF-8 locale and switch to en_US.UTF-8 if not

Should fix #1587 and #1675, but I was never able to reproduce either of
them so maybe not.
Fix compilation with non-16-bit wchar_t