Merge branch 'jk/forbid-lf-in-git-url' into maint
[git/debian.git] / gettext.c
blob1b564216d03f6a7bb75fb4c867ad1b0e5cfbecd0
1 /*
2 * Copyright (c) 2010 Ævar Arnfjörð Bjarmason
3 */
5 #include "cache.h"
6 #include "exec-cmd.h"
7 #include "gettext.h"
8 #include "strbuf.h"
9 #include "utf8.h"
10 #include "config.h"
12 #ifndef NO_GETTEXT
13 # include <locale.h>
14 # include <libintl.h>
15 # ifdef GIT_WINDOWS_NATIVE
17 static const char *locale_charset(void)
19 const char *env = getenv("LC_ALL"), *dot;
21 if (!env || !*env)
22 env = getenv("LC_CTYPE");
23 if (!env || !*env)
24 env = getenv("LANG");
26 if (!env)
27 return "UTF-8";
29 dot = strchr(env, '.');
30 return !dot ? env : dot + 1;
33 # elif defined HAVE_LIBCHARSET_H
34 # include <libcharset.h>
35 # else
36 # include <langinfo.h>
37 # define locale_charset() nl_langinfo(CODESET)
38 # endif
39 #endif
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)
51 const char *retval;
53 retval = getenv("LANGUAGE");
54 if (retval && *retval)
55 return retval;
57 #ifndef NO_GETTEXT
58 retval = setlocale(LC_MESSAGES, NULL);
59 if (retval && *retval &&
60 strcmp(retval, "C") &&
61 strcmp(retval, "POSIX"))
62 return retval;
63 #endif
65 return NULL;
68 int use_gettext_poison(void)
70 static int poison_requested = -1;
71 if (poison_requested == -1)
72 poison_requested = git_env_bool("GIT_TEST_GETTEXT_POISON", 0);
73 return poison_requested;
76 #ifndef NO_GETTEXT
77 static int test_vsnprintf(const char *fmt, ...)
79 char buf[26];
80 int ret;
81 va_list ap;
82 va_start(ap, fmt);
83 ret = vsnprintf(buf, sizeof(buf), fmt, ap);
84 va_end(ap);
85 return ret;
88 static void init_gettext_charset(const char *domain)
90 setlocale(LC_CTYPE, "");
91 charset = locale_charset();
92 bind_textdomain_codeset(domain, charset);
95 * Work around an old bug fixed in glibc 2.17 (released on
96 * 2012-12-24), at the cost of potentially making translated
97 * messages from external functions like perror() emitted in
98 * the wrong encoding.
100 * The bug affected e.g. git.git's own 7eb93c89651 ([PATCH]
101 * Simplify git script, 2005-09-07), which is the origin of
102 * the "David_K\345gedal" test string.
104 * See a much longer comment added to this file in 5e9637c6297
105 * (i18n: add infrastructure for translating Git with gettext,
106 * 2011-11-18) for more details.
108 if (test_vsnprintf("%.*s", 13, "David_K\345gedal") < 0)
109 setlocale(LC_CTYPE, "C");
112 void git_setup_gettext(void)
114 const char *podir = getenv(GIT_TEXT_DOMAIN_DIR_ENVIRONMENT);
115 char *p = NULL;
117 if (!podir)
118 podir = p = system_path(GIT_LOCALE_PATH);
120 use_gettext_poison(); /* getenv() reentrancy paranoia */
122 if (!is_directory(podir)) {
123 free(p);
124 return;
127 bindtextdomain("git", podir);
128 setlocale(LC_MESSAGES, "");
129 setlocale(LC_TIME, "");
130 init_gettext_charset("git");
131 textdomain("git");
133 free(p);
136 /* return the number of columns of string 's' in current locale */
137 int gettext_width(const char *s)
139 static int is_utf8 = -1;
140 if (is_utf8 == -1)
141 is_utf8 = is_utf8_locale();
143 return is_utf8 ? utf8_strwidth(s) : strlen(s);
145 #endif
147 int is_utf8_locale(void)
149 #ifdef NO_GETTEXT
150 if (!charset) {
151 const char *env = getenv("LC_ALL");
152 if (!env || !*env)
153 env = getenv("LC_CTYPE");
154 if (!env || !*env)
155 env = getenv("LANG");
156 if (!env)
157 env = "";
158 if (strchr(env, '.'))
159 env = strchr(env, '.') + 1;
160 charset = xstrdup(env);
162 #endif
163 return is_encoding_utf8(charset);