0

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;
}

1 Answers1

0

Unfortunaltely, strerror() doesn't promise to use the locale. From the man page (emphasis mine):

The strerror() function returns a pointer to a string that describes the error code passed in the argument errnum, possibly using the LC_MESSAGES part of the current locale to select the appropriate language. (For example, if errnum is EINVAL, the returned description will be "Invalid argument".) This string must not be modified by the application, but may be modified by a subsequent call to strerror() or strerror_l(). No other library function, including perror(3), will modify this string.

But you could use strerror_l() instead (see the same man page)

Ingo Leonhardt
  • 9,435
  • 2
  • 24
  • 33
  • Thanks for the info, I must have missed it. Changed the code as below after your help, but still does not work: `int fd; locale_t loc = newlocale(LC_MESSAGES_MASK, "tr_TR.UTF-8", (locale_t)0); if ((fd = open("test.dat", O_RDONLY)) == -1) { fprintf(stderr, "open failed: %s\n", strerror_l(errno, loc)); exit(EXIT_FAILURE); } printf("OK\n"); uselocale(LC_GLOBAL_LOCALE); freelocale(loc);` Also tried as `strerror_l(errno, uselocale(loc))` too but it did not work either. – necdetsanli Aug 02 '23 at 10:16
  • strange -- to be honest, I've never used `strerror_l()`, but I think what you've tried should have worked according to man. Maybe turkish translation are simply not provided on your computer? Maybe you want to try with another language just to be sure – Ingo Leonhardt Aug 02 '23 at 11:27
  • Tried with German also but same problem continues. Also checked language files from GNOME desktop language settings and all languages ​​seem to be fully installed. – necdetsanli Aug 02 '23 at 12:13