read keyword translations from JSON configuration.

This commit is contained in:
Enno Rehling 2014-06-16 11:30:23 -07:00
parent 645a7fcde6
commit 249a4390c4
3 changed files with 63 additions and 2 deletions

View File

@ -16,6 +16,8 @@ without prior permission by the authors of Eressea.
/* kernel includes */
#include "building.h"
#include "direction.h"
#include "keyword.h"
#include "equipment.h"
#include "item.h"
#include "messages.h"
@ -265,6 +267,42 @@ void json_directions(cJSON *json) {
}
}
static void json_keyword(cJSON *json, struct locale *lang) {
cJSON *child;
if (json->type!=cJSON_Object) {
log_error("keywords for locale `%s` not a json object: %d\n", locale_name(lang), json->type);
return;
}
for (child=json->child;child;child=child->next) {
keyword_t kwd = findkeyword(child->string);
if (kwd!=NOKEYWORD) {
if (child->type==cJSON_String) {
init_keyword(lang, kwd, child->valuestring);
}
else if (child->type==cJSON_Array) {
cJSON *entry;
for (entry=child->child;entry;entry=entry->next) {
init_keyword(lang, kwd, entry->valuestring);
}
} else {
log_error("invalid type %d for keyword `%s`\n", child->type, child->string);
}
}
}
}
void json_keywords(cJSON *json) {
cJSON *child;
if (json->type!=cJSON_Object) {
log_error("keywords is not a json object: %d\n", json->type);
return;
}
for (child=json->child;child;child=child->next) {
struct locale * lang = get_or_create_locale(child->string);
json_keyword(child, lang);
}
}
void json_races(cJSON *json) {
cJSON *child;
if (json->type!=cJSON_Object) {
@ -292,6 +330,9 @@ void json_config(cJSON *json) {
else if (strcmp(child->string, "directions")==0) {
json_directions(child);
}
else if (strcmp(child->string, "keywords")==0) {
json_keywords(child);
}
else if (strcmp(child->string, "buildings")==0) {
json_buildings(child);
}

View File

@ -4,6 +4,7 @@
#include "building.h"
#include "direction.h"
#include "keyword.h"
#include "race.h"
#include "ship.h"
#include "terrain.h"
@ -166,9 +167,29 @@ static void test_directions(CuTest * tc)
test_cleanup();
}
static void test_keywords(CuTest * tc)
{
const char * data = "{\"keywords\": { \"de\" : { \"NACH\" : \"nach\", \"LERNEN\" : \"lernen\" }}}";
const struct locale * lang;
cJSON *json = cJSON_Parse(data);
test_cleanup();
lang = get_or_create_locale("de");
CuAssertPtrNotNull(tc, json);
CuAssertIntEquals(tc, NOKEYWORD, get_keyword("potato", lang));
json_config(json);
CuAssertIntEquals(tc, K_STUDY, get_keyword("lerne", lang));
CuAssertIntEquals(tc, K_MOVE, get_keyword("nach", lang));
test_cleanup();
}
CuSuite *get_jsonconf_suite(void)
{
CuSuite *suite = CuSuiteNew();
SUITE_ADD_TEST(suite, test_keywords);
SUITE_ADD_TEST(suite, test_directions);
SUITE_ADD_TEST(suite, test_ships);
SUITE_ADD_TEST(suite, test_buildings);

View File

@ -53,8 +53,7 @@ keyword_t get_keyword(const char *s, const struct locale *lang) {
const void *match;
void **tokens = get_translations(lang, UT_KEYWORDS);
critbit_tree *cb = (critbit_tree *)*tokens;
assert(cb);
if (cb_find_prefix(cb, str, strlen(str), &match, 1, 0)) {
if (cb && cb_find_prefix(cb, str, strlen(str), &match, 1, 0)) {
cb_get_kv(match, &i, sizeof(int));
result = (keyword_t)i;
return keyword_disabled(result) ? NOKEYWORD : result;