From bd4f4a136fc218f0854f5ee5219149ce5b0acdb3 Mon Sep 17 00:00:00 2001 From: Lukas Fleischer Date: Tue, 27 Aug 2013 19:32:02 +0200 Subject: Display translated help pages Use an algorithm similar to gettext's locale resolution to find an appropriate translation to display. Fall back to the English version. Signed-off-by: Lukas Fleischer --- src/help.c | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 56 insertions(+), 3 deletions(-) (limited to 'src/help.c') diff --git a/src/help.c b/src/help.c index bf755fd..b2a7e72 100644 --- a/src/help.c +++ b/src/help.c @@ -36,21 +36,74 @@ #include "calcurse.h" +static int find_basedir(const char *locale_info[], unsigned n, char *basedir) +{ + int i; + char *locale = NULL; + int ret = 0; + + for (i = 0; i < n; i++) { + if (!locale_info[i]) + continue; + locale = strdup(locale_info[i]); + + snprintf(basedir, BUFSIZ, DOCDIR "/%s", locale); + if (io_dir_exists(basedir)) { + ret = 1; + goto cleanup; + } + + strtok(locale, ".@"); + + snprintf(basedir, BUFSIZ, DOCDIR "/%s", locale); + if (io_dir_exists(basedir)) { + ret = 1; + goto cleanup; + } + + strtok(locale, "_"); + + snprintf(basedir, BUFSIZ, DOCDIR "/%s", locale); + if (io_dir_exists(basedir)) { + ret = 1; + goto cleanup; + } + + free(locale); + locale = NULL; + } + +cleanup: + if (locale) + free(locale); + return ret; +} + int display_help(const char *topic) { + const char *locale_info[] = { + getenv("LANGUAGE"), + getenv("LC_ALL"), + getenv("LC_MESSAGE"), + getenv("LANG") + }; + char basedir[BUFSIZ]; char path[BUFSIZ]; if (!topic) topic = "intro"; - snprintf(path, BUFSIZ, DOCDIR "/%s.txt", topic); + if (!find_basedir(locale_info, ARRAY_SIZE(locale_info), basedir)) + snprintf(basedir, BUFSIZ, DOCDIR); + + snprintf(path, BUFSIZ, "%s/%s.txt", basedir, topic); if (!io_file_exists(path) && keys_str2int(topic) > 0 && keys_get_action(keys_str2int(topic)) > 0) { int ch = keys_str2int(topic); enum key action = keys_get_action(ch); topic = keys_get_label(action); - snprintf(path, BUFSIZ, DOCDIR "/%s.txt", topic); + snprintf(path, BUFSIZ, "%s/%s.txt", basedir, topic); } if (!io_file_exists(path)) { @@ -132,7 +185,7 @@ int display_help(const char *topic) topic = "priority"; else if (!strcmp(topic, "lower-priority")) topic = "priority"; - snprintf(path, BUFSIZ, DOCDIR "/%s.txt", topic); + snprintf(path, BUFSIZ, "%s/%s.txt", basedir, topic); } if (io_file_exists(path)) { -- cgit v1.2.3