Have threadsafe locale switching

See #1682.

r? @frank-trampe