For a little side project I need to output strings of text in Windows' CMD that may be localized, and some strings are read from the arguments of the program. To simplify matters I'll be using a simple echo program as a demonstration.
Please consider snippet in C language:
#include <stdio.h>
int main(int argc, char **argv) {
// Display the first argument through the standard output:
if (argc > 1)
puts(argv[1]);
return 0;
}
These are the outputs of two executions:
$ test.exe Wilhelm
$ Wilhelm
$ test.exe Röntgen
$ R÷ntgen
There you can already see that things like ö
which would be out of ASCII would not be displayed correctly. But they're correctly recognized in the program, for example if you do something like:
if (argv[1][1] == 'ö')
puts("It is.");
The sentence would be displayed, so the program is receiving the characters correctly.
So I though, OK, that wchar_t
thing may be needed, so making the appropriate changes and defined UNICODE
and _UNICODE
you'd get:
#include <stdio.h>
int wmain(int argc, wchar_t **argv) {
// Display the first argument through the standard output:
if (argc > 1)
_putws(argv[1]);
return 0;
}
Still the output of this test program would be the same.
Looking around and reading docs I found somewhat of a workaround, which is to set the locale to English for example: the text would then be displayed correctly. Modifying the first version (without wchar_t
s) I ended up with this:
#include <stdio.h>
#include <locale.h>
int main(int argc, char **argv) {
// Get the previous locale and change to English:
char *old_locale = setlocale(LC_ALL, NULL);
setlocale(LC_ALL, "English");
// Display the first argument through the standard output:
if (argc > 1)
puts(argv[1]);
// Restore locale:
setlocale(LC_ALL, old_locale);
return 0;
}
("en-US"
doesn't seem to work in MinGW-w64 while "English"
works with it and Microsoft Visual C++)
Now the program is able to print so that the character is actually displayed correctly in the command line window.
The problem is that setting things to English is not the best thing to do in a Spanish system, or a Japanese one for example. So I thought about getting the locale from the system in some way. I found a function called _get_current_locale
which returns a _locale_t
, but it seems not to be what I wanted at all:
_locale_t_variable->locinfo->lc_category[LC_ALL].locale
(which is a char *
) seems to be NULL
.
So the question is, how to get or display text in the locale of the command line? What would be the right way to deal with localized text in Windows' CMD (not necessarily in Unicode)?