Ævar Arnfjörð Bjarmason | 6578483 | 2011-02-22 23:41:20 +0000 | [diff] [blame] | 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 | |
Jonathan Nieder | 0c9ea33 | 2011-03-09 21:17:58 -0600 | [diff] [blame] | 12 | #if defined(_) || defined(Q_) |
| 13 | #error "namespace conflict: '_' or 'Q_' is pre-defined?" |
Ævar Arnfjörð Bjarmason | 6578483 | 2011-02-22 23:41:20 +0000 | [diff] [blame] | 14 | #endif |
| 15 | |
Ævar Arnfjörð Bjarmason | 5e9637c | 2011-11-18 00:14:42 +0100 | [diff] [blame] | 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 | |
Ævar Arnfjörð Bjarmason | 6578483 | 2011-02-22 23:41:20 +0000 | [diff] [blame] | 29 | #define FORMAT_PRESERVING(n) __attribute__((format_arg(n))) |
| 30 | |
Ævar Arnfjörð Bjarmason | 5e9637c | 2011-11-18 00:14:42 +0100 | [diff] [blame] | 31 | #ifndef NO_GETTEXT |
| 32 | extern void git_setup_gettext(void); |
Nguyễn Thái Ngọc Duy | 754395d | 2012-09-04 17:39:35 +0700 | [diff] [blame] | 33 | extern int gettext_width(const char *s); |
Ævar Arnfjörð Bjarmason | 5e9637c | 2011-11-18 00:14:42 +0100 | [diff] [blame] | 34 | #else |
| 35 | static inline void git_setup_gettext(void) |
| 36 | { |
| 37 | } |
Nguyễn Thái Ngọc Duy | 754395d | 2012-09-04 17:39:35 +0700 | [diff] [blame] | 38 | static inline int gettext_width(const char *s) |
| 39 | { |
| 40 | return strlen(s); |
| 41 | } |
Ævar Arnfjörð Bjarmason | 5e9637c | 2011-11-18 00:14:42 +0100 | [diff] [blame] | 42 | #endif |
| 43 | |
Ævar Arnfjörð Bjarmason | bb946bb | 2011-02-22 23:41:21 +0000 | [diff] [blame] | 44 | #ifdef GETTEXT_POISON |
Jonathan Nieder | 3095522 | 2011-02-22 23:41:22 +0000 | [diff] [blame] | 45 | extern int use_gettext_poison(void); |
Ævar Arnfjörð Bjarmason | bb946bb | 2011-02-22 23:41:21 +0000 | [diff] [blame] | 46 | #else |
| 47 | #define use_gettext_poison() 0 |
| 48 | #endif |
| 49 | |
Ævar Arnfjörð Bjarmason | 6578483 | 2011-02-22 23:41:20 +0000 | [diff] [blame] | 50 | static inline FORMAT_PRESERVING(1) const char *_(const char *msgid) |
| 51 | { |
Thomas Rast | 0c3a433 | 2012-08-20 20:24:56 +0200 | [diff] [blame] | 52 | if (!*msgid) |
| 53 | return ""; |
Ævar Arnfjörð Bjarmason | 5e9637c | 2011-11-18 00:14:42 +0100 | [diff] [blame] | 54 | return use_gettext_poison() ? "# GETTEXT POISON #" : gettext(msgid); |
Ævar Arnfjörð Bjarmason | 6578483 | 2011-02-22 23:41:20 +0000 | [diff] [blame] | 55 | } |
| 56 | |
Jonathan Nieder | 0c9ea33 | 2011-03-09 21:17:58 -0600 | [diff] [blame] | 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 #"; |
Ævar Arnfjörð Bjarmason | 5e9637c | 2011-11-18 00:14:42 +0100 | [diff] [blame] | 62 | return ngettext(msgid, plu, n); |
Jonathan Nieder | 0c9ea33 | 2011-03-09 21:17:58 -0600 | [diff] [blame] | 63 | } |
| 64 | |
Ævar Arnfjörð Bjarmason | 6578483 | 2011-02-22 23:41:20 +0000 | [diff] [blame] | 65 | /* Mark msgid for translation but do not translate it. */ |
Kyle J. McKay | 290c8e7 | 2015-01-11 12:09:22 -0800 | [diff] [blame] | 66 | #if !USE_PARENS_AROUND_GETTEXT_N |
Ramsay Jones | 642f85f | 2011-04-07 19:41:48 +0100 | [diff] [blame] | 67 | #define N_(msgid) msgid |
Kyle J. McKay | 290c8e7 | 2015-01-11 12:09:22 -0800 | [diff] [blame] | 68 | #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ð Bjarmason | 6578483 | 2011-02-22 23:41:20 +0000 | [diff] [blame] | 91 | |
Jeff King | 93f7d91 | 2015-02-25 22:04:16 -0500 | [diff] [blame] | 92 | const char *get_preferred_languages(void); |
| 93 | |
Ævar Arnfjörð Bjarmason | 6578483 | 2011-02-22 23:41:20 +0000 | [diff] [blame] | 94 | #endif |