REKRUTIERE RASSE funktioniert nicht (Bug 2012)

http://bugs.eressea.de/view.php?id=2012
init_locales must not be called before races have been configured.
make jsonconf initialize translations
handle missing strings in translations, log errors.
This commit is contained in:
Enno Rehling 2014-07-17 00:45:45 +02:00
parent 722be026b2
commit 1500bcac70
6 changed files with 78 additions and 20 deletions

View File

@ -1736,12 +1736,51 @@ static const char * parameter_key(int i)
return parameters[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) static void init_locale(const struct locale *lang)
{ {
variant var; variant var;
int i; int i;
const struct race *rc; const struct race *rc;
const terrain_type *terrain;
void **tokens; void **tokens;
tokens = get_translations(lang, UT_MAGIC); tokens = get_translations(lang, UT_MAGIC);
@ -1755,12 +1794,19 @@ static void init_locale(const struct locale *lang)
sstr = _strdup(str); sstr = _strdup(str);
tok = strtok(sstr, " "); tok = strtok(sstr, " ");
while (tok) { while (tok) {
const char *name;
for (i = 0; i != MAXMAGIETYP; ++i) { for (i = 0; i != MAXMAGIETYP; ++i) {
if (strcmp(tok, magic_school[i]) == 0) break; if (strcmp(tok, magic_school[i]) == 0) break;
} }
assert(i != MAXMAGIETYP); assert(i != MAXMAGIETYP);
var.i = i; 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, " "); tok = strtok(NULL, " ");
} }
free(sstr); free(sstr);
@ -1772,24 +1818,18 @@ static void init_locale(const struct locale *lang)
tokens = get_translations(lang, UT_RACES); tokens = get_translations(lang, UT_RACES);
for (rc = races; rc; rc = rc->next) { for (rc = races; rc; rc = rc->next) {
const char *name;
var.v = (void *)rc; var.v = (void *)rc;
addtoken(tokens, LOC(lang, rc_name(rc, 1)), var); name = LOC(lang, rc_name(rc, 1));
addtoken(tokens, LOC(lang, rc_name(rc, 0)), var); 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); init_translations(lang, UT_PARAMS, parameter_key, MAXPARAMS);
tokens = get_translations(lang, UT_OPTIONS); init_options_translation(lang);
for (i = 0; i != MAXOPTIONS; ++i) { init_terrains_translation(lang);
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);
}
} }
typedef struct param { typedef struct param {
@ -1918,10 +1958,7 @@ void init_locales(void)
{ {
int l; int l;
for (l = 0; localenames[l]; ++l) { for (l = 0; localenames[l]; ++l) {
const struct locale *lang = get_locale(localenames[l]); const struct locale *lang = get_or_create_locale(localenames[l]);
if (!lang) {
lang = get_or_create_locale(localenames[l]);
}
init_locale(lang); init_locale(lang);
} }
} }

View File

@ -109,6 +109,7 @@ extern "C" {
/* ------------------------------------------------------------- */ /* ------------------------------------------------------------- */
void add_translation(struct critbit_tree **cb, const char *str, int i); 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_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); int shipspeed(const struct ship *sh, const struct unit *u);
#define i2b(i) ((bool)((i)?(true):(false))) #define i2b(i) ((bool)((i)?(true):(false)))

View File

@ -670,5 +670,6 @@ void json_config(cJSON *json) {
log_error_n("config contains unknown attribute %s", child->string); log_error_n("config contains unknown attribute %s", child->string);
} }
} }
init_locales();
} }

View File

@ -2,6 +2,7 @@
#include "types.h" #include "types.h"
#include "jsonconf.h" #include "jsonconf.h"
#include "config.h"
#include "building.h" #include "building.h"
#include "direction.h" #include "direction.h"
#include "item.h" #include "item.h"
@ -97,6 +98,23 @@ static void test_races(CuTest * tc)
test_cleanup(); 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) static void test_items(CuTest * tc)
{ {
const char * data = "{\"items\": { " const char * data = "{\"items\": { "
@ -389,6 +407,7 @@ CuSuite *get_jsonconf_suite(void)
SUITE_ADD_TEST(suite, test_castles); SUITE_ADD_TEST(suite, test_castles);
SUITE_ADD_TEST(suite, test_terrains); SUITE_ADD_TEST(suite, test_terrains);
SUITE_ADD_TEST(suite, test_races); SUITE_ADD_TEST(suite, test_races);
SUITE_ADD_TEST(suite, test_findrace);
SUITE_ADD_TEST(suite, test_strings); SUITE_ADD_TEST(suite, test_strings);
SUITE_ADD_TEST(suite, test_spells); SUITE_ADD_TEST(suite, test_spells);
SUITE_ADD_TEST(suite, test_flags); SUITE_ADD_TEST(suite, test_flags);

View File

@ -2175,7 +2175,6 @@ static int parse_strings(xmlDocPtr doc)
xmlXPathFreeObject(strings); xmlXPathFreeObject(strings);
xmlXPathFreeContext(xpath); xmlXPathFreeContext(xpath);
init_locales();
return 0; return 0;
} }

View File

@ -136,6 +136,7 @@ int read_xml(const char *filename, const char *catalog)
result = 0; result = 0;
} }
xmlFreeDoc(doc); xmlFreeDoc(doc);
init_locales();
return result; return result;
#else #else
log_error("LIBXML2 disabled, cannot read %s.\n", filename); log_error("LIBXML2 disabled, cannot read %s.\n", filename);