Locale: improve detection

Linux: always use `setlocale`, fix #688
Windows: don't append `UTF-8`
This commit is contained in:
李通洲
2024-01-19 21:06:42 +08:00
parent 8e7d465333
commit 8404ae034f
2 changed files with 5 additions and 56 deletions
+5 -51
View File
@@ -1,61 +1,15 @@
#include "detection/locale/locale.h"
#include "common/properties.h"
#include "common/parsing.h"
#include <stdlib.h>
#include <locale.h>
FF_MAYBE_UNUSED
static void getLocaleFromEnv(FFstrbuf* locale)
{
ffStrbufAppendS(locale, getenv("LANG"));
if(locale->length > 0)
return;
ffStrbufAppendS(locale, getenv("LC_ALL"));
if(locale->length > 0)
return;
ffStrbufAppendS(locale, getenv("LC_MESSAGES"));
}
static void getLocaleFromStdFn(FFstrbuf* locale)
{
#ifdef LC_MESSAGES
ffStrbufAppendS(locale, setlocale(LC_MESSAGES, NULL));
if(locale->length > 0)
return;
#endif
ffStrbufAppendS(locale, setlocale(LC_ALL, NULL));
}
void ffDetectLocale(FFstrbuf* result)
{
#if !(defined(__APPLE__) || defined(_WIN32))
//Ubuntu (and deriviates) use a non standard locale file.
//Parse it first, because on distributions where it exists, it takes precedence.
//Otherwise use the standard etc/locale.conf file.
ffParsePropFile(FASTFETCH_TARGET_DIR_ETC"/default/locale", "LANG =", result);
if(result->length > 0)
return;
ffParsePropFile(FASTFETCH_TARGET_DIR_ETC"/locale.conf", "LANG =", result);
if(result->length > 0)
return;
#ifdef LC_MESSAGES
ffStrbufAppendS(result, setlocale(LC_MESSAGES, NULL));
if(result->length > 0)
return;
#endif
#ifndef _WIN32
getLocaleFromEnv(result);
if(result->length > 0)
return;
#endif
getLocaleFromStdFn(result);
ffStrbufAppendS(result, setlocale(LC_ALL, NULL));
}
-5
View File
@@ -9,10 +9,5 @@ void ffDetectLocale(FFstrbuf* result)
wchar_t name[LOCALE_NAME_MAX_LENGTH];
int size = GetUserDefaultLocaleName(name, LOCALE_NAME_MAX_LENGTH);
if (size > 1) // including '\0'
{
ffStrbufSetNWS(result, (uint32_t)size - 1, name);
ffStrbufAppendS(result, ".UTF-8"); // set explicitly in `ffInitInstance`
}
else
ffStrbufSetS(result, setlocale(LC_ALL, NULL));
}