From 57dae8f42c0f08a1cf26ea71adb9a3fc5cef4cc1 Mon Sep 17 00:00:00 2001 From: rcombs Date: Wed, 26 May 2021 17:55:08 -0500 Subject: osdep: add mp_get_user_langs --- osdep/language-posix.c | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 osdep/language-posix.c (limited to 'osdep/language-posix.c') diff --git a/osdep/language-posix.c b/osdep/language-posix.c new file mode 100644 index 0000000000..f5d1a3a72c --- /dev/null +++ b/osdep/language-posix.c @@ -0,0 +1,61 @@ +/* + * User language lookup for generic POSIX platforms + * + * This file is part of mpv. + * + * mpv is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * mpv is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with mpv. If not, see . + */ + +#include "misc/language.h" +#include "mpv_talloc.h" + +#include + +char **mp_get_user_langs(void) +{ + static const char *const list[] = { + "LC_ALL", + "LC_MESSAGES", + "LANG", + NULL + }; + + size_t nb = 0; + char **ret = NULL; + + // Prefer anything we get from LANGUAGE first + for (const char *langList = getenv("LANGUAGE"); langList && *langList;) { + size_t len = strcspn(langList, ":"); + MP_TARRAY_GROW(NULL, ret, nb); + ret[nb++] = talloc_strndup(ret, langList, len); + langList += len; + while (*langList == ':') + langList++; + } + + // Then, the language components of other relevant locale env vars + for (int i = 0; list[i]; i++) { + const char *envval = getenv(list[i]); + if (envval && *envval) { + size_t len = strcspn(envval, ".@"); + MP_TARRAY_GROW(NULL, ret, nb); + ret[nb++] = talloc_strndup(ret, envval, len); + } + } + + // Null-terminate the list + MP_TARRAY_APPEND(NULL, ret, nb, NULL); + + return ret; +} -- cgit v1.2.3