3

I was trying to get the thousand separator used in Portuguese-speaking countries, on Windows, compiling my C code with GCC. For this, I was using locale.h's function setlocale(int category, const char* locale), and then localeconv()->thousands_sep.

The GCC manual says the locale names are system-specific. The Microsoft's documentation says that the following options are functionally equivalent, though the first is the recommended one.

  • setlocale( LC_ALL, "en-US" );
  • setlocale( LC_ALL, "English" );
  • setlocale( LC_ALL, "English_United States.1252" );

As I was trying to use Portuguese, I had tried setlocale( LC_ALL , "pt-BR" );. The thousand separator was NULL, which is not usually Portuguese's thousand separator. But when I tried setlocale( LC_ALL, "portuguese" ), it worked!

But... What are the correct locale names on Windows? using GCC, if the compiler matters. Or, is my GCC broken? Or maybe Windows, or maybe me. Or is Microsoft's documentation wrong =]

Schilive
  • 187
  • 7
  • On Windows the native way is a whole set of different functions in kernel32 but here you are asking about the Microsoft Visual Studio C library implementation... – Anders Oct 31 '22 at 21:10
  • @Anders, thank you very much for commenting! But I am sorry: why did you say I am asking about the Microsoft Visual Studio C library implementation? I ask because I am using GCC on CLion, and I believe Microsoft has their own compiler for Microsoft Visual Studio. Though, my link does go to something of MSVC. But Microsoft's documentation on [Win32](https://learn.microsoft.com/en-us/windows/win32/intl/locale-names)'s locale names confirms that “pt-BR” should work, which I do believe to be concording with kernel32. – Schilive Oct 31 '22 at 21:47
  • The WINDOWS locale names are can probably be enumerated by `EnumSystemLocalesEx`. Assuming you are using mingw you might be relying on parts of the Microsoft C implementation (msvcrt.dll etc.) and who knows if they match Windows exactly but one would hope so. – Anders Oct 31 '22 at 23:02
  • @Anders, I now understand. Thank! It is weird they call it “msvcrt”. – Schilive Oct 31 '22 at 23:26
  • 1
    MicroSoft Visual C RunTime probably. – Anders Nov 01 '22 at 00:02

1 Answers1

1

Although Microsoft documentation says that you can use the locale name such as en-US, fr-FR, pt-BR, etc., it looks like Mingw library for gcc can only use full country name and full region name for setlocale. Cygwin is okay with short codes. Here is the test with Mingw gcc:

  • "French_France.1252" -- fine
  • "French_France" -- fine
  • "French" -- fine
  • "fr" -- failed
  • "fr-FR" -- failed
  • "fr-FR.utf8" -- failed

For region Brazil, you have to use "Portuguese_Brazil".

In order to be able to write compatible codes, you'll probably need a short code to full name mapping for both countries and regions and do the translation before call setlocale, if you want your code to be compatible when run in other systems. I am not sure if Windows provides such a mapping function. If not, it is not difficult to write your own.

  • Thank you for the answer! Though it would not be difficult, it would be tiring… which is a big part of the point of macros. It'd interesting to see if in Visual Studio's compiler, all options would work... Thanks! – Schilive Jan 16 '23 at 00:24