gettext: add is_utf8_locale()
This function returns true if git is running under an UTF-8 locale. pcre in the next patch will need this. is_encoding_utf8() is used instead of strcmp() to catch both "utf-8" and "utf8" suffixes. When built with no gettext support, we peek in several env variables to detect UTF-8. pcre library might support utf-8 even if libc is built without locale support.. The peeking code is a copy from compat/regex/regcomp.c Helped-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com> Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Nguyễn Thái Ngọc Duy committed
Jun 25, 2016 at 07:22 UTC
e8c1672655dcff59aaf0f78fa256b1d2e3f1ba9b
2 files changed
+23
-2
gettext.c
+22
-2
@@ -18,6 +18,8 @@
18
# endif
19
#endif
20
21
+static const char *charset;
22
+
23
/*
24
* Guess the user's preferred languages from the value in LANGUAGE environment
25
* variable and LC_MESSAGES locale category if NO_GETTEXT is not defined.
@@ -65,7 +67,6 @@ static int test_vsnprintf(const char *fmt, ...)
67
return ret;
68
}
69
68
-static const char *charset;
70
static void init_gettext_charset(const char *domain)
71
{
72
/*
@@ -172,8 +173,27 @@ int gettext_width(const char *s)
173
{
174
static int is_utf8 = -1;
175
if (is_utf8 == -1)
175
- is_utf8 = !strcmp(charset, "UTF-8");
176
+ is_utf8 = is_utf8_locale();
177
178
return is_utf8 ? utf8_strwidth(s) : strlen(s);
179
}
180
#endif
181
+
182
+int is_utf8_locale(void)
183
+{
184
+#ifdef NO_GETTEXT
185
+ if (!charset) {
186
+ const char *env = getenv("LC_ALL");
187
+ if (!env || !*env)
188
+ env = getenv("LC_CTYPE");
189
+ if (!env || !*env)
190
+ env = getenv("LANG");
191
+ if (!env)
192
+ env = "";
193
+ if (strchr(env, '.'))
194
+ env = strchr(env, '.') + 1;
195
+ charset = xstrdup(env);
196
+ }
197
+#endif
198
+ return is_encoding_utf8(charset);
199
+}
gettext.h
+1
@@ -90,5 +90,6 @@ const char *Q_(const char *msgid, const char *plu, unsigned long n)
90
#endif
91
92
const char *get_preferred_languages(void);
93
+extern int is_utf8_locale(void);
94
95
#endif