From 38c382431557106fc2bb7c4cb630ecb0f8059364 Mon Sep 17 00:00:00 2001 From: axtloss Date: Thu, 31 Oct 2024 21:43:29 +0100 Subject: [PATCH] fix: [close #22] Properly detect system locale --- defaults.go | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/defaults.go b/defaults.go index 6a77f7b..ced33f8 100644 --- a/defaults.go +++ b/defaults.go @@ -2,6 +2,7 @@ package orchid import ( "os" + "strings" l "github.com/vanilla-os/orchid/log" ) @@ -29,10 +30,20 @@ func InitLog(prefix string, flags int) { // from the LANG environment variable, or "en" // if unset. func Locale() string { - lang := os.Getenv("LANG") - if lang == "" { - lang = "en" + var locale string + for _, env := range []string{"LC_ALL", "LC_MESSAGES", "LANG"} { + locale = os.Getenv(env) + if strings.TrimSpace(locale) != "" { + break + } + } + if strings.TrimSpace(locale) == "C" || strings.TrimSpace(locale) == "POSIX" { + return strings.TrimSpace(locale) + } + + langs := strings.Split(os.Getenv("LANGUAGE"), ":") + if len(langs) > 0 { + return strings.Split(langs[0], "_")[0] } - locale := lang[:2] return locale }