xzdec: Use setlocale() via tuklib_gettext_setlocale()

xzdec isn't translated and didn't have locale-specific behavior
in the past. On Windows with UTF-8 in the application manifest,
setting the locale makes a difference though:

  - Without any setlocale() call, non-ASCII filenames don't display
    properly in Command Prompt unless one first uses "chcp 65001"
    to set the console code page to UTF-8.

  - setlocale(LC_ALL, "") is enough to make non-ASCII filenames
    print correctly in Command Prompt without using "chcp 65001",
    assuming that the non-UTF-8 code page (like 850) supports
    those non-ASCII characters.

  - setlocale(LC_ALL, ".UTF8") is even better because then mbrtowc() and
    such functions use an UTF-8 locale instead of a legacy code page.
    The tuklib_gettext_setlocale() macro takes care of this (without
    enabling any translations).

Fixes: 46ee006162
(cherry picked from commit 78868b6ed6)
This commit is contained in:
Lasse Collin 2024-12-18 14:23:13 +02:00
parent 4e7a48bf15
commit 4ff609adb0
No known key found for this signature in database
GPG Key ID: 38EE757D69184620
1 changed files with 12 additions and 0 deletions

View File

@ -41,6 +41,7 @@
#endif #endif
#include "getopt.h" #include "getopt.h"
#include "tuklib_gettext.h"
#include "tuklib_progname.h" #include "tuklib_progname.h"
#include "tuklib_exit.h" #include "tuklib_exit.h"
@ -419,6 +420,17 @@ main(int argc, char **argv)
(void)prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0); (void)prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0);
#endif #endif
// We need to set the locale even though we don't have any
// translated messages:
//
// - This is needed on Windows to make non-ASCII filenames display
// properly when the active code page has been set to UTF-8
// in the application manifest. Use the helper macro from
// tuklib_gettext.h instead of plain setlocale(LC_ALL, "")
// because on Windows the standard call isn't enough for
// full UTF-8 support.
tuklib_gettext_setlocale();
// Parse the command line options. // Parse the command line options.
parse_options(argc, argv); parse_options(argc, argv);