Skip to content

Commit f9fa482

Browse files
committed
Locale: try detecting with locale command
in case $LC_* envs are not set ( macOS )
1 parent 65ed48a commit f9fa482

File tree

3 files changed

+38
-4
lines changed

3 files changed

+38
-4
lines changed

src/common/processing.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,16 @@
66
#include <unistd.h>
77
#include <sys/wait.h>
88

9-
void ffProcessAppendStdOut(FFstrbuf* buffer, char* const argv[])
9+
const char* ffProcessAppendStdOut(FFstrbuf* buffer, char* const argv[])
1010
{
1111
int pipes[2];
1212

1313
if(pipe(pipes) == -1)
14-
return;
14+
return "pipe() failed";
1515

1616
pid_t childPid = fork();
1717
if(childPid == -1)
18-
return;
18+
return "fork() failed";
1919

2020
//Child
2121
if(childPid == 0)
@@ -33,4 +33,6 @@ void ffProcessAppendStdOut(FFstrbuf* buffer, char* const argv[])
3333
waitpid(childPid, NULL, 0);
3434
ffAppendFDBuffer(pipes[0], buffer);
3535
close(pipes[0]);
36+
37+
return NULL;
3638
}

src/common/processing.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@
55

66
#include "util/FFstrbuf.h"
77

8-
void ffProcessAppendStdOut(FFstrbuf* buffer, char* const argv[]);
8+
const char* ffProcessAppendStdOut(FFstrbuf* buffer, char* const argv[]);
99

1010
#endif

src/modules/locale.c

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
#include "common/properties.h"
33
#include "common/printing.h"
44
#include "common/caching.h"
5+
#include "common/processing.h"
6+
#include "common/parsing.h"
57

68
#include <stdlib.h>
79

@@ -21,6 +23,31 @@ static void getLocaleFromEnv(FFstrbuf* locale)
2123
ffStrbufAppendS(locale, getenv("LC_MESSAGES"));
2224
}
2325

26+
static void getLocaleFromCmd(FFstrbuf* locale)
27+
{
28+
FFstrbuf buffer;
29+
ffStrbufInitA(&buffer, 0);
30+
char* args[] = { "locale", NULL };
31+
if(ffProcessAppendStdOut(&buffer, args) == NULL)
32+
{
33+
ffParsePropLines(buffer.chars, "LANG=\"", locale);
34+
ffStrbufTrimRight(locale, '"');
35+
36+
if(locale->length == 0)
37+
{
38+
ffParsePropLines(buffer.chars, "LC_ALL=\"", locale);
39+
ffStrbufTrimRight(locale, '"');
40+
41+
if(locale->length == 0)
42+
{
43+
ffParsePropLines(buffer.chars, "LC_MESSAGES=\"", locale);
44+
ffStrbufTrimRight(locale, '"');
45+
}
46+
}
47+
}
48+
ffStrbufDestroy(&buffer);
49+
}
50+
2451
void ffPrintLocale(FFinstance* instance)
2552
{
2653
if(ffPrintFromCache(instance, FF_LOCALE_MODULE_NAME, &instance->config.locale, FF_LOCALE_NUM_FORMAT_ARGS))
@@ -44,6 +71,11 @@ void ffPrintLocale(FFinstance* instance)
4471
getLocaleFromEnv(&locale);
4572
}
4673

74+
if(locale.length == 0)
75+
{
76+
getLocaleFromCmd(&locale);
77+
}
78+
4779
if(locale.length == 0)
4880
{
4981
ffPrintError(instance, FF_LOCALE_MODULE_NAME, 0, &instance->config.locale, "No locale found");

0 commit comments

Comments
 (0)