summaryrefslogtreecommitdiffstats
path: root/osdep/language-posix.c
diff options
context:
space:
mode:
authorrcombs <rcombs@rcombs.me>2021-05-26 17:55:08 -0500
committersfan5 <sfan5@live.de>2023-06-25 11:01:58 +0200
commit57dae8f42c0f08a1cf26ea71adb9a3fc5cef4cc1 (patch)
tree9c93f5d946c4e24b1376cd6459f9ea227dfbc79a /osdep/language-posix.c
parent0463096b3c5a717a7c181801c8df797b046989fc (diff)
downloadmpv-57dae8f42c0f08a1cf26ea71adb9a3fc5cef4cc1.tar.bz2
mpv-57dae8f42c0f08a1cf26ea71adb9a3fc5cef4cc1.tar.xz
osdep: add mp_get_user_langs
Diffstat (limited to 'osdep/language-posix.c')
-rw-r--r--osdep/language-posix.c61
1 files changed, 61 insertions, 0 deletions
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 <http://www.gnu.org/licenses/>.
+ */
+
+#include "misc/language.h"
+#include "mpv_talloc.h"
+
+#include <stddef.h>
+
+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;
+}