summaryrefslogtreecommitdiff
path: root/gettext.c
diff options
context:
space:
mode:
authorKarsten Blees <blees@dcon.de>2019-07-03 13:46:04 -0700
committerJunio C Hamano <gitster@pobox.com>2019-07-03 14:03:32 -0700
commit090d1e84771bb4a310e3fe8291ec71b0ddb03d4f (patch)
tree1afe8e1c180b326e733ddfaf190e183f82b87e89 /gettext.c
parent8dca754b1e874719a732bc9ab7b0e14b21b1bc10 (diff)
downloadgit-090d1e84771bb4a310e3fe8291ec71b0ddb03d4f.tar.gz
gettext: always use UTF-8 on native Windows
On native Windows, Git exclusively uses UTF-8 for console output (both with MinTTY and native Win32 Console). Gettext uses `setlocale()` to determine the output encoding for translated text, however, MSVCRT's `setlocale()` does not support UTF-8. As a result, translated text is encoded in system encoding (as per `GetAPC()`), and non-ASCII chars are mangled in console output. Side note: There is actually a code page for UTF-8: 65001. In practice, it does not work as expected at least on Windows 7, though, so we cannot use it in Git. Besides, if we overrode the code page, any process spawned from Git would inherit that code page (as opposed to the code page configured for the current user), which would quite possibly break e.g. diff or merge helpers. So we really cannot override the code page. In `init_gettext_charset()`, Git calls gettext's `bind_textdomain_codeset()` with the character set obtained via `locale_charset()`; Let's override that latter function to force the encoding to UTF-8 on native Windows. In Git for Windows' SDK, there is a `libcharset.h` and therefore we define `HAVE_LIBCHARSET_H` in the MINGW-specific section in `config.mak.uname`, therefore we need to add the override before that conditionally-compiled code block. Rather than simply defining `locale_charset()` to return the string `"UTF-8"`, though, we are careful not to break `LC_ALL=C`: the `ab/no-kwset` patch series, for example, needs to have a way to prevent Git from expecting UTF-8-encoded input. Signed-off-by: Karsten Blees <blees@dcon.de> Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'gettext.c')
-rw-r--r--gettext.c20
1 files changed, 19 insertions, 1 deletions
diff --git a/gettext.c b/gettext.c
index d4021d690c..3f2aca5c3b 100644
--- a/gettext.c
+++ b/gettext.c
@@ -12,7 +12,25 @@
#ifndef NO_GETTEXT
# include <locale.h>
# include <libintl.h>
-# ifdef HAVE_LIBCHARSET_H
+# ifdef GIT_WINDOWS_NATIVE
+
+static const char *locale_charset(void)
+{
+ const char *env = getenv("LC_ALL"), *dot;
+
+ if (!env || !*env)
+ env = getenv("LC_CTYPE");
+ if (!env || !*env)
+ env = getenv("LANG");
+
+ if (!env)
+ return "UTF-8";
+
+ dot = strchr(env, '.');
+ return !dot ? env : dot + 1;
+}
+
+# elif defined HAVE_LIBCHARSET_H
# include <libcharset.h>
# else
# include <langinfo.h>