read keywords from json configuration, use German imperative forms.

This commit is contained in:
Enno Rehling 2014-10-29 19:40:09 +01:00
parent f9c5d58f41
commit 6bd52012c0
19 changed files with 177 additions and 233 deletions

5
conf/e2/config.json Normal file
View File

@ -0,0 +1,5 @@
{
"include": [
"keywords.json"
]
}

5
conf/e3/config.json Normal file
View File

@ -0,0 +1,5 @@
{
"include": [
"keywords.json"
]
}

5
conf/e4/config.json Normal file
View File

@ -0,0 +1,5 @@
{
"include": [
"keywords.json"
]
}

65
conf/keywords.json Normal file
View File

@ -0,0 +1,65 @@
{
"keywords": {
"de": {
"//" : "//",
"banner": "BANNER",
"work": [ "ARBEITE", "ARBEITEN" ],
"attack": ["ATTACKIERE", "ATTACKIEREN"],
"steal": [ "BEKLAUE", "BEKLAUEN" ],
"besiege": ["BELAGERE", "BELAGERN" ],
"name": [ "BENENNE", "BENENNEN" ],
"use": [ "BENUTZE", "BENUTZEN" ],
"describe": [ "BESCHREIBE", "BESCHREIBEN" ],
"enter": ["BETRETE", "BETRETEN"],
"guard": ["BEWACHE", "BEWACHEN"],
"message": "BOTSCHAFT",
"end": "ENDE",
"ride": ["REITE", "REITEN"],
"number": "NUMMER",
"follow": ["FOLGE","FOLGEN"],
"research": ["FORSCHE", "FORSCHEN"],
"give": "GIB",
"help": [ "HELFE", "HELFEN" ],
"combat": [ "KÄMPFE", "KÄMPFEN" ],
"ready" : "KAMPFZAUBER",
"buy" : [ "KAUFE", "KAUFEN"],
"contact": [ "KONTAKT", "KONTAKTIEREN"],
"teach": ["LEHRE", "LEHREN"],
"study": ["LERNE", "LERNEN"],
"make": ["MACHE", "MACHEN"],
"maketemp": ["MACHE TEMP", "MACHETEMP"],
"move" : "NACH",
"password" : "PASSWORD",
"recruit": ["REKRUTIERE", "REKRUTIEREN"],
"reserve": ["RESERVIERE", "RESERVIEREN"],
"route": "ROUTE",
"sabotage": ["SABOTIERE", "SABOTIEREN"],
"option": "OPTION",
"spy": ["SPIONIERE", "SPIONIEREN"],
"quit": "STIRB",
"hide": ["TARNE", "TARNEN"],
"carry": ["TRANSPORTIERE", "TRANSPORTIEREN"],
"tax": ["TREIBE", "TREIBEN", "STEUERN"],
"entertain": ["UNTERHALTE", "UNTERHALTEN"],
"sell": ["VERKAUFE", "VERKAUFEN"],
"leave": ["VERLASSE", "VERLASSEN"],
"forget": ["VERGISS", "VERGESSEN"],
"cast": ["ZAUBERE", "ZAUBERN"],
"show": ["ZEIGE", "ZEIGEN"],
"destroy": ["ZERSTÖRE", "ZERSTÖREN"],
"grow": ["ZÜCHTE", "ZÜCHTEN"],
"default": "DEFAULT",
"origin": "URSPRUNG",
"email": "EMAIL",
"piracy": "PIRATERIE",
"group": "GRUPPE",
"sort": ["SORTIERE", "SORTIEREN"],
"prefix": "PRÄFIX",
"plant": ["PFLANZE", "PFLANZEN"],
"alliance": "ALLIANZ",
"claim": ["BEANSPRUCHE", "BEANSPRUCHEN"],
"promote": ["BEFÖRDERE", "BEFÖRDERUNG"],
"pay": ["BEZAHLE", "BEZAHLEN"]
}
}
}

View File

