Skip to content

Commit e8c1672

Browse files
pcloudsgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Nguyễn Thái Ngọc Duy <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 9d9babb commit e8c1672

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

gettext.c

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
# endif
1919
#endif
2020

21+
static const char *charset;
22+
2123
/*
2224
* Guess the user's preferred languages from the value in LANGUAGE environment
2325
* variable and LC_MESSAGES locale category if NO_GETTEXT is not defined.
@@ -65,7 +67,6 @@ static int test_vsnprintf(const char *fmt, ...)
6567
return ret;
6668
}
6769

68-
static const char *charset;
6970
static void init_gettext_charset(const char *domain)
7071
{
7172
/*
@@ -172,8 +173,27 @@ int gettext_width(const char *s)
172173
{
173174
static int is_utf8 = -1;
174175
if (is_utf8 == -1)
175-
is_utf8 = !strcmp(charset, "UTF-8");
176+
is_utf8 = is_utf8_locale();
176177

177178
return is_utf8 ? utf8_strwidth(s) : strlen(s);
178179
}
179180
#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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,5 +90,6 @@ const char *Q_(const char *msgid, const char *plu, unsigned long n)
9090
#endif
9191

9292
const char *get_preferred_languages(void);
93+
extern int is_utf8_locale(void);
9394

9495
#endif

0 commit comments

Comments
 (0)