]> git.ipfire.org Git - thirdparty/git.git/blame - gettext.c
i18n: add infrastructure for translating Git with gettext
[thirdparty/git.git] / gettext.c
CommitLineData
30955229
JN
1/*
2 * Copyright (c) 2010 Ævar Arnfjörð Bjarmason
3 */
4
5#include "git-compat-util.h"
6#include "gettext.h"
7
5e9637c6
ÆAB
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
18
19#ifdef GETTEXT_POISON
30955229
JN
20int use_gettext_poison(void)
21{
22 static int poison_requested = -1;
23 if (poison_requested == -1)
24 poison_requested = getenv("GIT_GETTEXT_POISON") ? 1 : 0;
25 return poison_requested;
26}
5e9637c6
ÆAB
27#endif
28
29#ifndef NO_GETTEXT
30static void init_gettext_charset(const char *domain)
31{
32 const char *charset;
33
34 /*
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.
38
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.
43
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.
48
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.
52
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:
58
59 Bj? til t?ma Git lind ? /hlagh/.git/
60
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.
64
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:
69
70 Bjó til tóma Git lind í /hlagh/.git/
71
72 And the equivalent ISO-8859-1 string will be emitted under a
73 ISO-8859-1 locale.
74
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).
78
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):
82
83 #include <stdio.h>
84 #include <locale.h>
85 #include <errno.h>
86
87 int main(void)
88 {
89 setlocale(LC_MESSAGES, "");
90 setlocale(LC_CTYPE, "C");
91 errno = ENODEV;
92 perror("test");
93 return 0;
94 }
95
96 Running that will give you a message with question marks:
97
98 $ LANGUAGE= LANG=de_DE.utf8 ./test
99 test: Kein passendes Ger?t gefunden
100
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.
104
105 Then we could simply set LC_CTYPE from the environment, which would
106 make things like the external perror(3) messages work.
107
108 See t/t0203-gettext-setlocale-sanity.sh's "gettext.c" tests for
109 regression tests.
110
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
113 */
114 setlocale(LC_CTYPE, "");
115 charset = locale_charset();
116 bind_textdomain_codeset(domain, charset);
117 setlocale(LC_CTYPE, "C");
118}
119
120void git_setup_gettext(void)
121{
122 const char *podir = getenv("GIT_TEXTDOMAINDIR");
123
124 if (!podir)
125 podir = GIT_LOCALE_PATH;
126 bindtextdomain("git", podir);
127 setlocale(LC_MESSAGES, "");
128 init_gettext_charset("git");
129 textdomain("git");
130}
131#endif