icu4x icon indicating copy to clipboard operation
icu4x copied to clipboard

Fall back to parsing environment variables if setlocale reports C

Open hsivonen opened this issue 8 months ago • 1 comments

ICU4X currently has code for obtaining system locale settings by calling POSIX setlocale with the null pointer as the second argument.

However, this always says C unless the application has first initialized the libc localization subsystem by calling setlocale(LC_ALL, ""). It is not OK for ICU4X to perform that call, because that call is permitted only when the process has a single thread, so performing that call must be the responsibility of the application before spawning additional threads.

ICU4C solves this by parsing the environment variables that setlocale(LC_ALL, "") causes libc to parse on its own if setlocale with the second argument set to the null pointer returns C.

I suggest that ICU4X does the same:

  1. If setlocale for the category of interest with the null pointer as the second argument return the null pointer, C, or POSIX, parse the environment variable for the category and LC_ALL giving them the right precedence.
  2. If those don't exist or says C or POSIX (possibly with some suffix like C.UTF-8), report en-US-posix.

hsivonen avatar May 09 '25 13:05 hsivonen

CC @Finchiedev @robertbastian

hsivonen avatar May 09 '25 13:05 hsivonen