Locale: reports API failure messages

This commit is contained in:
李通洲
2026-03-16 19:53:04 +08:00
parent 42e01e7b8f
commit edc718d1d1
4 changed files with 26 additions and 13 deletions
+1 -1
View File
@@ -2,4 +2,4 @@
#include "fastfetch.h"
void ffDetectLocale(FFstrbuf* result);
const char* ffDetectLocale(FFstrbuf* result);
+7 -3
View File
@@ -2,15 +2,19 @@
#include <locale.h>
void ffDetectLocale(FFstrbuf* result)
const char* ffDetectLocale(FFstrbuf* result)
{
ffStrbufAppendS(result, getenv("LC_ALL"));
if(result->length > 0)
return;
return NULL;
ffStrbufAppendS(result, getenv("LANG"));
if(result->length > 0)
return;
return NULL;
ffStrbufAppendS(result, setlocale(LC_TIME, NULL));
if(result->length > 0)
return NULL;
return "Failed to detect locale";
}
+7 -4
View File
@@ -2,12 +2,15 @@
#include "common/windows/unicode.h"
#include <windows.h>
#include <locale.h>
void ffDetectLocale(FFstrbuf* result)
const char* 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);
if (size <= 1) // including '\0'
return "GetUserDefaultLocaleName() failed";
ffStrbufSetNWS(result, (uint32_t)size - 1, name);
return NULL;
}
+11 -5
View File
@@ -1,6 +1,5 @@
#include "common/printing.h"
#include "common/jsonconfig.h"
#include "common/stringUtils.h"
#include "detection/locale/locale.h"
#include "modules/locale/locale.h"
@@ -8,10 +7,10 @@ bool ffPrintLocale(FFLocaleOptions* options)
{
FF_STRBUF_AUTO_DESTROY locale = ffStrbufCreate();
ffDetectLocale(&locale);
if(locale.length == 0)
const char* error = ffDetectLocale(&locale);
if(error)
{
ffPrintError(FF_LOCALE_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "No locale found");
ffPrintError(FF_LOCALE_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "%s", error);
return false;
}
@@ -52,7 +51,14 @@ bool ffGenerateLocaleJsonResult(FF_MAYBE_UNUSED FFLocaleOptions* options, yyjson
{
FF_STRBUF_AUTO_DESTROY locale = ffStrbufCreate();
ffDetectLocale(&locale);
const char* error = ffDetectLocale(&locale);
if(error)
{
yyjson_mut_obj_add_str(doc, module, "error", error);
return false;
}
if(locale.length == 0)
{
yyjson_mut_obj_add_str(doc, module, "error", "No locale found");