mirror of
https://github.com/fastfetch-cli/fastfetch.git
synced 2025-02-20 11:43:27 +08:00
Locale: refactor
This commit is contained in:
parent
d601cc823c
commit
a3cb6608ce
@ -239,6 +239,7 @@ set(LIBFASTFETCH_SRC
|
||||
src/detection/font/font.c
|
||||
src/detection/gpu/gpu.c
|
||||
src/detection/host/host.c
|
||||
src/detection/locale/locale.c
|
||||
src/detection/media/media.c
|
||||
src/detection/memory/memory.c
|
||||
src/detection/os/os.c
|
||||
|
60
src/detection/locale/locale.c
Normal file
60
src/detection/locale/locale.c
Normal file
@ -0,0 +1,60 @@
|
||||
#include "detection/locale/locale.h"
|
||||
|
||||
#include "common/properties.h"
|
||||
#include "common/parsing.h"
|
||||
#include <stdlib.h>
|
||||
#include <locale.h>
|
||||
|
||||
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)
|
||||
{
|
||||
ffStrbufAppendS(locale, setlocale(LC_ALL, NULL));
|
||||
|
||||
#ifdef LC_MESSAGES
|
||||
if(locale->length > 0)
|
||||
return;
|
||||
|
||||
ffStrbufAppendS(locale, setlocale(LC_MESSAGES, NULL));
|
||||
#endif
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
#endif
|
||||
|
||||
#ifndef _WIN32
|
||||
|
||||
getLocaleFromEnv(result);
|
||||
if(result->length > 0)
|
||||
return;
|
||||
|
||||
#endif
|
||||
|
||||
getLocaleFromStdFn(result);
|
||||
}
|
10
src/detection/locale/locale.h
Normal file
10
src/detection/locale/locale.h
Normal file
@ -0,0 +1,10 @@
|
||||
#pragma once
|
||||
|
||||
#ifndef FF_INCLUDED_detection_locale_locale
|
||||
#define FF_INCLUDED_detection_locale_locale
|
||||
|
||||
#include "fastfetch.h"
|
||||
|
||||
void ffDetectLocale(FFstrbuf* result);
|
||||
|
||||
#endif
|
@ -1,68 +1,20 @@
|
||||
#include "fastfetch.h"
|
||||
#include "common/properties.h"
|
||||
#include "common/printing.h"
|
||||
#include "common/caching.h"
|
||||
#include "common/parsing.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <locale.h>
|
||||
#include "common/printing.h"
|
||||
#include "detection/locale/locale.h"
|
||||
|
||||
#define FF_LOCALE_MODULE_NAME "Locale"
|
||||
#define FF_LOCALE_NUM_FORMAT_ARGS 1
|
||||
|
||||
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 getLocaleFromCmd(FFstrbuf* locale)
|
||||
{
|
||||
ffStrbufAppendS(locale, setlocale(LC_ALL, NULL));
|
||||
|
||||
#ifdef LC_MESSAGES
|
||||
if(locale->length > 0)
|
||||
return;
|
||||
|
||||
ffStrbufAppendS(locale, setlocale(LC_MESSAGES, NULL));
|
||||
#endif
|
||||
}
|
||||
|
||||
void ffPrintLocale(FFinstance* instance)
|
||||
{
|
||||
if(ffPrintFromCache(instance, FF_LOCALE_MODULE_NAME, &instance->config.locale, FF_LOCALE_NUM_FORMAT_ARGS))
|
||||
if(ffPrintFromCache(instance, FF_LOCALE_MODULE_NAME, &instance->config.locale, FF_LOCALE_NUM_FORMAT_ARGS))
|
||||
return;
|
||||
|
||||
FFstrbuf locale;
|
||||
FFstrbuf locale;
|
||||
ffStrbufInit(&locale);
|
||||
|
||||
//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 =", &locale);
|
||||
|
||||
if(locale.length == 0)
|
||||
{
|
||||
ffParsePropFile(FASTFETCH_TARGET_DIR_ETC"/locale.conf", "LANG =", &locale);
|
||||
}
|
||||
|
||||
if(locale.length == 0)
|
||||
{
|
||||
getLocaleFromEnv(&locale);
|
||||
}
|
||||
|
||||
if(locale.length == 0)
|
||||
{
|
||||
getLocaleFromCmd(&locale);
|
||||
}
|
||||
|
||||
ffDetectLocale(&locale);
|
||||
if(locale.length == 0)
|
||||
{
|
||||
ffPrintError(instance, FF_LOCALE_MODULE_NAME, 0, &instance->config.locale, "No locale found");
|
||||
@ -73,5 +25,5 @@ void ffPrintLocale(FFinstance* instance)
|
||||
{FF_FORMAT_ARG_TYPE_STRBUF, &locale}
|
||||
});
|
||||
|
||||
ffStrbufDestroy(&locale);
|
||||
ffStrbufDestroy(&locale);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user