Properly handle locales with country codes

Fixes #2890.