最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

internationalization - Is setlocale() in C restricted to locales shown by “locale -a”? - Stack Overflow

matteradmin3PV0评论

It seems like setlocale() in C (at least on Linux) only works if the locale you’re trying to set it to is one of those listed in the output of locale -a. Is this really true?

The reason I ask to confirm is that it seems like a potentially unpleasant restriction.

  • For example, my fairly standard Ubuntu installation only outputs English and C/Posix locales for locale -a. Yet in Java, by contrast, the list of available locales does not seem constrained by what’s installed in the OS (many languages’ and countries’ locales are available).

It seems like setlocale() in C (at least on Linux) only works if the locale you’re trying to set it to is one of those listed in the output of locale -a. Is this really true?

The reason I ask to confirm is that it seems like a potentially unpleasant restriction.

  • For example, my fairly standard Ubuntu installation only outputs English and C/Posix locales for locale -a. Yet in Java, by contrast, the list of available locales does not seem constrained by what’s installed in the OS (many languages’ and countries’ locales are available).
Share asked 23 hours ago NikSNikS 1545 bronze badges New contributor NikS is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct. 4
  • 1 Glibc manual states The set of locales supported depends on the operating system you are using, and so do their names, except that the standard locale called ‘C’ or ‘POSIX’ always exist. So your assumption seems correct. Available locales are determined by the OS and your custom definitions. – Martin Fischer Commented 22 hours ago
  • 1 Java delivers a locale database. Ubuntu has something like locale-all to install all locales. So it is up the administrator to choose the locales to install. it is not a limitation of C, but an old problem about disk usage – Giacomo Catenazzi Commented 22 hours ago
  • 1 Also note: locale are handled by system, but if you want java style, you can use a different library like Unicode ICU – Giacomo Catenazzi Commented 22 hours ago
  • 1 Linux and glibc indeed fail to distinguish the C runtime library and the OS. There's no fundamental reason for this; another C library can ship a Java-like locale database instead of just "C". – MSalters Commented 57 mins ago
Add a comment  | 

1 Answer 1

Reset to default 1

This is because locale -a merely lists the locales installed on the system. On Debian/Ubuntu based systems for example it looks for them under /usr/lib/locale. If a locale does not exist on the system, then you won't be able to use it, neither via setlocale() nor via any other means.

Post a comment

comment list (0)

  1. No comments so far