git-remote-mediawiki: scripts to install, delete and clear a MediaWiki
[git/gitweb.git] / gettext.c
blobf75bca7f56b7b27c135d92acba816c40211fdece
1 /*
2 * Copyright (c) 2010 Ævar Arnfjörð Bjarmason
3 */
5 #include "git-compat-util.h"
6 #include "gettext.h"
8 #ifndef NO_GETTEXT
9 # include <locale.h>
10 # include <libintl.h>
11 # ifdef HAVE_LIBCHARSET_H
12 # include <libcharset.h>
13 # else
14 # include <langinfo.h>
15 # define locale_charset() nl_langinfo(CODESET)
16 # endif
17 #endif
19 #ifdef GETTEXT_POISON
20 int use_gettext_poison(void)
22 static int poison_requested = -1;
23 if (poison_requested == -1)
24 poison_requested = getenv("GIT_GETTEXT_POISON") ? 1 : 0;
25 return poison_requested;
27 #endif
29 #ifndef NO_GETTEXT
30 static void init_gettext_charset(const char *domain)
32 const char *charset;
35 This trick arranges for messages to be emitted in the user's
36 requested encoding, but avoids setting LC_CTYPE from the
37 environment for the whole program.
39 This primarily done to avoid a bug in vsnprintf in the GNU C
40 Library [1]. which triggered a "your vsnprintf is broken" error
41 on Git's own repository when inspecting v0.99.6~1 under a UTF-8
42 locale.
44 That commit contains a ISO-8859-1 encoded author name, which
45 the locale aware vsnprintf(3) won't interpolate in the format
46 argument, due to mismatch between the data encoding and the
47 locale.
49 Even if it wasn't for that bug we wouldn't want to use LC_CTYPE at
50 this point, because it'd require auditing all the code that uses C
51 functions whose semantics are modified by LC_CTYPE.
53 But only setting LC_MESSAGES as we do creates a problem, since
54 we declare the encoding of our PO files[2] the gettext
55 implementation will try to recode it to the user's locale, but
56 without LC_CTYPE it'll emit something like this on 'git init'
57 under the Icelandic locale:
59 Bj? til t?ma Git lind ? /hlagh/.git/
61 Gettext knows about the encoding of our PO file, but we haven't
62 told it about the user's encoding, so all the non-US-ASCII
63 characters get encoded to question marks.
65 But we're in luck! We can set LC_CTYPE from the environment
66 only while we call nl_langinfo and
67 bind_textdomain_codeset. That suffices to tell gettext what
68 encoding it should emit in, so it'll now say:
70 Bjó til tóma Git lind í /hlagh/.git/
72 And the equivalent ISO-8859-1 string will be emitted under a
73 ISO-8859-1 locale.
75 With this change way we get the advantages of setting LC_CTYPE
76 (talk to the user in his language/encoding), without the major
77 drawbacks (changed semantics for C functions we rely on).
79 However foreign functions using other message catalogs that
80 aren't using our neat trick will still have a problem, e.g. if
81 we have to call perror(3):
83 #include <stdio.h>
84 #include <locale.h>
85 #include <errno.h>
87 int main(void)
89 setlocale(LC_MESSAGES, "");
90 setlocale(LC_CTYPE, "C");
91 errno = ENODEV;
92 perror("test");
93 return 0;
96 Running that will give you a message with question marks:
98 $ LANGUAGE= LANG=de_DE.utf8 ./test
99 test: Kein passendes Ger?t gefunden
101 In the long term we should probably see about getting that
102 vsnprintf bug in glibc fixed, and audit our code so it won't
103 fall apart under a non-C locale.
105 Then we could simply set LC_CTYPE from the environment, which would
106 make things like the external perror(3) messages work.
108 See t/t0203-gettext-setlocale-sanity.sh's "gettext.c" tests for
109 regression tests.
111 1. http://sourceware.org/bugzilla/show_bug.cgi?id=6530
112 2. E.g. "Content-Type: text/plain; charset=UTF-8\n" in po/is.po
114 setlocale(LC_CTYPE, "");
115 charset = locale_charset();
116 bind_textdomain_codeset(domain, charset);
117 setlocale(LC_CTYPE, "C");
120 void git_setup_gettext(void)
122 const char *podir = getenv("GIT_TEXTDOMAINDIR");
124 if (!podir)
125 podir = GIT_LOCALE_PATH;
126 bindtextdomain("git", podir);
127 setlocale(LC_MESSAGES, "");
128 init_gettext_charset("git");
129 textdomain("git");
131 #endif