Fix accidental switch/teleporter behavior changes
[neverball] / share / lang.c
index bd03bd5..999c239 100644 (file)
 #include <locale.h>
 #include <stdlib.h>
 #include <stdio.h>
+#include <errno.h>
 
 #include "lang.h"
+#include "common.h"
+#include "base_config.h"
+#include "fs.h"
 
 /*---------------------------------------------------------------------------*/
 
 
 /*---------------------------------------------------------------------------*/
 
-void lang_init(const char *domain, const char *default_dir)
+void lang_init(const char *domain)
 {
 #if ENABLE_NLS
-    char *dir = getenv("NEVERBALL_LOCALE");
+    char *dir = strdup(getenv("NEVERBALL_LOCALE"));
 
-    setlocale(LC_ALL, "");
+    if (!dir)
+    {
+        if (path_is_abs(CONFIG_LOCALE))
+            dir = strdup(CONFIG_LOCALE);
+        else
+            dir = concat_string(fs_base_dir(), "/", CONFIG_LOCALE, NULL);
+    }
+
+    errno = 0;
 
-    bindtextdomain(domain, dir ? dir : default_dir);
+    if (!setlocale(LC_ALL, ""))
+    {
+        fprintf(stderr, "Failed to set LC_ALL to native locale: %s\n",
+                errno ? strerror(errno) : "Unknown error");
+    }
+
+    /* The C locale is guaranteed (sort of) to be available. */
+
+    setlocale(LC_NUMERIC, "C");
+
+    bindtextdomain(domain, dir);
     bind_textdomain_codeset(domain, DEFAULT_CODESET);
     textdomain(domain);
+
+    free(dir);
 #else
     return;
 #endif