diff options
author | Vincent Sanders <vince@kyllikki.org> | 2019-02-17 00:08:28 +0000 |
---|---|---|
committer | Vincent Sanders <vince@kyllikki.org> | 2019-02-17 00:11:27 +0000 |
commit | 54371c28f07e638694ad2148e8900e1bae71e3d4 (patch) | |
tree | 70e32c6b86f5d584d039b01edf4d9b5eb3a7d039 /frontends/framebuffer/findfile.c | |
parent | cfb6c461fc61c64cf39e4708922ad024ac5fce91 (diff) | |
download | netsurf-54371c28f07e638694ad2148e8900e1bae71e3d4.tar.gz netsurf-54371c28f07e638694ad2148e8900e1bae71e3d4.tar.bz2 |
make framebuffer use the language environment for the UI resources
Diffstat (limited to 'frontends/framebuffer/findfile.c')
-rw-r--r-- | frontends/framebuffer/findfile.c | 107 |
1 files changed, 103 insertions, 4 deletions
diff --git a/frontends/framebuffer/findfile.c b/frontends/framebuffer/findfile.c index 67312f452..44a07b383 100644 --- a/frontends/framebuffer/findfile.c +++ b/frontends/framebuffer/findfile.c @@ -17,29 +17,128 @@ */ #include <stdio.h> +#include <stdlib.h> +#include <string.h> #include "utils/filepath.h" +#include "utils/log.h" #include "framebuffer/findfile.h" char **respaths; /** resource search path vector */ -/** Create an array of valid paths to search for resources. +#define MAX_LANGV_SIZE 32 + +/** + * goes through the environment in appropriate order to find configured language + * + * \return language to use or "C" if nothing appropriate is set + */ +static const char *get_language_env(void) +{ + const char *envstr; + + envstr = getenv("LANGUAGE"); + if ((envstr != NULL) && (envstr[0] != 0)) { + return envstr; + } + + envstr = getenv("LC_ALL"); + if ((envstr != NULL) && (envstr[0] != 0)) { + return envstr; + } + + envstr = getenv("LC_MESSAGES"); + if ((envstr != NULL) && (envstr[0] != 0)) { + return envstr; + } + + envstr = getenv("LANG"); + if ((envstr != NULL) && (envstr[0] != 0)) { + return envstr; + } + + return "C"; +} + +/** + * build a string vector of language names + */ +static char **get_language_names(void) +{ + char **langv; /* output string vector of languages */ + int langc; /* count of languages in vector */ + const char *envlang; /* colon separated list of languages from environment */ + int lstart = 0; /* offset to start of current language */ + int lunder = 0; /* offset to underscore in current language */ + int lend = 0; /* offset to end of current language */ + char *nlang; + + langv = calloc(MAX_LANGV_SIZE + 2, sizeof(char *)); + if (langv == NULL) { + return NULL; + } + + envlang = get_language_env(); + + for (langc = 0; langc < MAX_LANGV_SIZE; langc++) { + /* work through envlang splitting on : */ + while ((envlang[lend] != 0) && + (envlang[lend] != ':') && + (envlang[lend] != '.')) { + if (envlang[lend] == '_') { + lunder = lend; + } + lend++; + } + /* place language in string vector */ + nlang = malloc(lend - lstart + 1); + memcpy(nlang, envlang + lstart, lend - lstart); + nlang[lend - lstart] = 0; + langv[langc] = nlang; + + /* add language without specialisation to vector */ + if (lunder != lstart) { + nlang = malloc(lunder - lstart + 1); + memcpy(nlang, envlang + lstart, lunder - lstart); + nlang[lunder - lstart] = 0; + langv[++langc] = nlang; + } + + /* if we stopped at the dot, move to the colon delimiter */ + while ((envlang[lend] != 0) && + (envlang[lend] != ':')) { + lend++; + } + if (envlang[lend] == 0) { + /* reached end of environment language list */ + break; + } + lend++; + lstart = lunder = lend; + } + return langv; +} + +/** + * Create an array of valid paths to search for resources. * * The idea is that all the complex path computation to find resources * is performed here, once, rather than every time a resource is * searched for. */ char ** -fb_init_resource(const char *resource_path) +fb_init_resource_path(const char *resource_path) { char **pathv; /* resource path string vector */ char **respath; /* resource paths vector */ - const char *lang = NULL; + char **langv; pathv = filepath_path_to_strvec(resource_path); - respath = filepath_generate(pathv, &lang); + langv = get_language_names(); + + respath = filepath_generate(pathv, (const char * const *)langv); filepath_free_strvec(pathv); |