]> git.ipfire.org Git - thirdparty/git.git/blob - gettext.h
Merge branch 'avoid-using-uninitialized-gettext'
[thirdparty/git.git] / gettext.h
1 /*
2 * Copyright (c) 2010-2011 Ævar Arnfjörð Bjarmason
3 *
4 * This is a skeleton no-op implementation of gettext for Git.
5 * You can replace it with something that uses libintl.h and wraps
6 * gettext() to try out the translations.
7 */
8
9 #ifndef GETTEXT_H
10 #define GETTEXT_H
11
12 #if defined(_) || defined(Q_)
13 #error "namespace conflict: '_' or 'Q_' is pre-defined?"
14 #endif
15
16 #ifndef NO_GETTEXT
17 # include <libintl.h>
18 #else
19 # ifdef gettext
20 # undef gettext
21 # endif
22 # define gettext(s) (s)
23 # ifdef ngettext
24 # undef ngettext
25 # endif
26 # define ngettext(s, p, n) ((n == 1) ? (s) : (p))
27 #endif
28
29 #define FORMAT_PRESERVING(n) __attribute__((format_arg(n)))
30
31 int use_gettext_poison(void);
32
33 #ifndef NO_GETTEXT
34 extern int git_gettext_enabled;
35 void git_setup_gettext(void);
36 int gettext_width(const char *s);
37 #else
38 #define git_gettext_enabled (0)
39 static inline void git_setup_gettext(void)
40 {
41 use_gettext_poison(); /* getenv() reentrancy paranoia */
42 }
43 static inline int gettext_width(const char *s)
44 {
45 return strlen(s);
46 }
47 #endif
48
49 static inline FORMAT_PRESERVING(1) const char *_(const char *msgid)
50 {
51 if (!*msgid)
52 return "";
53 return use_gettext_poison() ? "# GETTEXT POISON #" :
54 !git_gettext_enabled ? msgid : gettext(msgid);
55 }
56
57 static inline FORMAT_PRESERVING(1) FORMAT_PRESERVING(2)
58 const char *Q_(const char *msgid, const char *plu, unsigned long n)
59 {
60 if (use_gettext_poison())
61 return "# GETTEXT POISON #";
62 if (!git_gettext_enabled)
63 return n == 1 ? msgid : plu;
64 return ngettext(msgid, plu, n);
65 }
66
67 /* Mark msgid for translation but do not translate it. */
68 #if !USE_PARENS_AROUND_GETTEXT_N
69 #define N_(msgid) msgid
70 #else
71 /*
72 * Strictly speaking, this will lead to invalid C when
73 * used this way:
74 * static const char s[] = N_("FOO");
75 * which will expand to
76 * static const char s[] = ("FOO");
77 * and in valid C, the initializer on the right hand side must
78 * be without the parentheses. But many compilers do accept it
79 * as a language extension and it will allow us to catch mistakes
80 * like:
81 * static const char *msgs[] = {
82 * N_("one")
83 * N_("two"),
84 * N_("three"),
85 * NULL
86 * };
87 * (notice the missing comma on one of the lines) by forcing
88 * a compilation error, because parenthesised ("one") ("two")
89 * will not get silently turned into ("onetwo").
90 */
91 #define N_(msgid) (msgid)
92 #endif
93
94 const char *get_preferred_languages(void);
95 int is_utf8_locale(void);
96
97 #endif