@ -2111,199 +2111,6 @@
</string> </string>
</namespace> </namespace>
<namespace name="keyword">
<!--Befehle -->
<string name="//">
<text locale="de">//</text>
</string>
<string name="banner">
<text locale="de">BANNER</text>
</string>
<string name="work">
<text locale="de">ARBEITEN</text>
</string>
<string name="attack">
<text locale="de">ATTACKIEREN</text>
</string>
<string name="claim">
<text locale="de">BEANSPRUCHEN</text>
</string>
<string name="steal">
<text locale="de">BEKLAUEN</text>
</string>
<string name="besiege">
<text locale="de">BELAGERE</text>
</string>
<string name="name">
<text locale="de">BENENNEN</text>
</string>
<string name="use">
<text locale="de">BENUTZEN</text>
</string>
<string name="describe">
<text locale="de">BESCHREIBEN</text>
</string>
<string name="enter">
<text locale="de">BETRETEN</text>
</string>
<string name="give">
<text locale="de">GIB</text>
</string>
<string name="guard">
<text locale="de">BEWACHEN</text>
</string>
<string name="message">
<text locale="de">BOTSCHAFT</text>
</string>
<string name="end">
<text locale="de">ENDE</text>
</string>
<string name="ride">
<text locale="de">FAHREN</text>
</string>
<string name="number">
<text locale="de">NUMMER</text>
</string>
<string name="follow">
<text locale="de">FOLGEN</text>
</string>
<string name="research">
<text locale="de">FORSCHEN</text>
</string>
<string name="help">
<text locale="de">HELFEN</text>
</string>
<string name="combat">
<text locale="de">KÄMPFEN</text>
</string>
<string name="ready">
<text locale="de">KAMPFZAUBER</text>
</string>
<string name="buy">
<text locale="de">KAUFEN</text>
</string>
<string name="contact">
<text locale="de">KONTAKTIEREN</text>
</string>
<string name="teach">
<text locale="de">LEHREN</text>
</string>
<string name="study">
<text locale="de">LERNEN</text>
</string>
<string name="locale">
<text locale="de">LOCALE</text>
</string>
<string name="make">
<text locale="de">MACHEN</text>
</string>
<string name="maketemp">
<text locale="de">MACHETEMP</text>
</string>
<string name="move">
<text locale="de">NACH</text>
</string>
<string name="alliance">
<text locale="de">ALLIANZ</text>
</string>
<string name="promote">
<text locale="de">BEFÖRDERUNG</text>
</string>
<string name="pay">
<text locale="de">BEZAHLEN</text>
</string>
<string name="plant">
<text locale="de">PFLANZEN</text>
</string>
<string name="prefix">
<text locale="de">PRÄFIX</text>
</string>
<string name="info">
<text locale="de">INFO</text>
</string>
<string name="password">
<text locale="de">PASSWORT</text>
</string>
<string name="recruit">
<text locale="de">REKRUTIEREN</text>
</string>
<string name="reserve">
<text locale="de">RESERVIEREN</text>
</string>
<string name="route">
<text locale="de">ROUTE</text>
</string>
<string name="sabotage">
<text locale="de">SABOTIEREN</text>
</string>
<string name="option">
<text locale="de">OPTION</text>
</string>
<string name="spy">
<text locale="de">SPIONIEREN</text>
</string>
<string name="quit">
<text locale="de">STIRB</text>
</string>
<string name="hide">
<text locale="de">TARNEN</text>
</string>
<string name="carry">
<text locale="de">TRANSPORTIEREN</text>
</string>
<string name="tax">
<text locale="de">TREIBEN</text>
</string>
<string name="entertain">
<text locale="de">UNTERHALTEN</text>
</string>
<string name="sell">
<text locale="de">VERKAUFEN</text>
</string>
<string name="leave">
<text locale="de">VERLASSEN</text>
</string>
<string name="forget">
<text locale="de">VERGESSEN</text>
</string>
<string name="cast">
<text locale="de">ZAUBERE</text>
</string>
<string name="show">
<text locale="de">ZEIGEN</text>
</string>
<string name="destroy">
<text locale="de">ZERSTÖREN</text>
</string>
<string name="grow">
<text locale="de">ZÜCHTEN</text>
</string>
<string name="default">
<text locale="de">DEFAULT</text>
</string>
<string name="report">
<text locale="de">REPORT</text>
</string>
<string name="origin">
<text locale="de">URSPRUNG</text>
</string>
<string name="email">
<text locale="de">EMAIL</text>
</string>
<string name="piracy">
<text locale="de">PIRATERIE</text>
</string>
<string name="locale">
<text locale="de">LOCALE</text>
</string>
<string name="group">
<text locale="de">GRUPPE</text>
</string>
<string name="sort">
<text locale="de">SORTIEREN</text>
</string>
</namespace>
<!--NR generieren --> <!--NR generieren -->
<string name="nr_options"> <string name="nr_options">
<text locale="de">Optionen</text> <text locale="de">Optionen</text>

