1Password / sys-locale

A small and lightweight Rust library to obtain the active locale on the system.
Apache License 2.0
75 stars 15 forks source link

Query the environment directly instead of using libc's setlocale. #1

Closed mathieulj closed 2 years ago

mathieulj commented 2 years ago

Why

setlocale is unfortunately not safe to use in a multi-threaded application.

How

We can avoid using it altogether by pulling the locale from the environment. Both glibc and FreeBSD's libc simply pull this out of the environment using the first set variable of:

Since we are just using LC_CTYPE when libc has many different locales, we can get the same locale information by just checking three environment variables. glibc does a lot of other operations around including locale alias substitution but those don't end up affecting the name it hands back out of the setlocale call.

Drawbacks

Progdrasil commented 2 years ago

Looks good to me, i appreciate the trait approach for the tests :)