t/lib-git-svn.sh: check GIT_TEST_SVN_HTTPD when running SVN HTTP tests
[gitweb.git] / gettext.c
index a268a2c52c0ed444b073d0411ca0dcc3f210ef51..35d2c1218db2e27a6ac07fbf3c1244987e3e3021 100644 (file)
--- a/gettext.c
+++ b/gettext.c
@@ -2,15 +2,35 @@
  * Copyright (c) 2010 Ævar Arnfjörð Bjarmason
  */
 
-#include "git-compat-util.h"
+#include "cache.h"
+#include "exec-cmd.h"
 #include "gettext.h"
 #include "strbuf.h"
 #include "utf8.h"
+#include "config.h"
 
 #ifndef NO_GETTEXT
 #      include <locale.h>
 #      include <libintl.h>
-#      ifdef HAVE_LIBCHARSET_H
+#      ifdef GIT_WINDOWS_NATIVE
+
+static const char *locale_charset(void)
+{
+       const char *env = getenv("LC_ALL"), *dot;
+
+       if (!env || !*env)
+               env = getenv("LC_CTYPE");
+       if (!env || !*env)
+               env = getenv("LANG");
+
+       if (!env)
+               return "UTF-8";
+
+       dot = strchr(env, '.');
+       return !dot ? env : dot + 1;
+}
+
+#      elif defined HAVE_LIBCHARSET_H
 #              include <libcharset.h>
 #      else
 #              include <langinfo.h>
@@ -18,6 +38,8 @@
 #      endif
 #endif
 
+static const char *charset;
+
 /*
  * Guess the user's preferred languages from the value in LANGUAGE environment
  * variable and LC_MESSAGES locale category if NO_GETTEXT is not defined.
@@ -43,15 +65,13 @@ const char *get_preferred_languages(void)
        return NULL;
 }
 
-#ifdef GETTEXT_POISON
 int use_gettext_poison(void)
 {
        static int poison_requested = -1;
        if (poison_requested == -1)
-               poison_requested = getenv("GIT_GETTEXT_POISON") ? 1 : 0;
+               poison_requested = git_env_bool("GIT_TEST_GETTEXT_POISON", 0);
        return poison_requested;
 }
-#endif
 
 #ifndef NO_GETTEXT
 static int test_vsnprintf(const char *fmt, ...)
@@ -65,7 +85,6 @@ static int test_vsnprintf(const char *fmt, ...)
        return ret;
 }
 
-static const char *charset;
 static void init_gettext_charset(const char *domain)
 {
        /*
@@ -156,15 +175,26 @@ static void init_gettext_charset(const char *domain)
 
 void git_setup_gettext(void)
 {
-       const char *podir = getenv("GIT_TEXTDOMAINDIR");
+       const char *podir = getenv(GIT_TEXT_DOMAIN_DIR_ENVIRONMENT);
+       char *p = NULL;
 
        if (!podir)
-               podir = GIT_LOCALE_PATH;
+               podir = p = system_path(GIT_LOCALE_PATH);
+
+       use_gettext_poison(); /* getenv() reentrancy paranoia */
+
+       if (!is_directory(podir)) {
+               free(p);
+               return;
+       }
+
        bindtextdomain("git", podir);
        setlocale(LC_MESSAGES, "");
        setlocale(LC_TIME, "");
        init_gettext_charset("git");
        textdomain("git");
+
+       free(p);
 }
 
 /* return the number of columns of string 's' in current locale */
@@ -172,8 +202,27 @@ int gettext_width(const char *s)
 {
        static int is_utf8 = -1;
        if (is_utf8 == -1)
-               is_utf8 = !strcmp(charset, "UTF-8");
+               is_utf8 = is_utf8_locale();
 
        return is_utf8 ? utf8_strwidth(s) : strlen(s);
 }
 #endif
+
+int is_utf8_locale(void)
+{
+#ifdef NO_GETTEXT
+       if (!charset) {
+               const char *env = getenv("LC_ALL");
+               if (!env || !*env)
+                       env = getenv("LC_CTYPE");
+               if (!env || !*env)
+                       env = getenv("LANG");
+               if (!env)
+                       env = "";
+               if (strchr(env, '.'))
+                       env = strchr(env, '.') + 1;
+               charset = xstrdup(env);
+       }
+#endif
+       return is_encoding_utf8(charset);
+}