View File

@ -1,9 +1,10 @@
local confdir = 'conf/' local confdir = 'conf/'
if config.rules then
confdir = confdir .. config.rules .. '/'
end
if config.install then if config.install then
confdir = config.install .. '/' .. confdir confdir = config.install .. '/' .. confdir
end end
read_xml(confdir .. 'config.xml', confdir .. 'catalog.xml') rules=''
eressea.config.read(confdir .. 'config.json') if config.rules then
rules = config.rules .. '/'
end
read_xml(confdir .. rules .. 'config.xml', confdir .. rules .. 'catalog.xml')
eressea.config.read(rules .. 'config.json', confdir)

View File

@ -50,9 +50,19 @@ int config_parse(const char *json)
return 1; return 1;
} }
int config_read(const char *filename) int config_read(const char *filename, const char * relpath)
{ {
FILE *F = fopen(filename, "rt"); char name[MAX_PATH];
FILE *F;
json_relpath = relpath;
if (relpath) {
_snprintf(name, sizeof(name), "%s/%s", relpath, filename);
F = fopen(name, "rt");
}
else {
F = fopen(filename, "rt");
}
if (F) { if (F) {
int result; int result;
char *data; char *data;

View File

@ -6,7 +6,7 @@ extern "C" {
void config_reset(void); void config_reset(void);
int config_parse(const char *json); int config_parse(const char *json);
int config_read(const char *filename); int config_read(const char *filename, const char * relpath);
#ifdef __cplusplus #ifdef __cplusplus
} }

View File

@ -5,7 +5,7 @@ $#include "bind_config.h"
module eressea { module eressea {
module config { module config {
void config_reset @ reset(void); void config_reset @ reset(void);
int config_read @ read(const char *filename); int config_read @ read(const char *filename, const char *relpath);
int config_parse @ parse(const char *json); int config_parse @ parse(const char *json);
} }
} }

View File

@ -57,16 +57,18 @@ static int tolua_config_eressea_config_read00(lua_State* tolua_S)
#ifndef TOLUA_RELEASE #ifndef TOLUA_RELEASE
tolua_Error tolua_err; tolua_Error tolua_err;
if ( if (
!tolua_isstring(tolua_S,1,0,&tolua_err) || !tolua_isstring(tolua_S, 1, 0, &tolua_err) ||
!tolua_isnoobj(tolua_S,2,&tolua_err) !tolua_isstring(tolua_S, 2, 0, &tolua_err) ||
!tolua_isnoobj(tolua_S,3,&tolua_err)
) )
goto tolua_lerror; goto tolua_lerror;
else else
#endif #endif
{ {
const char* filename = ((const char*) tolua_tostring(tolua_S,1,0)); const char* filename = ((const char*)tolua_tostring(tolua_S, 1, 0));
const char* relpath = ((const char*)tolua_tostring(tolua_S, 2, 0));
{ {
int tolua_ret = (int) config_read(filename); int tolua_ret = (int) config_read(filename, relpath);
tolua_pushnumber(tolua_S,(lua_Number)tolua_ret); tolua_pushnumber(tolua_S,(lua_Number)tolua_ret);
} }
} }

View File

@ -1961,20 +1961,6 @@ void kernel_done(void)
gc_done(); gc_done();
} }
const char *localenames[] = {
"de", "en",
NULL
};
void init_locales(void)
{
int l;
for (l = 0; localenames[l]; ++l) {
struct locale *lang = get_or_create_locale(localenames[l]);
init_locale(lang);
}
}
/* TODO: soll hier weg */ /* TODO: soll hier weg */
extern struct attrib_type at_shiptrail; extern struct attrib_type at_shiptrail;

View File

@ -101,7 +101,6 @@ 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)))
@ -171,7 +170,6 @@ extern "C" {
/* returns a value between [0..xpct_2], generated with two dice */ /* returns a value between [0..xpct_2], generated with two dice */
int distribute(int old, int new_value, int n); int distribute(int old, int new_value, int n);
void init_locales(void);
void init_locale(struct locale *lang); void init_locale(struct locale *lang);
int newunitid(void); int newunitid(void);

View File

@ -636,14 +636,24 @@ static void json_races(cJSON *json) {
} }
} }
static void json_configs(cJSON *json) { const char * json_relpath;
static void json_include(cJSON *json) {
cJSON *child; cJSON *child;
if (json->type != cJSON_Array) { if (json->type != cJSON_Array) {
log_error("config is not a json array: %d", json->type); log_error("config is not a json array: %d", json->type);
return; return;
} }
for (child = json->child; child; child = child->next) { for (child = json->child; child; child = child->next) {
FILE *F = fopen(child->valuestring, "rt"); FILE *F;
if (json_relpath) {
char name[MAX_PATH];
_snprintf(name, sizeof(name), "%s/%s", json_relpath, child->valuestring);
F = fopen(name, "rt");
}
else {
F = fopen(child->valuestring, "rt");
}
if (F) { if (F) {
cJSON *config; cJSON *config;
char *data; char *data;
@ -668,6 +678,7 @@ void json_config(cJSON *json) {
log_error("config is not a json object: %d", json->type); log_error("config is not a json object: %d", json->type);
return; return;
} }
reset_locales();
for (child=json->child;child;child=child->next) { for (child=json->child;child;child=child->next) {
if (strcmp(child->string, "races")==0) { if (strcmp(child->string, "races")==0) {
json_races(child); json_races(child);
@ -675,8 +686,8 @@ void json_config(cJSON *json) {
else if (strcmp(child->string, "items") == 0) { else if (strcmp(child->string, "items") == 0) {
json_items(child); json_items(child);
} }
else if (strcmp(child->string, "config") == 0) { else if (strcmp(child->string, "include") == 0) {
json_configs(child); json_include(child);
} }
else if (strcmp(child->string, "ships") == 0) { else if (strcmp(child->string, "ships") == 0) {
json_ships(child); json_ships(child);
@ -705,6 +716,5 @@ void json_config(cJSON *json) {
log_error("config contains unknown attribute %s", child->string); log_error("config contains unknown attribute %s", child->string);
} }
} }
init_locales();
} }

View File

@ -18,6 +18,7 @@ extern "C" {
struct cJSON; struct cJSON;
void json_config(struct cJSON *str); void json_config(struct cJSON *str);
extern const char * json_relpath;
#ifdef __cplusplus #ifdef __cplusplus
} }

View File

@ -10,6 +10,7 @@
#include "race.h" #include "race.h"
#include "ship.h" #include "ship.h"
#include "spell.h" #include "spell.h"
#include "order.h"
#include "terrain.h" #include "terrain.h"
#include "util/language.h" #include "util/language.h"
#include <CuTest.h> #include <CuTest.h>
@ -295,7 +296,7 @@ static void test_buildings(CuTest * tc)
static void test_configs(CuTest * tc) static void test_configs(CuTest * tc)
{ {
const char * data = "{\"config\": [ \"test.json\" ] }"; const char * data = "{\"include\": [ \"test.json\" ] }";
FILE *F; FILE *F;
cJSON *json = cJSON_Parse(data); cJSON *json = cJSON_Parse(data);
@ -416,6 +417,26 @@ static void test_strings(CuTest * tc)
CuAssertStrEquals(tc, "LERNEN", locale_string(lang, "study")); CuAssertStrEquals(tc, "LERNEN", locale_string(lang, "study"));
} }
static void test_infinitive_from_config(CuTest *tc) {
char buffer[32];
struct locale *lang;
struct order *ord;
const char * data = "{\"keywords\": { \"de\" : { \"study\" : [ \"LERNE\", \"LERNEN\" ] }}}";
cJSON *json = cJSON_Parse(data);
CuAssertPtrNotNull(tc, json);
json_config(json);
lang = get_or_create_locale("de");
CuAssertIntEquals(tc, K_STUDY, get_keyword("LERN", lang));
CuAssertIntEquals(tc, K_STUDY, get_keyword("LERNE", lang));
CuAssertIntEquals(tc, K_STUDY, get_keyword("LERNEN", lang));
ord = create_order(K_STUDY, lang, "");
CuAssertStrEquals(tc, "LERNE", get_command(ord, buffer, sizeof(buffer)));
test_cleanup();
}
CuSuite *get_jsonconf_suite(void) CuSuite *get_jsonconf_suite(void)
{ {
CuSuite *suite = CuSuiteNew(); CuSuite *suite = CuSuiteNew();
@ -433,6 +454,7 @@ CuSuite *get_jsonconf_suite(void)
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);
SUITE_ADD_TEST(suite, test_infinitive_from_config);
return suite; return suite;
} }

View File

@ -1460,6 +1460,7 @@ int readgame(const char *filename, int backup)
storage store; storage store;
FILE *F; FILE *F;
init_locales();
log_printf(stdout, "- reading game data from %s\n", filename); log_printf(stdout, "- reading game data from %s\n", filename);
sprintf(path, "%s/%s", datapath(), filename); sprintf(path, "%s/%s", datapath(), filename);

View File

@ -4562,10 +4562,10 @@ int init_data(const char *filename, const char *catalog)
{ {
int l; int l;
l = read_xml(filename, catalog); l = read_xml(filename, catalog);
reset_locales();
if (l) { if (l) {
return l; return l;
} }
init_locales();
if (turn < 0) { if (turn < 0) {
turn = first_turn; turn = first_turn;
} }

View File

@ -243,8 +243,32 @@ void *get_translation(const struct locale *lang, const char *str, int index) {
return NULL; return NULL;
} }
void free_locales(void) const char *localenames[] = {
"de", "en",
NULL
};
extern void init_locale(struct locale *lang);
static int locale_init = 0;
void init_locales(void)
{ {
int l;
if (locale_init) return;
for (l = 0; localenames[l]; ++l) {
struct locale *lang = get_or_create_locale(localenames[l]);
init_locale(lang);
}
locale_init = 1;
}
void reset_locales(void) {
locale_init = 0;
}
void free_locales(void) {
locale_init = 0;
while (locales) { while (locales) {
int i; int i;
locale * next = locales->next; locale * next = locales->next;

View File

@ -29,7 +29,9 @@ extern "C" {
/** managing multiple locales: **/ /** managing multiple locales: **/
extern struct locale *get_locale(const char *name); extern struct locale *get_locale(const char *name);
extern struct locale *get_or_create_locale(const char *key); extern struct locale *get_or_create_locale(const char *key);
extern void free_locales(void); void init_locales(void);
void free_locales(void);
void reset_locales(void);
/** operations on locales: **/ /** operations on locales: **/
extern void locale_setstring(struct locale *lang, const char *key, extern void locale_setstring(struct locale *lang, const char *key,