THIS IS A TEST INSTANCE ONLY! REPOSITORIES CAN BE DELETED AT ANY TIME!

Git Source Code Mirror - This is a publish-only repository and all pull requests are ignored. Please follow Documentation/SubmittingPatches procedure for any of your improvements.
git
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

229 lines
5.6KB

  1. /*
  2. * Copyright (c) 2010 Ævar Arnfjörð Bjarmason
  3. */
  4. #include "cache.h"
  5. #include "exec-cmd.h"
  6. #include "gettext.h"
  7. #include "strbuf.h"
  8. #include "utf8.h"
  9. #include "config.h"
  10. #ifndef NO_GETTEXT
  11. # include <locale.h>
  12. # include <libintl.h>
  13. # ifdef GIT_WINDOWS_NATIVE
  14. static const char *locale_charset(void)
  15. {
  16. const char *env = getenv("LC_ALL"), *dot;
  17. if (!env || !*env)
  18. env = getenv("LC_CTYPE");
  19. if (!env || !*env)
  20. env = getenv("LANG");
  21. if (!env)
  22. return "UTF-8";
  23. dot = strchr(env, '.');
  24. return !dot ? env : dot + 1;
  25. }
  26. # elif defined HAVE_LIBCHARSET_H
  27. # include <libcharset.h>
  28. # else
  29. # include <langinfo.h>
  30. # define locale_charset() nl_langinfo(CODESET)
  31. # endif
  32. #endif
  33. static const char *charset;
  34. /*
  35. * Guess the user's preferred languages from the value in LANGUAGE environment
  36. * variable and LC_MESSAGES locale category if NO_GETTEXT is not defined.
  37. *
  38. * The result can be a colon-separated list like "ko:ja:en".
  39. */
  40. const char *get_preferred_languages(void)
  41. {
  42. const char *retval;
  43. retval = getenv("LANGUAGE");
  44. if (retval && *retval)
  45. return retval;
  46. #ifndef NO_GETTEXT
  47. retval = setlocale(LC_MESSAGES, NULL);
  48. if (retval && *retval &&
  49. strcmp(retval, "C") &&
  50. strcmp(retval, "POSIX"))
  51. return retval;
  52. #endif
  53. return NULL;
  54. }
  55. int use_gettext_poison(void)
  56. {
  57. static int poison_requested = -1;
  58. if (poison_requested == -1)
  59. poison_requested = git_env_bool("GIT_TEST_GETTEXT_POISON", 0);
  60. return poison_requested;
  61. }
  62. #ifndef NO_GETTEXT
  63. static int test_vsnprintf(const char *fmt, ...)
  64. {
  65. char buf[26];
  66. int ret;
  67. va_list ap;
  68. va_start(ap, fmt);
  69. ret = vsnprintf(buf, sizeof(buf), fmt, ap);
  70. va_end(ap);
  71. return ret;
  72. }
  73. static void init_gettext_charset(const char *domain)
  74. {
  75. /*
  76. This trick arranges for messages to be emitted in the user's
  77. requested encoding, but avoids setting LC_CTYPE from the
  78. environment for the whole program.
  79. This primarily done to avoid a bug in vsnprintf in the GNU C
  80. Library [1]. which triggered a "your vsnprintf is broken" error
  81. on Git's own repository when inspecting v0.99.6~1 under a UTF-8
  82. locale.
  83. That commit contains a ISO-8859-1 encoded author name, which
  84. the locale aware vsnprintf(3) won't interpolate in the format
  85. argument, due to mismatch between the data encoding and the
  86. locale.
  87. Even if it wasn't for that bug we wouldn't want to use LC_CTYPE at
  88. this point, because it'd require auditing all the code that uses C
  89. functions whose semantics are modified by LC_CTYPE.
  90. But only setting LC_MESSAGES as we do creates a problem, since
  91. we declare the encoding of our PO files[2] the gettext
  92. implementation will try to recode it to the user's locale, but
  93. without LC_CTYPE it'll emit something like this on 'git init'
  94. under the Icelandic locale:
  95. Bj? til t?ma Git lind ? /hlagh/.git/
  96. Gettext knows about the encoding of our PO file, but we haven't
  97. told it about the user's encoding, so all the non-US-ASCII
  98. characters get encoded to question marks.
  99. But we're in luck! We can set LC_CTYPE from the environment
  100. only while we call nl_langinfo and
  101. bind_textdomain_codeset. That suffices to tell gettext what
  102. encoding it should emit in, so it'll now say:
  103. Bjó til tóma Git lind í /hlagh/.git/
  104. And the equivalent ISO-8859-1 string will be emitted under a
  105. ISO-8859-1 locale.
  106. With this change way we get the advantages of setting LC_CTYPE
  107. (talk to the user in his language/encoding), without the major
  108. drawbacks (changed semantics for C functions we rely on).
  109. However foreign functions using other message catalogs that
  110. aren't using our neat trick will still have a problem, e.g. if
  111. we have to call perror(3):
  112. #include <stdio.h>
  113. #include <locale.h>
  114. #include <errno.h>
  115. int main(void)
  116. {
  117. setlocale(LC_MESSAGES, "");
  118. setlocale(LC_CTYPE, "C");
  119. errno = ENODEV;
  120. perror("test");
  121. return 0;
  122. }
  123. Running that will give you a message with question marks:
  124. $ LANGUAGE= LANG=de_DE.utf8 ./test
  125. test: Kein passendes Ger?t gefunden
  126. The vsnprintf bug has been fixed since glibc 2.17.
  127. Then we could simply set LC_CTYPE from the environment, which would
  128. make things like the external perror(3) messages work.
  129. See t/t0203-gettext-setlocale-sanity.sh's "gettext.c" tests for
  130. regression tests.
  131. 1. http://sourceware.org/bugzilla/show_bug.cgi?id=6530
  132. 2. E.g. "Content-Type: text/plain; charset=UTF-8\n" in po/is.po
  133. */
  134. setlocale(LC_CTYPE, "");
  135. charset = locale_charset();
  136. bind_textdomain_codeset(domain, charset);
  137. /* the string is taken from v0.99.6~1 */
  138. if (test_vsnprintf("%.*s", 13, "David_K\345gedal") < 0)
  139. setlocale(LC_CTYPE, "C");
  140. }
  141. void git_setup_gettext(void)
  142. {
  143. const char *podir = getenv(GIT_TEXT_DOMAIN_DIR_ENVIRONMENT);
  144. char *p = NULL;
  145. if (!podir)
  146. podir = p = system_path(GIT_LOCALE_PATH);
  147. use_gettext_poison(); /* getenv() reentrancy paranoia */
  148. if (!is_directory(podir)) {
  149. free(p);
  150. return;
  151. }
  152. bindtextdomain("git", podir);
  153. setlocale(LC_MESSAGES, "");
  154. setlocale(LC_TIME, "");
  155. init_gettext_charset("git");
  156. textdomain("git");
  157. free(p);
  158. }
  159. /* return the number of columns of string 's' in current locale */
  160. int gettext_width(const char *s)
  161. {
  162. static int is_utf8 = -1;
  163. if (is_utf8 == -1)
  164. is_utf8 = is_utf8_locale();
  165. return is_utf8 ? utf8_strwidth(s) : strlen(s);
  166. }
  167. #endif
  168. int is_utf8_locale(void)
  169. {
  170. #ifdef NO_GETTEXT
  171. if (!charset) {
  172. const char *env = getenv("LC_ALL");
  173. if (!env || !*env)
  174. env = getenv("LC_CTYPE");
  175. if (!env || !*env)
  176. env = getenv("LANG");
  177. if (!env)
  178. env = "";
  179. if (strchr(env, '.'))
  180. env = strchr(env, '.') + 1;
  181. charset = xstrdup(env);
  182. }
  183. #endif
  184. return is_encoding_utf8(charset);
  185. }