tests: make GIT_TEST_GETTEXT_POISON a boolean
[git] / gettext.c
1 /*
2  * Copyright (c) 2010 Ævar Arnfjörð Bjarmason
3  */
4
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"
11
12 #ifndef NO_GETTEXT
13 #       include <locale.h>
14 #       include <libintl.h>
15 #       ifdef HAVE_LIBCHARSET_H
16 #               include <libcharset.h>
17 #       else
18 #               include <langinfo.h>
19 #               define locale_charset() nl_langinfo(CODESET)
20 #       endif
21 #endif
22
23 static const char *charset;
24
25 /*
26  * Guess the user's preferred languages from the value in LANGUAGE environment
27  * variable and LC_MESSAGES locale category if NO_GETTEXT is not defined.
28  *
29  * The result can be a colon-separated list like "ko:ja:en".
30  */
31 const char *get_preferred_languages(void)
32 {
33         const char *retval;
34
35         retval = getenv("LANGUAGE");
36         if (retval && *retval)
37                 return retval;
38
39 #ifndef NO_GETTEXT
40         retval = setlocale(LC_MESSAGES, NULL);
41         if (retval && *retval &&
42                 strcmp(retval, "C") &&
43                 strcmp(retval, "POSIX"))
44                 return retval;
45 #endif
46
47         return NULL;
48 }
49
50 int use_gettext_poison(void)
51 {
52         static int poison_requested = -1;
53         if (poison_requested == -1)
54                 poison_requested = git_env_bool("GIT_TEST_GETTEXT_POISON", 0);
55         return poison_requested;
56 }
57
58 #ifndef NO_GETTEXT
59 static int test_vsnprintf(const char *fmt, ...)
60 {
61         char buf[26];
62         int ret;
63         va_list ap;
64         va_start(ap, fmt);
65         ret = vsnprintf(buf, sizeof(buf), fmt, ap);
66         va_end(ap);
67         return ret;
68 }
69
70 static void init_gettext_charset(const char *domain)
71 {
72         /*
73            This trick arranges for messages to be emitted in the user's
74            requested encoding, but avoids setting LC_CTYPE from the
75            environment for the whole program.
76
77            This primarily done to avoid a bug in vsnprintf in the GNU C
78            Library [1]. which triggered a "your vsnprintf is broken" error
79            on Git's own repository when inspecting v0.99.6~1 under a UTF-8
80            locale.
81
82            That commit contains a ISO-8859-1 encoded author name, which
83            the locale aware vsnprintf(3) won't interpolate in the format
84            argument, due to mismatch between the data encoding and the
85            locale.
86
87            Even if it wasn't for that bug we wouldn't want to use LC_CTYPE at
88            this point, because it'd require auditing all the code that uses C
89            functions whose semantics are modified by LC_CTYPE.
90
91            But only setting LC_MESSAGES as we do creates a problem, since
92            we declare the encoding of our PO files[2] the gettext
93            implementation will try to recode it to the user's locale, but
94            without LC_CTYPE it'll emit something like this on 'git init'
95            under the Icelandic locale:
96
97                Bj? til t?ma Git lind ? /hlagh/.git/
98
99            Gettext knows about the encoding of our PO file, but we haven't
100            told it about the user's encoding, so all the non-US-ASCII
101            characters get encoded to question marks.
102
103            But we're in luck! We can set LC_CTYPE from the environment
104            only while we call nl_langinfo and
105            bind_textdomain_codeset. That suffices to tell gettext what
106            encoding it should emit in, so it'll now say:
107
108                Bjó til tóma Git lind í /hlagh/.git/
109
110            And the equivalent ISO-8859-1 string will be emitted under a
111            ISO-8859-1 locale.
112
113            With this change way we get the advantages of setting LC_CTYPE
114            (talk to the user in his language/encoding), without the major
115            drawbacks (changed semantics for C functions we rely on).
116
117            However foreign functions using other message catalogs that
118            aren't using our neat trick will still have a problem, e.g. if
119            we have to call perror(3):
120
121            #include <stdio.h>
122            #include <locale.h>
123            #include <errno.h>
124
125            int main(void)
126            {
127                    setlocale(LC_MESSAGES, "");
128                    setlocale(LC_CTYPE, "C");
129                    errno = ENODEV;
130                    perror("test");
131                    return 0;
132            }
133
134            Running that will give you a message with question marks:
135
136            $ LANGUAGE= LANG=de_DE.utf8 ./test
137            test: Kein passendes Ger?t gefunden
138
139            The vsnprintf bug has been fixed since glibc 2.17.
140
141            Then we could simply set LC_CTYPE from the environment, which would
142            make things like the external perror(3) messages work.
143
144            See t/t0203-gettext-setlocale-sanity.sh's "gettext.c" tests for
145            regression tests.
146
147            1. http://sourceware.org/bugzilla/show_bug.cgi?id=6530
148            2. E.g. "Content-Type: text/plain; charset=UTF-8\n" in po/is.po
149         */
150         setlocale(LC_CTYPE, "");
151         charset = locale_charset();
152         bind_textdomain_codeset(domain, charset);
153         /* the string is taken from v0.99.6~1 */
154         if (test_vsnprintf("%.*s", 13, "David_K\345gedal") < 0)
155                 setlocale(LC_CTYPE, "C");
156 }
157
158 void git_setup_gettext(void)
159 {
160         const char *podir = getenv(GIT_TEXT_DOMAIN_DIR_ENVIRONMENT);
161         char *p = NULL;
162
163         if (!podir)
164                 podir = p = system_path(GIT_LOCALE_PATH);
165
166         use_gettext_poison(); /* getenv() reentrancy paranoia */
167
168         if (!is_directory(podir)) {
169                 free(p);
170                 return;
171         }
172
173         bindtextdomain("git", podir);
174         setlocale(LC_MESSAGES, "");
175         setlocale(LC_TIME, "");
176         init_gettext_charset("git");
177         textdomain("git");
178
179         free(p);
180 }
181
182 /* return the number of columns of string 's' in current locale */
183 int gettext_width(const char *s)
184 {
185         static int is_utf8 = -1;
186         if (is_utf8 == -1)
187                 is_utf8 = is_utf8_locale();
188
189         return is_utf8 ? utf8_strwidth(s) : strlen(s);
190 }
191 #endif
192
193 int is_utf8_locale(void)
194 {
195 #ifdef NO_GETTEXT
196         if (!charset) {
197                 const char *env = getenv("LC_ALL");
198                 if (!env || !*env)
199                         env = getenv("LC_CTYPE");
200                 if (!env || !*env)
201                         env = getenv("LANG");
202                 if (!env)
203                         env = "";
204                 if (strchr(env, '.'))
205                         env = strchr(env, '.') + 1;
206                 charset = xstrdup(env);
207         }
208 #endif
209         return is_encoding_utf8(charset);
210 }