diff --git a/src/kernel/config.c b/src/kernel/config.c index 01fb7a7ee..d7ad98ecc 100644 --- a/src/kernel/config.c +++ b/src/kernel/config.c @@ -1736,12 +1736,51 @@ static const char * parameter_key(int i) return parameters[i]; } + +void init_terrains_translation(const struct locale *lang) { + void **tokens; + const terrain_type *terrain; + + tokens = get_translations(lang, UT_TERRAINS); + for (terrain = terrains(); terrain != NULL; terrain = terrain->next) { + variant var; + const char *name; + var.v = (void *)terrain; + name = LOC(lang, terrain->_name); + if (name) { + addtoken(tokens, name, var); + } + else { + log_error_n("no translation for terrain %s in locale %s", terrain->_name, locale_name(lang)); + } + } +} + +void init_options_translation(const struct locale * lang) { + void **tokens; + int i; + + tokens = get_translations(lang, UT_OPTIONS); + for (i = 0; i != MAXOPTIONS; ++i) { + variant var; + var.i = i; + if (options[i]) { + const char *name = LOC(lang, options[i]); + if (name) { + addtoken(tokens, name, var); + } + else { + log_error_n("no translation for OPTION %s in locale %s", options[i], locale_name(lang)); + } + } + } +} + static void init_locale(const struct locale *lang) { variant var; int i; const struct race *rc; - const terrain_type *terrain; void **tokens; tokens = get_translations(lang, UT_MAGIC); @@ -1755,12 +1794,19 @@ static void init_locale(const struct locale *lang) sstr = _strdup(str); tok = strtok(sstr, " "); while (tok) { + const char *name; for (i = 0; i != MAXMAGIETYP; ++i) { if (strcmp(tok, magic_school[i]) == 0) break; } assert(i != MAXMAGIETYP); var.i = i; - addtoken(tokens, LOC(lang, mkname("school", tok)), var); + name = LOC(lang, mkname("school", tok)); + if (name) { + addtoken(tokens, name, var); + } + else { + log_error_n("no translation for magic school %s in locale %s", tok, locale_name(lang)); + } tok = strtok(NULL, " "); } free(sstr); @@ -1772,24 +1818,18 @@ static void init_locale(const struct locale *lang) tokens = get_translations(lang, UT_RACES); for (rc = races; rc; rc = rc->next) { + const char *name; var.v = (void *)rc; - addtoken(tokens, LOC(lang, rc_name(rc, 1)), var); - addtoken(tokens, LOC(lang, rc_name(rc, 0)), var); + name = LOC(lang, rc_name(rc, 1)); + if (name) addtoken(tokens, name, var); + name = LOC(lang, rc_name(rc, 0)); + if (name) addtoken(tokens, name, var); } init_translations(lang, UT_PARAMS, parameter_key, MAXPARAMS); - tokens = get_translations(lang, UT_OPTIONS); - for (i = 0; i != MAXOPTIONS; ++i) { - var.i = i; - if (options[i]) addtoken(tokens, LOC(lang, options[i]), var); - } - - tokens = get_translations(lang, UT_TERRAINS); - for (terrain = terrains(); terrain != NULL; terrain = terrain->next) { - var.v = (void *)terrain; - addtoken(tokens, LOC(lang, terrain->_name), var); - } + init_options_translation(lang); + init_terrains_translation(lang); } typedef struct param { @@ -1918,10 +1958,7 @@ void init_locales(void) { int l; for (l = 0; localenames[l]; ++l) { - const struct locale *lang = get_locale(localenames[l]); - if (!lang) { - lang = get_or_create_locale(localenames[l]); - } + const struct locale *lang = get_or_create_locale(localenames[l]); init_locale(lang); } } diff --git a/src/kernel/config.h b/src/kernel/config.h index 6aef0a61e..859f5e0a6 100644 --- a/src/kernel/config.h +++ b/src/kernel/config.h @@ -109,6 +109,7 @@ extern "C" { /* ------------------------------------------------------------- */ void add_translation(struct critbit_tree **cb, const char *str, int i); void init_translations(const struct locale *lang, int ut, const char * (*string_cb)(int i), int maxstrings); + void init_locales(void); int shipspeed(const struct ship *sh, const struct unit *u); #define i2b(i) ((bool)((i)?(true):(false))) diff --git a/src/kernel/jsonconf.c b/src/kernel/jsonconf.c index 829e93113..232a8890a 100644 --- a/src/kernel/jsonconf.c +++ b/src/kernel/jsonconf.c @@ -670,5 +670,6 @@ void json_config(cJSON *json) { log_error_n("config contains unknown attribute %s", child->string); } } + init_locales(); } diff --git a/src/kernel/jsonconf.test.c b/src/kernel/jsonconf.test.c index 5083f8e73..a9283ecc3 100644 --- a/src/kernel/jsonconf.test.c +++ b/src/kernel/jsonconf.test.c @@ -2,6 +2,7 @@ #include "types.h" #include "jsonconf.h" +#include "config.h" #include "building.h" #include "direction.h" #include "item.h" @@ -97,6 +98,23 @@ static void test_races(CuTest * tc) test_cleanup(); } +static void test_findrace(CuTest *tc) { + const char * data = "{\"races\": { \"dwarf\": {} }, \"strings\": { \"de\" : { \"race::dwarf\" : \"Zwerg\" } } }"; + cJSON *json = cJSON_Parse(data); + const struct locale *lang; + const race *rc; + + CuAssertPtrNotNull(tc, json); + test_cleanup(); + lang = get_or_create_locale("de"); + CuAssertPtrEquals(tc, 0, (void *)findrace("Zwerg", lang)); + + json_config(json); + rc = findrace("Zwerg", lang); + CuAssertPtrNotNull(tc, rc); + CuAssertStrEquals(tc, "dwarf", rc->_name[0]); +} + static void test_items(CuTest * tc) { const char * data = "{\"items\": { " @@ -389,6 +407,7 @@ CuSuite *get_jsonconf_suite(void) SUITE_ADD_TEST(suite, test_castles); SUITE_ADD_TEST(suite, test_terrains); SUITE_ADD_TEST(suite, test_races); + SUITE_ADD_TEST(suite, test_findrace); SUITE_ADD_TEST(suite, test_strings); SUITE_ADD_TEST(suite, test_spells); SUITE_ADD_TEST(suite, test_flags); diff --git a/src/kernel/xmlreader.c b/src/kernel/xmlreader.c index 3832c9bb1..933fcaaa2 100644 --- a/src/kernel/xmlreader.c +++ b/src/kernel/xmlreader.c @@ -2175,7 +2175,6 @@ static int parse_strings(xmlDocPtr doc) xmlXPathFreeObject(strings); xmlXPathFreeContext(xpath); - init_locales(); return 0; } diff --git a/src/util/xml.c b/src/util/xml.c index b4f7dadac..078b08b9b 100644 --- a/src/util/xml.c +++ b/src/util/xml.c @@ -136,6 +136,7 @@ int read_xml(const char *filename, const char *catalog) result = 0; } xmlFreeDoc(doc); + init_locales(); return result; #else log_error("LIBXML2 disabled, cannot read %s.\n", filename);