Fix non-unicode strings and locales handling

Fixes #1071 and #1072.