From 7d197a380ce19e09c774dabe5448a9706398437b Mon Sep 17 00:00:00 2001
From: Kubat <mael.martin31@gmail.com>
Date: Wed, 16 Feb 2022 21:54:24 +0100
Subject: [PATCH] MISC: A better way to check the locale

Now it is up to the user to select a correct UTF-8 locale. In lektor we
only check if it is an UTF-8 locale, we don't set it (because
en_US.UTF-8 or fr_FR.UTF-8 is not always available)
---
 inc/lektor/internal/os.h | 1 +
 src/main/lkt.c           | 8 +++++++-
 src/main/luka.c          | 8 +++++++-
 src/main/server.c        | 5 +++--
 4 files changed, 18 insertions(+), 4 deletions(-)

diff --git a/inc/lektor/internal/os.h b/inc/lektor/internal/os.h
index e2742e53..c2f6d62d 100644
--- a/inc/lektor/internal/os.h
+++ b/inc/lektor/internal/os.h
@@ -6,6 +6,7 @@ extern "C" {
 
 #include <inttypes.h>
 #include <locale.h>
+#include <langinfo.h>
 #include <memory.h>
 #include <regex.h>
 #include <stddef.h>
diff --git a/src/main/lkt.c b/src/main/lkt.c
index e62a532e..b673e2c5 100644
--- a/src/main/lkt.c
+++ b/src/main/lkt.c
@@ -1381,7 +1381,13 @@ main(int argc, const char **argv)
     lkt_install_segv_handler();
     lkt_set_log_level(LOG_LEVEL_INFO);
     cmd_set_executable_name("lkt");
-    assert(NULL != setlocale(LC_ALL, "en_US.UTF-8")); /* BECAUSE! */
+
+    /* Enforce some locals */
+    RETURN_UNLESS(setlocale(LC_ALL, ""), "Failed to set LC_ALL to UTF-8", 1);
+    RETURN_UNLESS(setlocale(LC_CTYPE, ""), "Failed to set LC_CTYPE", 1);
+    RETURN_UNLESS(setlocale(LC_NUMERIC, ""), "Failed to set LC_NUMERIC for mpv", 1);
+    RETURN_UNLESS(STR_MATCH(nl_langinfo(CODESET), "UTF-8"), "Your locale is not set to an UTF-8 one. " "Consider using en_US.UTF-8 or fr_FR.UTF-8!", 1);
+
     if (signal(SIGPIPE, sigpipe__))
         LOG_ERROR("SYS", "Failed to install handler for SIGPIPE signal (you may be using php...)");
 
diff --git a/src/main/luka.c b/src/main/luka.c
index 20581f59..43f20460 100644
--- a/src/main/luka.c
+++ b/src/main/luka.c
@@ -455,7 +455,13 @@ main(const int argc, const char **argv)
     lkt_set_log_level(LOG_LEVEL_ERROR);
     lkt_segv_quiet();
     lkt_install_segv_handler();
-    assert(NULL != setlocale(LC_ALL, "en_US.UTF-8")); /* BECAUSE! */
+
+    /* Enforce some locals */
+    RETURN_UNLESS(setlocale(LC_ALL, ""), "Failed to set LC_ALL to UTF-8", 1);
+    RETURN_UNLESS(setlocale(LC_CTYPE, ""), "Failed to set LC_CTYPE", 1);
+    RETURN_UNLESS(setlocale(LC_NUMERIC, ""), "Failed to set LC_NUMERIC for mpv", 1);
+    RETURN_UNLESS(STR_MATCH(nl_langinfo(CODESET), "UTF-8"), "Your locale is not set to an UTF-8 one. " "Consider using en_US.UTF-8 or fr_FR.UTF-8!", 1);
+
     if (signal(SIGPIPE, __sigpipe))
         LOG_ERROR("SYS", "Failed to install handler for SIGPIPE signal (you may be using php...)");
 
diff --git a/src/main/server.c b/src/main/server.c
index 480473a7..f795ff02 100644
--- a/src/main/server.c
+++ b/src/main/server.c
@@ -45,9 +45,10 @@ main(int argc, char *argv[])
     REG_END()
 
     /* Enforce some locals */
-    RETURN_UNLESS(setlocale(LC_ALL, "en_US.UTF-8"), "Failed to set LC_ALL to UTF-8", 1);
+    RETURN_UNLESS(setlocale(LC_ALL, ""), "Failed to set LC_ALL to UTF-8", 1);
     RETURN_UNLESS(setlocale(LC_CTYPE, ""), "Failed to set LC_CTYPE", 1);
-    RETURN_UNLESS(setlocale(LC_NUMERIC, "C"), "Failed to set LC_NUMERIC for mpv", 1);
+    RETURN_UNLESS(setlocale(LC_NUMERIC, ""), "Failed to set LC_NUMERIC for mpv", 1);
+    RETURN_UNLESS(STR_MATCH(nl_langinfo(CODESET), "UTF-8"), "Your locale is not set to an UTF-8 one. " "Consider using en_US.UTF-8 or fr_FR.UTF-8!", 1);
 
     int autoclear;
     int opt;
-- 
GitLab