2 * Copyright (c) 2010 Ævar Arnfjörð Bjarmason
15 # ifdef GIT_WINDOWS_NATIVE
17 static const char *locale_charset(void)
19 const char *env
= getenv("LC_ALL"), *dot
;
22 env
= getenv("LC_CTYPE");
29 dot
= strchr(env
, '.');
30 return !dot
? env
: dot
+ 1;
33 # elif defined HAVE_LIBCHARSET_H
34 # include <libcharset.h>
36 # include <langinfo.h>
37 # define locale_charset() nl_langinfo(CODESET)
41 static const char *charset
;
44 * Guess the user's preferred languages from the value in LANGUAGE environment
45 * variable and LC_MESSAGES locale category if NO_GETTEXT is not defined.
47 * The result can be a colon-separated list like "ko:ja:en".
49 const char *get_preferred_languages(void)
53 retval
= getenv("LANGUAGE");
54 if (retval
&& *retval
)
58 retval
= setlocale(LC_MESSAGES
, NULL
);
59 if (retval
&& *retval
&&
60 strcmp(retval
, "C") &&
61 strcmp(retval
, "POSIX"))
69 static int test_vsnprintf(const char *fmt
, ...)
75 ret
= vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
80 static void init_gettext_charset(const char *domain
)
82 setlocale(LC_CTYPE
, "");
83 charset
= locale_charset();
84 bind_textdomain_codeset(domain
, charset
);
87 * Work around an old bug fixed in glibc 2.17 (released on
88 * 2012-12-24), at the cost of potentially making translated
89 * messages from external functions like perror() emitted in
92 * The bug affected e.g. git.git's own 7eb93c89651 ([PATCH]
93 * Simplify git script, 2005-09-07), which is the origin of
94 * the "David_K\345gedal" test string.
96 * See a much longer comment added to this file in 5e9637c6297
97 * (i18n: add infrastructure for translating Git with gettext,
98 * 2011-11-18) for more details.
100 if (test_vsnprintf("%.*s", 13, "David_K\345gedal") < 0)
101 setlocale(LC_CTYPE
, "C");
104 void git_setup_gettext(void)
106 const char *podir
= getenv(GIT_TEXT_DOMAIN_DIR_ENVIRONMENT
);
110 podir
= p
= system_path(GIT_LOCALE_PATH
);
112 if (!is_directory(podir
)) {
117 bindtextdomain("git", podir
);
118 setlocale(LC_MESSAGES
, "");
119 setlocale(LC_TIME
, "");
120 init_gettext_charset("git");
126 /* return the number of columns of string 's' in current locale */
127 int gettext_width(const char *s
)
129 static int is_utf8
= -1;
131 is_utf8
= is_utf8_locale();
133 return is_utf8
? utf8_strwidth(s
) : strlen(s
);
137 int is_utf8_locale(void)
141 const char *env
= getenv("LC_ALL");
143 env
= getenv("LC_CTYPE");
145 env
= getenv("LANG");
148 if (strchr(env
, '.'))
149 env
= strchr(env
, '.') + 1;
150 charset
= xstrdup(env
);
153 return is_encoding_utf8(charset
);