Merge pull request #7 from saloniamatteo/master

Locale: implement /etc/locale.conf fallbacks
This commit is contained in:
Linus Dierheimer
2021-03-21 09:58:49 +01:00
committed by GitHub
+51 -25
View File
@@ -2,34 +2,60 @@
void ffPrintLocale(FFinstance* instance)
{
if(ffPrintCachedValue(instance, "Locale"))
return;
if(ffPrintCachedValue(instance, "Locale"))
return;
char localeCode[256];
ffParsePropFile("/etc/locale.conf", "LANG=%[^\n]", localeCode);
if(localeCode[0] == '\0' && ffStrbufIsEmpty(&instance->config.localeFormat))
{
ffPrintError(instance, "Locale", "\"LANG=%[^\\n]\" not found in \"/etc/locale.conf\"");
return;
}
char localeCode[256];
ffPrintLogoAndKey(instance, "Locale");
/* Try to open /etc/locale.conf in read-only mode */
FILE *fp;
fp = fopen("/etc/locale.conf", "r");
FFstrbuf locale;
ffStrbufInit(&locale);
/* File does not exist */
if (fp == NULL) {
/* 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
ffPrintError(instance, "Locale", "Locale not found!");
/* /etc/locale.conf exists */
} else {
ffParsePropFile("/etc/locale.conf", "LANG=%[^\n]", localeCode);
}
if(ffStrbufIsEmpty(&instance->config.localeFormat))
{
ffStrbufSetS(&locale, localeCode);
}
else
{
ffParseFormatString(&locale, &instance->config.localeFormat, 1,
(FFformatarg){FF_FORMAT_ARG_TYPE_STRING, localeCode}
);
}
/* Free pointer to file */
fclose(fp);
ffPrintLogoAndKey(instance, "Locale");
FFstrbuf locale;
ffStrbufInit(&locale);
if(ffStrbufIsEmpty(&instance->config.localeFormat))
{
ffStrbufSetS(&locale, localeCode);
}
else
{
ffParseFormatString(&locale, &instance->config.localeFormat, 1,
(FFformatarg){FF_FORMAT_ARG_TYPE_STRING, localeCode}
);
}
ffPrintAndSaveCachedValue(instance, "Locale", locale.chars);
ffStrbufDestroy(&locale);
}
ffPrintAndSaveCachedValue(instance, "Locale", locale.chars);
ffStrbufDestroy(&locale);
}