diff options
author | David Robillard <d@drobilla.net> | 2011-02-11 04:58:06 +0000 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2011-02-11 04:58:06 +0000 |
commit | 38de3875424f70f89d617c228e0815680e300700 (patch) | |
tree | c848d26d01d5eb715c1bb0df924998e14774077f /src/util.c | |
parent | ad69b46ed592c3f8b680f29653702b03b0559963 (diff) | |
download | lilv-38de3875424f70f89d617c228e0815680e300700.tar.gz lilv-38de3875424f70f89d617c228e0815680e300700.tar.bz2 lilv-38de3875424f70f89d617c228e0815680e300700.zip |
Better, tested, i18n system.
git-svn-id: http://svn.drobilla.net/lad/trunk/slv2@2916 a436a847-0d15-0410-975c-d299462d15a1
Diffstat (limited to 'src/util.c')
-rw-r--r-- | src/util.c | 45 |
1 files changed, 29 insertions, 16 deletions
@@ -62,24 +62,37 @@ slv2_uri_to_path(const char* uri) return NULL; } -const char* +/** Return the current LANG converted to Turtle (i.e. RFC3066) style. + * For example, if LANG is set to "en_CA.utf-8", this returns "en-ca". + */ +char* slv2_get_lang() { - static char lang[32]; - lang[31] = '\0'; - char* tmp = getenv("LANG"); - if (!tmp) { - lang[0] = '\0'; - } else { - strncpy(lang, tmp, 31); - for (int i = 0; i < 31 && lang[i]; ++i) { - if (lang[i] == '_') { - lang[i] = '-'; - } else if ( !(lang[i] >= 'a' && lang[i] <= 'z') - && !(lang[i] >= 'A' && lang[i] <= 'Z')) { - lang[i] = '\0'; - break; - } + const char* const env_lang = getenv("LANG"); + if (!env_lang || !strcmp(env_lang, "") + || !strcmp(env_lang, "C") || !strcmp(env_lang, "POSIX")) { + return NULL; + } + + const size_t env_lang_len = strlen(env_lang); + char* const lang = malloc(env_lang_len + 1); + for (size_t i = 0; i < env_lang_len + 1; ++i) { + if (env_lang[i] == '_') { + lang[i] = '-'; // Convert _ to - + } else if (env_lang[i] >= 'A' && env_lang[i] <= 'Z') { + lang[i] = env_lang[i] + ('a' - 'A'); // Convert to lowercase + } else if (env_lang[i] >= 'a' && env_lang[i] <= 'z') { + lang[i] = env_lang[i]; // Lowercase letter, copy verbatim + } else if (env_lang[i] >= '0' && env_lang[i] <= '9') { + lang[i] = env_lang[i]; // Digit, copy verbatim + } else if (env_lang[i] == '\0' || env_lang[i] == '.') { + // End, or start of suffix (e.g. en_CA.utf-8), finished + lang[i] = '\0'; + break; + } else { + SLV2_ERRORF("Illegal LANG `%s' ignored\n", env_lang); + free(lang); + return NULL; } } |