msysGit-based Git for Windows 1.x was superseded by Git for Windows 2.x
[git/mingw/4msysgit.git] / gettext.c
blob8b2da4641f8b20ff3cc70c63636de878eec72470
1 /*
2 * Copyright (c) 2010 Ævar Arnfjörð Bjarmason
3 */
5 #include "git-compat-util.h"
6 #include "gettext.h"
7 #include "strbuf.h"
8 #include "utf8.h"
10 #ifndef NO_GETTEXT
11 # include <locale.h>
12 # include <libintl.h>
13 # ifdef HAVE_LIBCHARSET_H
14 # include <libcharset.h>
15 # else
16 # include <langinfo.h>
17 # define locale_charset() nl_langinfo(CODESET)
18 # endif
19 #endif
21 #ifdef GETTEXT_POISON
22 int use_gettext_poison(void)
24 static int poison_requested = -1;
25 if (poison_requested == -1)
26 poison_requested = getenv("GIT_GETTEXT_POISON") ? 1 : 0;
27 return poison_requested;
29 #endif
31 #ifndef NO_GETTEXT
32 static int test_vsnprintf(const char *fmt, ...)
34 char buf[26];
35 int ret;
36 va_list ap;
37 va_start(ap, fmt);
38 ret = vsnprintf(buf, sizeof(buf), fmt, ap);
39 va_end(ap);
40 return ret;
43 static const char *charset;
44 static void init_gettext_charset(const char *domain)
47 This trick arranges for messages to be emitted in the user's
48 requested encoding, but avoids setting LC_CTYPE from the
49 environment for the whole program.
51 This primarily done to avoid a bug in vsnprintf in the GNU C
52 Library [1]. which triggered a "your vsnprintf is broken" error
53 on Git's own repository when inspecting v0.99.6~1 under a UTF-8
54 locale.
56 That commit contains a ISO-8859-1 encoded author name, which
57 the locale aware vsnprintf(3) won't interpolate in the format
58 argument, due to mismatch between the data encoding and the
59 locale.
61 Even if it wasn't for that bug we wouldn't want to use LC_CTYPE at
62 this point, because it'd require auditing all the code that uses C
63 functions whose semantics are modified by LC_CTYPE.
65 But only setting LC_MESSAGES as we do creates a problem, since
66 we declare the encoding of our PO files[2] the gettext
67 implementation will try to recode it to the user's locale, but
68 without LC_CTYPE it'll emit something like this on 'git init'
69 under the Icelandic locale:
71 Bj? til t?ma Git lind ? /hlagh/.git/
73 Gettext knows about the encoding of our PO file, but we haven't
74 told it about the user's encoding, so all the non-US-ASCII
75 characters get encoded to question marks.
77 But we're in luck! We can set LC_CTYPE from the environment
78 only while we call nl_langinfo and
79 bind_textdomain_codeset. That suffices to tell gettext what
80 encoding it should emit in, so it'll now say:
82 Bjó til tóma Git lind í /hlagh/.git/
84 And the equivalent ISO-8859-1 string will be emitted under a
85 ISO-8859-1 locale.
87 With this change way we get the advantages of setting LC_CTYPE
88 (talk to the user in his language/encoding), without the major
89 drawbacks (changed semantics for C functions we rely on).
91 However foreign functions using other message catalogs that
92 aren't using our neat trick will still have a problem, e.g. if
93 we have to call perror(3):
95 #include <stdio.h>
96 #include <locale.h>
97 #include <errno.h>
99 int main(void)
101 setlocale(LC_MESSAGES, "");
102 setlocale(LC_CTYPE, "C");
103 errno = ENODEV;
104 perror("test");
105 return 0;
108 Running that will give you a message with question marks:
110 $ LANGUAGE= LANG=de_DE.utf8 ./test
111 test: Kein passendes Ger?t gefunden
113 The vsnprintf bug has been fixed since glibc 2.17.
115 Then we could simply set LC_CTYPE from the environment, which would
116 make things like the external perror(3) messages work.
118 See t/t0203-gettext-setlocale-sanity.sh's "gettext.c" tests for
119 regression tests.
121 1. http://sourceware.org/bugzilla/show_bug.cgi?id=6530
122 2. E.g. "Content-Type: text/plain; charset=UTF-8\n" in po/is.po
124 setlocale(LC_CTYPE, "");
125 charset = locale_charset();
126 bind_textdomain_codeset(domain, charset);
127 /* the string is taken from v0.99.6~1 */
128 if (test_vsnprintf("%.*s", 13, "David_K\345gedal") < 0)
129 setlocale(LC_CTYPE, "C");
132 void git_setup_gettext(void)
134 const char *podir = getenv("GIT_TEXTDOMAINDIR");
136 if (!podir)
137 podir = GIT_LOCALE_PATH;
138 bindtextdomain("git", podir);
139 setlocale(LC_MESSAGES, "");
140 init_gettext_charset("git");
141 textdomain("git");
144 /* return the number of columns of string 's' in current locale */
145 int gettext_width(const char *s)
147 static int is_utf8 = -1;
148 if (is_utf8 == -1)
149 is_utf8 = !strcmp(charset, "UTF-8");
151 return is_utf8 ? utf8_strwidth(s) : strlen(s);
153 #endif