setlocale() 对 macOS 没有影响

Sza*_*lcs 0 c macos locale

I am using the example program for setlocale() from cppreference.com on a macOS 10.14.6 system, however, the output is not localized. I checked that the return value from setlocale() is not NULL and tried a variety of locale specifications in an attempt to get a decimal comma, such as de, de_DE, de_DE.utf8.

Why does this not work?

How can I find out why the output isn't affected, and whether it is because of incorrect locale names?

On the same OS, locale changes from Python do appear to work (as in that they affect number printing by C code running within the same process). Thus I do not think that the problem is that this locale is not supported.


The full program:

#include <stdio.h>
#include <locale.h>
#include <time.h>
#include <wchar.h>
 
int main(void)
{
    // the C locale will be UTF-8 enabled English;
    // decimal dot will be German
    // date and time formatting will be Japanese
    setlocale(LC_ALL, "en_US.UTF-8");
    setlocale(LC_NUMERIC, "de_DE.utf8");
    setlocale(LC_TIME, "ja_JP.utf8");
 
    wchar_t str[100];
    time_t t = time(NULL);
    wcsftime(str, 100, L"%A %c", localtime(&t));
    wprintf(L"Number: %.2f\nDate: %ls\n", 3.14, str);
}
Run Code Online (Sandbox Code Playgroud)

Oka*_*Oka 5

从终端,locale -a可用于查看所有可用的区域设置。

\n

在我的系统1上,locale -a | grep de_产生以下结果

\n
de_CH\nde_DE.UTF-8\nde_AT.ISO8859-1\nde_AT.UTF-8\nde_AT.ISO8859-15\nde_DE.ISO8859-15\nde_CH.UTF-8\nde_DE-A.ISO8859-1\nde_CH.ISO8859-15\nde_DE.ISO8859-1\nde_CH.ISO8859-1\nde_AT\nde_DE\n
Run Code Online (Sandbox Code Playgroud)\n

setlocaleNULL如果参数组合没有意义,则返回。在我的系统1上,

\n
#include <stdio.h>\n#include <locale.h>\n#include <time.h>\n#include <wchar.h>\n\nint main(void)\n{\n    // the C locale will be UTF-8 enabled English;\n    // decimal dot will be German\n    // date and time formatting will be Japanese\n    char *all = setlocale(LC_ALL, "en_US.UTF-8");\n    char *num = setlocale(LC_NUMERIC, "de_DE.utf8");\n    char *tim = setlocale(LC_TIME, "ja_JP.utf8");\n\n    printf("%s\\n%s\\n%s\\n", all, num, tim);\n\n    wchar_t str[100];\n    time_t t = time(NULL);\n    wcsftime(str, 100, L"%A %c", localtime(&t));\n    wprintf(L"Number: %.2f\\nDate: %ls\\n", 3.14, str);\n}\n
Run Code Online (Sandbox Code Playgroud)\n

输出

\n
#include <stdio.h>\n#include <locale.h>\n#include <time.h>\n#include <wchar.h>\n\nint main(void)\n{\n    // the C locale will be UTF-8 enabled English;\n    // decimal dot will be German\n    // date and time formatting will be Japanese\n    char *all = setlocale(LC_ALL, "en_US.UTF-8");\n    char *num = setlocale(LC_NUMERIC, "de_DE.utf8");\n    char *tim = setlocale(LC_TIME, "ja_JP.utf8");\n\n    printf("%s\\n%s\\n%s\\n", all, num, tim);\n\n    wchar_t str[100];\n    time_t t = time(NULL);\n    wcsftime(str, 100, L"%A %c", localtime(&t));\n    wprintf(L"Number: %.2f\\nDate: %ls\\n", 3.14, str);\n}\n
Run Code Online (Sandbox Code Playgroud)\n

使用先前列表中的区域设置

\n
#include <stdio.h>\n#include <locale.h>\n#include <time.h>\n#include <wchar.h>\n\nint main(void)\n{\n    // the C locale will be UTF-8 enabled English;\n    // decimal dot will be German\n    // date and time formatting will be Japanese\n    char *all = setlocale(LC_ALL, "en_US.UTF-8");\n    char *num = setlocale(LC_NUMERIC, "de_DE.UTF-8");\n    char *tim = setlocale(LC_TIME, "ja_JP.UTF-8");\n\n    printf("%s\\n%s\\n%s\\n", all, num, tim);\n\n    wchar_t str[100];\n    time_t t = time(NULL);\n    wcsftime(str, 100, L"%A %c", localtime(&t));\n    wprintf(L"Number: %.2f\\nDate: %ls\\n", 3.14, str);\n}\n
Run Code Online (Sandbox Code Playgroud)\n

输出

\n
en_US.UTF-8\n(null)\n(null)\nNumber: 3.14\nDate: Thursday Thu May 12 08:35:37 2022\n
Run Code Online (Sandbox Code Playgroud)\n
\n

1请注意,这是在 macOS 10.15.7 上测试的。在 macOS 10.14.6 上,结果可能有所不同。您不应该盲目相信您找到的示例(包括我的示例),而应该检查您自己系统的区域设置。

\n

locale自 Mac OS X 10.4 起可用。

\n