I want to print error messages in Turkish on Mint Linux which is running on VMware Workstation 17 Player by strerror and fprintf. To do that, I try to call setlocale with necessary arguments as shown below. But somehow, It does not work as expected. strerror continues to return error message strings in English. There is the output of locale -a command also shown below. Can you please help me to understand what is wrong here and what I can do to fix it?
output of locale -a:
C
C.utf8
en_AG
en_AG.utf8
en_AU.utf8
en_BW.utf8
en_CA.utf8
en_DK.utf8
en_GB.utf8
en_HK.utf8
en_IE.utf8
en_IL
en_IL.utf8
en_IN
en_IN.utf8
en_NG
en_NG.utf8
en_NZ.utf8
en_PH.utf8
en_SG.utf8
en_US.utf8
en_ZA.utf8
en_ZM
en_ZM.utf8
en_ZW.utf8
POSIX
tr_TR
tr_TR.iso88599
tr_TR.utf8
turkish
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>
#include <errno.h>
#include <locale.h>
int main(void)
{
int fd;
if (setlocale(LC_MESSAGES, "tr_TR.UTF-8") == NULL) {
fprintf(stderr, "cannot set locale!..\n");
exit(EXIT_FAILURE);
}
if ((fd = open("test.dat", O_RDONLY)) == -1) {
fprintf(stderr, "open failed: %s\n", strerror(errno));
exit(EXIT_FAILURE);
}
printf("OK\n");
return 0;
}
Unfortunaltely,
strerror()
doesn't promise to use the locale. From the man page (emphasis mine):But you could use
strerror_l()
instead (see the same man page)