Files
fastfetch/src/modules/locale.c
T

58 lines
1.5 KiB
C
Raw Normal View History

2021-02-18 22:25:36 +01:00
#include "fastfetch.h"
2021-02-27 18:30:05 +01:00
void ffPrintLocale(FFinstance* instance)
2021-02-18 22:25:36 +01:00
{
2021-04-03 11:39:59 +02:00
if(ffPrintCachedValue(instance, &instance->config.localeKey, "Locale"))
return;
2021-02-22 16:52:51 +01:00
char localeCode[256];
2021-03-20 10:39:05 +01:00
/* Try to open /etc/locale.conf in read-only mode */
FILE *fp;
fp = fopen("/etc/locale.conf", "r");
2021-03-20 10:39:05 +01:00
/* File does not exist */
if (fp == NULL) {
2021-03-21 11:18:06 +01:00
/* Check if LANG exists */
if (getenv("LANG") != NULL)
strcpy(localeCode, getenv("LANG"));
/* Check if LC_ALL exists */
else if (getenv("LC_ALL") != NULL)
strcpy(localeCode, getenv("LC_ALL"));
/* Check if LC_CTYPE exists */
else if (getenv("LC_CTYPE") != NULL)
strcpy(localeCode, getenv("LC_CTYPE"));
/* Check if LC_CTYPE exists */
else if (getenv("LC_CTYPE") != NULL)
strcpy(localeCode, getenv("LC_CTYPE"));
/* Check if LC_MESSAGES exists */
else if (getenv("LC_MESSAGES") != NULL)
strcpy(localeCode, getenv("LC_MESSAGES"));
/* User has broken system */
else
2021-04-03 11:39:59 +02:00
ffPrintError(instance, &instance->config.localeKey, "Locale", "Locale not found!");
2021-03-21 11:18:06 +01:00
/* /etc/locale.conf exists */
} else {
2021-03-21 11:18:06 +01:00
ffParsePropFile("/etc/locale.conf", "LANG=%[^\n]", localeCode);
2021-03-20 10:39:05 +01:00
2021-03-21 11:18:06 +01:00
/* Free pointer to file */
fclose(fp);
}
2021-02-18 22:25:36 +01:00
FF_STRBUF_CREATE(locale);
2021-03-19 20:57:07 +01:00
2021-03-23 19:48:52 +01:00
if(instance->config.localeFormat.length == 0)
{
ffStrbufSetS(&locale, localeCode);
}
else
{
ffParseFormatString(&locale, &instance->config.localeFormat, 1,
(FFformatarg){FF_FORMAT_ARG_TYPE_STRING, localeCode}
);
}
2021-03-19 20:57:07 +01:00
2021-04-03 11:39:59 +02:00
ffPrintAndSaveCachedValue(instance, &instance->config.localeKey, "Locale", &locale);
ffStrbufDestroy(&locale);
2021-03-20 10:39:05 +01:00
}