Commit e8c16726 authored by Duy Nguyen's avatar Duy Nguyen Committed by Junio C Hamano

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
Helped-by: Ævar Arnfjörð Bjarmason's avatarÆvar Arnfjörð Bjarmason <[email protected]>
Signed-off-by: Duy Nguyen's avatarNguyễn Thái Ngọc Duy <[email protected]>
Signed-off-by: default avatarJunio C Hamano <[email protected]>
parent 9d9babb8
......@@ -18,6 +18,8 @@
# endif
static const char *charset;
* Guess the user's preferred languages from the value in LANGUAGE environment
* variable and LC_MESSAGES locale category if NO_GETTEXT is not defined.
......@@ -65,7 +67,6 @@ static int test_vsnprintf(const char *fmt, ...)
return ret;
static const char *charset;
static void init_gettext_charset(const char *domain)
......@@ -172,8 +173,27 @@ int gettext_width(const char *s)
static int is_utf8 = -1;
if (is_utf8 == -1)
is_utf8 = !strcmp(charset, "UTF-8");
is_utf8 = is_utf8_locale();
return is_utf8 ? utf8_strwidth(s) : strlen(s);
int is_utf8_locale(void)
if (!charset) {
const char *env = getenv("LC_ALL");
if (!env || !*env)
env = getenv("LC_CTYPE");
if (!env || !*env)
env = getenv("LANG");
if (!env)
env = "";
if (strchr(env, '.'))
env = strchr(env, '.') + 1;
charset = xstrdup(env);
return is_encoding_utf8(charset);
......@@ -90,5 +90,6 @@ const char *Q_(const char *msgid, const char *plu, unsigned long n)
const char *get_preferred_languages(void);
extern int is_utf8_locale(void);
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment