t4046: avoid continue in &&-chain for zsh
[alt-git.git] / gettext.c
blob57facbc21ec2545e5bdeb1e738445ad403f419b4
1 /*
2 * Copyright (c) 2010 Ævar Arnfjörð Bjarmason
3 */
5 #include "git-compat-util.h"
6 #include "abspath.h"
7 #include "environment.h"
8 #include "exec-cmd.h"
9 #include "gettext.h"
10 #include "utf8.h"
12 #ifndef NO_GETTEXT
13 # include <libintl.h>
14 # ifdef GIT_WINDOWS_NATIVE
16 static const char *locale_charset(void)
18 const char *env = getenv("LC_ALL"), *dot;
20 if (!env || !*env)
21 env = getenv("LC_CTYPE");
22 if (!env || !*env)
23 env = getenv("LANG");
25 if (!env)
26 return "UTF-8";
28 dot = strchr(env, '.');
29 return !dot ? env : dot + 1;
32 # elif defined HAVE_LIBCHARSET_H
33 # include <libcharset.h>
34 # else
35 # include <langinfo.h>
36 # define locale_charset() nl_langinfo(CODESET)
37 # endif
38 #endif
40 static const char *charset;
43 * Guess the user's preferred languages from the value in LANGUAGE environment
44 * variable and LC_MESSAGES locale category if NO_GETTEXT is not defined.
46 * The result can be a colon-separated list like "ko:ja:en".
48 const char *get_preferred_languages(void)
50 const char *retval;
52 retval = getenv("LANGUAGE");
53 if (retval && *retval)
54 return retval;
56 #ifndef NO_GETTEXT
57 retval = setlocale(LC_MESSAGES, NULL);
58 if (retval && *retval &&
59 strcmp(retval, "C") &&
60 strcmp(retval, "POSIX"))
61 return retval;
62 #endif
64 return NULL;
67 #ifndef NO_GETTEXT
68 __attribute__((format (printf, 1, 2)))
69 static int test_vsnprintf(const char *fmt, ...)
71 char buf[26];
72 int ret;
73 va_list ap;
74 va_start(ap, fmt);
75 ret = vsnprintf(buf, sizeof(buf), fmt, ap);
76 va_end(ap);
77 return ret;
80 static void init_gettext_charset(const char *domain)
82 charset = locale_charset();
83 bind_textdomain_codeset(domain, charset);
86 * Work around an old bug fixed in glibc 2.17 (released on
87 * 2012-12-24), at the cost of potentially making translated
88 * messages from external functions like perror() emitted in
89 * the wrong encoding.
91 * The bug affected e.g. git.git's own 7eb93c89651 ([PATCH]
92 * Simplify git script, 2005-09-07), which is the origin of
93 * the "David_K\345gedal" test string.
95 * See a much longer comment added to this file in 5e9637c6297
96 * (i18n: add infrastructure for translating Git with gettext,
97 * 2011-11-18) for more details.
99 if (test_vsnprintf("%.*s", 13, "David_K\345gedal") < 0)
100 setlocale(LC_CTYPE, "C");
103 int git_gettext_enabled = 0;
105 void git_setup_gettext(void)
107 const char *podir = getenv(GIT_TEXT_DOMAIN_DIR_ENVIRONMENT);
108 char *p = NULL;
110 if (!podir)
111 podir = p = system_path(GIT_LOCALE_PATH);
113 if (!is_directory(podir)) {
114 free(p);
115 return;
118 bindtextdomain("git", podir);
119 setlocale(LC_MESSAGES, "");
120 setlocale(LC_TIME, "");
121 init_gettext_charset("git");
122 textdomain("git");
124 git_gettext_enabled = 1;
126 free(p);
129 /* return the number of columns of string 's' in current locale */
130 int gettext_width(const char *s)
132 static int is_utf8 = -1;
133 if (is_utf8 == -1)
134 is_utf8 = is_utf8_locale();
136 return is_utf8 ? utf8_strwidth(s) : strlen(s);
138 #endif
140 int is_utf8_locale(void)
142 #ifdef NO_GETTEXT
143 if (!charset) {
144 const char *env = getenv("LC_ALL");
145 if (!env || !*env)
146 env = getenv("LC_CTYPE");
147 if (!env || !*env)
148 env = getenv("LANG");
149 if (!env)
150 env = "";
151 if (strchr(env, '.'))
152 env = strchr(env, '.') + 1;
153 charset = xstrdup(env);
155 #endif
156 return is_encoding_utf8(charset);