Coverage Report

Created: 2026-03-27 06:40

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/mpv/osdep/language-posix.c
Line
Count
Source
1
/*
2
 * User language lookup for generic POSIX platforms
3
 *
4
 * This file is part of mpv.
5
 *
6
 * mpv is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU Lesser General Public
8
 * License as published by the Free Software Foundation; either
9
 * version 2.1 of the License, or (at your option) any later version.
10
 *
11
 * mpv is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU Lesser General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU Lesser General Public
17
 * License along with mpv.  If not, see <http://www.gnu.org/licenses/>.
18
 */
19
20
#include "misc/language.h"
21
#include "mpv_talloc.h"
22
23
#include <stddef.h>
24
25
char **mp_get_user_langs(void)
26
69.3k
{
27
69.3k
    static const char *const list[] = {
28
69.3k
        "LC_ALL",
29
69.3k
        "LC_MESSAGES",
30
69.3k
        "LANG",
31
69.3k
        NULL
32
69.3k
    };
33
34
69.3k
    size_t nb = 0;
35
69.3k
    char **ret = NULL;
36
69.3k
    bool has_c = false;
37
38
    // Prefer anything we get from LANGUAGE first
39
69.3k
    for (const char *langList = getenv("LANGUAGE"); langList && *langList;) {
40
0
        size_t len = strcspn(langList, ":");
41
0
        MP_TARRAY_GROW(NULL, ret, nb);
42
0
        char *lang = talloc_strndup(ret, langList, len);
43
0
        for (int i = 0; i < len; i++) {
44
0
            if (lang[i] == '_')
45
0
                lang[i] = '-';
46
0
        }
47
0
        ret[nb++] = lang;
48
0
        langList += len;
49
0
        while (*langList == ':')
50
0
            langList++;
51
0
    }
52
53
    // Then, the language components of other relevant locale env vars
54
277k
    for (int i = 0; list[i]; i++) {
55
208k
        const char *envval = getenv(list[i]);
56
208k
        if (envval && *envval) {
57
0
            size_t len = strcspn(envval, ".@");
58
0
            if (!strncmp("C", envval, len)) {
59
0
                has_c = true;
60
0
                continue;
61
0
            }
62
63
0
            MP_TARRAY_GROW(NULL, ret, nb);
64
0
            char *lang = talloc_strndup(ret, envval, len);
65
0
            for (int j = 0; j < len; j++) {
66
0
                if (lang[j] == '_')
67
0
                    lang[j] = '-';
68
0
            }
69
70
0
            ret[nb++] = lang;
71
0
        }
72
208k
    }
73
74
69.3k
    if (has_c && !nb) {
75
0
        MP_TARRAY_GROW(NULL, ret, nb);
76
0
        ret[nb++] = talloc_strdup(ret, "en");
77
0
    }
78
79
    // Null-terminate the list
80
69.3k
    MP_TARRAY_APPEND(NULL, ret, nb, NULL);
81
82
69.3k
    return ret;
83
69.3k
}