blob: 33696a40b8a11262d090a2eec4dfdce0fed6af0f [file] [log] [blame]
Ævar Arnfjörð Bjarmason65784832011-02-22 23:41:20 +00001/*
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
Jonathan Nieder0c9ea332011-03-09 21:17:58 -060012#if defined(_) || defined(Q_)
13#error "namespace conflict: '_' or 'Q_' is pre-defined?"
Ævar Arnfjörð Bjarmason65784832011-02-22 23:41:20 +000014#endif
15
Ævar Arnfjörð Bjarmason5e9637c2011-11-18 00:14:42 +010016#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
Ævar Arnfjörð Bjarmason65784832011-02-22 23:41:20 +000029#define FORMAT_PRESERVING(n) __attribute__((format_arg(n)))
30
Ævar Arnfjörð Bjarmason5e9637c2011-11-18 00:14:42 +010031#ifndef NO_GETTEXT
32extern void git_setup_gettext(void);
Nguyễn Thái Ngọc Duy754395d2012-09-04 17:39:35 +070033extern int gettext_width(const char *s);
Ævar Arnfjörð Bjarmason5e9637c2011-11-18 00:14:42 +010034#else
35static inline void git_setup_gettext(void)
36{
37}
Nguyễn Thái Ngọc Duy754395d2012-09-04 17:39:35 +070038static inline int gettext_width(const char *s)
39{
40 return strlen(s);
41}
Ævar Arnfjörð Bjarmason5e9637c2011-11-18 00:14:42 +010042#endif
43
Ævar Arnfjörð Bjarmasonbb946bb2011-02-22 23:41:21 +000044#ifdef GETTEXT_POISON
Jonathan Nieder30955222011-02-22 23:41:22 +000045extern int use_gettext_poison(void);
Ævar Arnfjörð Bjarmasonbb946bb2011-02-22 23:41:21 +000046#else
47#define use_gettext_poison() 0
48#endif
49
Ævar Arnfjörð Bjarmason65784832011-02-22 23:41:20 +000050static inline FORMAT_PRESERVING(1) const char *_(const char *msgid)
51{
Thomas Rast0c3a4332012-08-20 20:24:56 +020052 if (!*msgid)
53 return "";
Ævar Arnfjörð Bjarmason5e9637c2011-11-18 00:14:42 +010054 return use_gettext_poison() ? "# GETTEXT POISON #" : gettext(msgid);
Ævar Arnfjörð Bjarmason65784832011-02-22 23:41:20 +000055}
56
Jonathan Nieder0c9ea332011-03-09 21:17:58 -060057static inline FORMAT_PRESERVING(1) FORMAT_PRESERVING(2)
58const char *Q_(const char *msgid, const char *plu, unsigned long n)
59{
60 if (use_gettext_poison())
61 return "# GETTEXT POISON #";
Ævar Arnfjörð Bjarmason5e9637c2011-11-18 00:14:42 +010062 return ngettext(msgid, plu, n);
Jonathan Nieder0c9ea332011-03-09 21:17:58 -060063}
64
Ævar Arnfjörð Bjarmason65784832011-02-22 23:41:20 +000065/* Mark msgid for translation but do not translate it. */
Kyle J. McKay290c8e72015-01-11 12:09:22 -080066#if !USE_PARENS_AROUND_GETTEXT_N
Ramsay Jones642f85f2011-04-07 19:41:48 +010067#define N_(msgid) msgid
Kyle J. McKay290c8e72015-01-11 12:09:22 -080068#else
69/*
70 * Strictly speaking, this will lead to invalid C when
71 * used this way:
72 * static const char s[] = N_("FOO");
73 * which will expand to
74 * static const char s[] = ("FOO");
75 * and in valid C, the initializer on the right hand side must
76 * be without the parentheses. But many compilers do accept it
77 * as a language extension and it will allow us to catch mistakes
78 * like:
79 * static const char *msgs[] = {
80 * N_("one")
81 * N_("two"),
82 * N_("three"),
83 * NULL
84 * };
85 * (notice the missing comma on one of the lines) by forcing
86 * a compilation error, because parenthesised ("one") ("two")
87 * will not get silently turned into ("onetwo").
88 */
89#define N_(msgid) (msgid)
90#endif
Ævar Arnfjörð Bjarmason65784832011-02-22 23:41:20 +000091
Jeff King93f7d912015-02-25 22:04:16 -050092const char *get_preferred_languages(void);
93
Ævar Arnfjörð Bjarmason65784832011-02-22 23:41:20 +000094#endif