]> git.ipfire.org Git - thirdparty/git.git/blame - gettext.c
environment: move comment_line_char from cache.h
[thirdparty/git.git] / gettext.c
CommitLineData
30955229
JN
1/*
2 * Copyright (c) 2010 Ævar Arnfjörð Bjarmason
3 */
4
226c0ddd 5#include "cache.h"
92034a9c 6#include "exec-cmd.h"
30955229 7#include "gettext.h"
754395d3
NTND
8#include "strbuf.h"
9#include "utf8.h"
6cdccfce 10#include "config.h"
30955229 11
5e9637c6 12#ifndef NO_GETTEXT
5e9637c6 13# include <libintl.h>
090d1e84
KB
14# ifdef GIT_WINDOWS_NATIVE
15
16static const char *locale_charset(void)
17{
18 const char *env = getenv("LC_ALL"), *dot;
19
20 if (!env || !*env)
21 env = getenv("LC_CTYPE");
22 if (!env || !*env)
23 env = getenv("LANG");
24
25 if (!env)
26 return "UTF-8";
27
28 dot = strchr(env, '.');
29 return !dot ? env : dot + 1;
30}
31
32# elif defined HAVE_LIBCHARSET_H
5e9637c6
ÆAB
33# include <libcharset.h>
34# else
35# include <langinfo.h>
36# define locale_charset() nl_langinfo(CODESET)
37# endif
38#endif
39
e8c16726
NTND
40static const char *charset;
41
93f7d910
JK
42/*
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.
45 *
46 * The result can be a colon-separated list like "ko:ja:en".
47 */
48const char *get_preferred_languages(void)
49{
50 const char *retval;
51
52 retval = getenv("LANGUAGE");
53 if (retval && *retval)
54 return retval;
55
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
63
64 return NULL;
65}
66
5e9637c6 67#ifndef NO_GETTEXT
48ca53ca 68__attribute__((format (printf, 1, 2)))
9c0495d2
NTND
69static int test_vsnprintf(const char *fmt, ...)
70{
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;
78}
79
5e9637c6
ÆAB
80static void init_gettext_charset(const char *domain)
81{
5e9637c6
ÆAB
82 charset = locale_charset();
83 bind_textdomain_codeset(domain, charset);
9371c0e9
ÆAB
84
85 /*
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.
90 *
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.
94 *
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.
98 */
9c0495d2
NTND
99 if (test_vsnprintf("%.*s", 13, "David_K\345gedal") < 0)
100 setlocale(LC_CTYPE, "C");
5e9637c6
ÆAB
101}
102
103void git_setup_gettext(void)
104{
226c0ddd 105 const char *podir = getenv(GIT_TEXT_DOMAIN_DIR_ENVIRONMENT);
0210231b 106 char *p = NULL;
5e9637c6
ÆAB
107
108 if (!podir)
0210231b 109 podir = p = system_path(GIT_LOCALE_PATH);
226c0ddd 110
0210231b
JS
111 if (!is_directory(podir)) {
112 free(p);
cc5e1bf9 113 return;
0210231b 114 }
226c0ddd 115
5e9637c6
ÆAB
116 bindtextdomain("git", podir);
117 setlocale(LC_MESSAGES, "");
aa1462cc 118 setlocale(LC_TIME, "");
5e9637c6
ÆAB
119 init_gettext_charset("git");
120 textdomain("git");
0210231b
JS
121
122 free(p);
5e9637c6 123}
754395d3
NTND
124
125/* return the number of columns of string 's' in current locale */
126int gettext_width(const char *s)
127{
128 static int is_utf8 = -1;
129 if (is_utf8 == -1)
e8c16726 130 is_utf8 = is_utf8_locale();
754395d3
NTND
131
132 return is_utf8 ? utf8_strwidth(s) : strlen(s);
133}
5e9637c6 134#endif
e8c16726
NTND
135
136int is_utf8_locale(void)
137{
138#ifdef NO_GETTEXT
139 if (!charset) {
140 const char *env = getenv("LC_ALL");
141 if (!env || !*env)
142 env = getenv("LC_CTYPE");
143 if (!env || !*env)
144 env = getenv("LANG");
145 if (!env)
146 env = "";
147 if (strchr(env, '.'))
148 env = strchr(env, '.') + 1;
149 charset = xstrdup(env);
150 }
151#endif
152 return is_encoding_utf8(charset);
153}