1. nrmessage is locale-independent, uses locale_getstring

2. unique strings:
rename aurapotion50 message.
delete unused bithday_firework definitions.
This commit is contained in:
Enno Rehling 2018-05-17 19:03:49 +02:00
parent 9ca945cb2c
commit 7a10a58605
12 changed files with 22 additions and 121 deletions

View File

@ -1688,12 +1688,6 @@
<string name="mallornseed_p"> <string name="mallornseed_p">
<text locale="de">Mallornsamen</text> <text locale="de">Mallornsamen</text>
</string> </string>
<string name="birthday_firework">
<text locale="de">Feuerwerk</text>
</string>
<string name="birthday_firework_p">
<text locale="de">Feuerwerke</text>
</string>
<string name="lebkuchenherz"> <string name="lebkuchenherz">
<text locale="de">Lebkuchenherz mit der Aufschrift 'Erz und <text locale="de">Lebkuchenherz mit der Aufschrift 'Erz und
Stein, das ist fein'</text> Stein, das ist fein'</text>

View File

@ -874,12 +874,6 @@
<string name="mallornseed_p"> <string name="mallornseed_p">
<text locale="en">mallorn seeds</text> <text locale="en">mallorn seeds</text>
</string> </string>
<string name="birthday_firework">
<text locale="en">firework</text>
</string>
<string name="birthday_firework_p">
<text locale="en">fireworks</text>
</string>
<string name="lebkuchenherz"> <string name="lebkuchenherz">
<text locale="en">gingerbread heart</text> <text locale="en">gingerbread heart</text>
</string> </string>

View File

@ -881,12 +881,6 @@
<string name="mallornseed_p"> <string name="mallornseed_p">
<text locale="fr">graines de mallorn</text> <text locale="fr">graines de mallorn</text>
</string> </string>
<string name="birthday_firework">
<text locale="fr">feu d'artifice</text>
</string>
<string name="birthday_firework_p">
<text locale="fr">feux d'artifice</text>
</string>
<string name="lebkuchenherz"> <string name="lebkuchenherz">
<text locale="fr">coeur de pain d'épices</text> <text locale="fr">coeur de pain d'épices</text>
</string> </string>

View File

@ -750,38 +750,6 @@
<text locale="de">"$unit($unit) hat Glück und findet einen Hort von $int($amount) $resource($item,$amount)."</text> <text locale="de">"$unit($unit) hat Glück und findet einen Hort von $int($amount) $resource($item,$amount)."</text>
<text locale="en">"$unit($unit) luckily finds a cache of $int($amount) $resource($item,$amount)."</text> <text locale="en">"$unit($unit) luckily finds a cache of $int($amount) $resource($item,$amount)."</text>
</message> </message>
<message name="birthday_firework_noname_local" section="events">
<type>
<arg name="unit" type="unit"/>
</type>
<text locale="de">"$unit($unit) brennt ein großes Feuerwerk ab und Kaskaden bunter Sterne, leuchtende Wasserfälle aus Licht und strahlende Feuerdrachen erhellen den Himmel."</text>
<text locale="en">"A large firework is visible all over the sky."</text>
</message>
<message name="birthday_firework_noname" section="events">
<type>
<arg name="unit" type="unit"/>
<arg name="region" type="region"/>
</type>
<text locale="de">"In $region($region) wird ein großes Feuerwerk abgebrannt, welches noch hier zu bewundern ist. Kaskaden bunter Sterne, leuchtende Wasserfälle aus Licht und strahlende Feuerdrachen erhellen den Himmel."</text>
<text locale="en">"A large firework, visible all over the sky, has been started in $region($region)."</text>
</message>
<message name="birthday_firework_local" section="events">
<type>
<arg name="unit" type="unit"/>
<arg name="name" type="string"/>
</type>
<text locale="de">"Zur Feier des Geburtstags von ${name} brennt $unit($unit) ein großes Feuerwerk ab. Kaskaden bunter Sterne, leuchtende Wasserfälle aus Licht und strahlende Feuerdrachen erhellen den Himmel."</text>
<text locale="en">"A large firework in honor of ${name} is visible all over the sky."</text>
</message>
<message name="birthday_firework" section="events">
<type>
<arg name="unit" type="unit"/>
<arg name="region" type="region"/>
<arg name="name" type="string"/>
</type>
<text locale="de">"Zur Feier des Geburtstags von ${name} wird in $region($region) ein großes Feuerwerk abgebrannt, welches noch hier zu bewundern ist. Kaskaden bunter Sterne, leuchtende Wasserfälle aus Licht und strahlende Feuerdrachen erhellen den Himmel."</text>
<text locale="en">"A large firework in honor of ${name}, visible all over the sky, has been started in $region($region)."</text>
</message>
<message name="battle_critical" section="battle"> <message name="battle_critical" section="battle">
<type> <type>
<arg name="unit" type="unit"/> <arg name="unit" type="unit"/>
@ -7628,7 +7596,7 @@
<text locale="en">"$unit($unit) in $region($region): '$order($command)' - The $ship($ship) will now be faster."</text> <text locale="en">"$unit($unit) in $region($region): '$order($command)' - The $ship($ship) will now be faster."</text>
</message> </message>
<message name="aurapotion50" section="magic"> <message name="aurapotion50_effect" section="magic">
<type> <type>
<arg name="unit" type="unit"/> <arg name="unit" type="unit"/>
<arg name="region" type="region"/> <arg name="region" type="region"/>

View File

@ -86,7 +86,7 @@ function test_aurapotion50()
turn_begin() turn_begin()
turn_process() turn_process()
assert_equal(0, u:get_item("aurapotion50")) assert_equal(0, u:get_item("aurapotion50"))
assert_equal(1, f:count_msg_type('aurapotion50')) assert_equal(1, f:count_msg_type('aurapotion50_effect'))
assert_equal(50, u.aura) assert_equal(50, u.aura)
turn_end() turn_end()
end end

View File

@ -924,12 +924,6 @@ static void export_locale(const struct locale *lang, const char *name) {
export_strings(lang, F); export_strings(lang, F);
fclose(F); fclose(F);
} }
sprintf(fname, "messages.%2s.po", name);
F = fopen(fname, "wt");
if (F) {
export_messages(lang, F, NULL);
fclose(F);
}
} }
static int tolua_export_locales(lua_State *L) { static int tolua_export_locales(lua_State *L) {

View File

@ -512,14 +512,14 @@ static void report_crtypes(FILE * F, const struct locale *lang)
for (i = 0; i != MTMAXHASH; ++i) { for (i = 0; i != MTMAXHASH; ++i) {
struct known_mtype *kmt; struct known_mtype *kmt;
for (kmt = mtypehash[i]; kmt; kmt = kmt->nexthash) { for (kmt = mtypehash[i]; kmt; kmt = kmt->nexthash) {
const struct nrmessage_type *nrt = nrt_find(lang, kmt->mtype); const struct nrmessage_type *nrt = nrt_find(kmt->mtype);
if (nrt) { if (nrt) {
char buffer[DISPLAYSIZE]; char buffer[DISPLAYSIZE];
int hash = (int)kmt->mtype->key; int hash = (int)kmt->mtype->key;
assert(hash > 0); assert(hash > 0);
fprintf(F, "MESSAGETYPE %d\n", hash); fprintf(F, "MESSAGETYPE %d\n", hash);
fputc('\"', F); fputc('\"', F);
fputs(str_escape(nrt_string(nrt), buffer, sizeof(buffer)), F); fputs(str_escape(nrt_string(nrt, lang), buffer, sizeof(buffer)), F);
fputs("\";text\n", F); fputs("\";text\n", F);
fprintf(F, "\"%s\";section\n", nrt_section(nrt)); fprintf(F, "\"%s\";section\n", nrt_section(nrt));
} }

View File

@ -224,7 +224,7 @@ int amount, struct order *ord)
change_spellpoints(u, 50); change_spellpoints(u, 50);
ADDMSG(&u->faction->msgs, msg_message("aurapotion50", ADDMSG(&u->faction->msgs, msg_message("aurapotion50_effect",
"unit region command", u, u->region, ord)); "unit region command", u, u->region, ord));
use_pooled(u, itype->rtype, GET_DEFAULT, 1); use_pooled(u, itype->rtype, GET_DEFAULT, 1);

View File

@ -30,38 +30,26 @@
#define NRT_MAXHASH 1021 #define NRT_MAXHASH 1021
static nrmessage_type *nrtypes[NRT_MAXHASH]; static nrmessage_type *nrtypes[NRT_MAXHASH];
const char *nrt_string(const struct nrmessage_type *type) const char *nrt_string(const struct nrmessage_type *nrt, const struct locale *lang)
{ {
if (type->string) { return locale_getstring(lang, nrt->mtype->name);
return type->string;
}
return locale_get(type->lang, type->mtype->name);
} }
nrmessage_type *nrt_find(const struct locale * lang, nrmessage_type *nrt_find(const struct message_type * mtype)
const struct message_type * mtype)
{ {
nrmessage_type *found = NULL; nrmessage_type *found = NULL;
unsigned int hash = mtype->key % NRT_MAXHASH; unsigned int hash = mtype->key % NRT_MAXHASH;
nrmessage_type *type = nrtypes[hash]; nrmessage_type *type = nrtypes[hash];
while (type) { while (type) {
if (type->mtype == mtype) { if (type->mtype == mtype) {
if (found == NULL) if (found == NULL) {
found = type; found = type;
else if (type->lang == NULL)
found = type;
if (lang == type->lang) {
found = type;
break;
} }
} }
type = type->next; type = type->next;
} }
if (!found) { if (!found) {
log_warning("could not find nr-type %s for locale %s\n", mtype->name, locale_name(lang)); log_warning("could not find nr-type %s\n", mtype->name);
}
if (lang && found && found->lang != lang) {
log_warning("could not find nr-type %s for locale %s, using %s\n", mtype->name, locale_name(lang), locale_name(found->lang));
} }
return found; return found;
} }
@ -100,12 +88,11 @@ const nrsection *section_add(const char *name)
} }
void void
nrt_register(const struct message_type *mtype, const struct locale *lang, nrt_register(const struct message_type *mtype, const char *section)
const char *string, int level, const char *section)
{ {
unsigned int hash = mtype->key % NRT_MAXHASH; unsigned int hash = mtype->key % NRT_MAXHASH;
nrmessage_type *nrt = nrtypes[hash]; nrmessage_type *nrt = nrtypes[hash];
while (nrt && (nrt->lang != lang || nrt->mtype != mtype)) { while (nrt && nrt->mtype != mtype) {
nrt = nrt->next; nrt = nrt->next;
} }
if (nrt) { if (nrt) {
@ -117,10 +104,8 @@ const char *string, int level, const char *section)
char zNames[256]; char zNames[256];
char *c = zNames; char *c = zNames;
nrt = malloc(sizeof(nrmessage_type)); nrt = malloc(sizeof(nrmessage_type));
nrt->lang = lang;
nrt->mtype = mtype; nrt->mtype = mtype;
nrt->next = nrtypes[hash]; nrt->next = nrtypes[hash];
nrt->level = level;
if (section) { if (section) {
const nrsection *s = section_find(section); const nrsection *s = section_find(section);
if (s == NULL) { if (s == NULL) {
@ -131,8 +116,6 @@ const char *string, int level, const char *section)
else else
nrt->section = NULL; nrt->section = NULL;
nrtypes[hash] = nrt; nrtypes[hash] = nrt;
assert(string && *string);
nrt->string = str_strdup(string);
*c = '\0'; *c = '\0';
for (i = 0; i != mtype->nparameters; ++i) { for (i = 0; i != mtype->nparameters; ++i) {
if (i != 0) if (i != 0)
@ -147,11 +130,11 @@ size_t
nr_render(const struct message *msg, const struct locale *lang, char *buffer, nr_render(const struct message *msg, const struct locale *lang, char *buffer,
size_t size, const void *userdata) size_t size, const void *userdata)
{ {
struct nrmessage_type *nrt = nrt_find(lang, msg->type); struct nrmessage_type *nrt = nrt_find(msg->type);
if (nrt) { if (nrt) {
const char *m = const char *m =
translate(nrt_string(nrt), userdata, nrt->vars, msg->parameters); translate(nrt_string(nrt, lang), userdata, nrt->vars, msg->parameters);
if (m) { if (m) {
return str_strlcpy((char *)buffer, m, size); return str_strlcpy((char *)buffer, m, size);
} }
@ -164,15 +147,9 @@ size_t size, const void *userdata)
return 0; return 0;
} }
int nr_level(const struct message *msg)
{
nrmessage_type *nrt = nrt_find(NULL, msg->type);
return nrt ? nrt->level : 0;
}
const char *nr_section(const struct message *msg) const char *nr_section(const struct message *msg)
{ {
nrmessage_type *nrt = nrt_find(default_locale, msg->type); nrmessage_type *nrt = nrt_find(msg->type);
return nrt ? nrt->section : NULL; return nrt ? nrt->section : NULL;
} }
@ -187,20 +164,8 @@ void free_nrmesssages(void) {
while (nrtypes[i]) { while (nrtypes[i]) {
nrmessage_type *nr = nrtypes[i]; nrmessage_type *nr = nrtypes[i];
nrtypes[i] = nr->next; nrtypes[i] = nr->next;
free(nr->string);
free(nr->vars); free(nr->vars);
free(nr); free(nr);
} }
} }
} }
void export_messages(const struct locale * lang, FILE *F, const char *context) {
int i;
for (i = 0; i != NRT_MAXHASH; ++i) {
nrmessage_type *nrt = nrtypes[i];
while (nrt) {
po_write_msg(F, nrt->mtype->name, nrt->string, context);
nrt = nrt->next;
}
}
}

View File

@ -34,21 +34,15 @@ extern "C" {
void free_nrmesssages(void); void free_nrmesssages(void);
void nrt_register(const struct message_type *mtype, void nrt_register(const struct message_type *mtype, const char *section);
const struct locale *lang, const char *script, struct nrmessage_type *nrt_find(const struct message_type *);
int level, const char *section); const char *nrt_string(const struct nrmessage_type *nrt, const struct locale *lang);
struct nrmessage_type *nrt_find(const struct locale *, const char *nrt_section(const struct nrmessage_type *nrt);
const struct message_type *);
const char *nrt_string(const struct nrmessage_type *type);
const char *nrt_section(const struct nrmessage_type *mt);
size_t nr_render(const struct message *msg, const struct locale *lang, size_t nr_render(const struct message *msg, const struct locale *lang,
char *buffer, size_t size, const void *userdata); char *buffer, size_t size, const void *userdata);
int nr_level(const struct message *msg);
const char *nr_section(const struct message *msg); const char *nr_section(const struct message *msg);
void export_messages(const struct locale * lang, FILE *F, const char *context);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View File

@ -7,11 +7,8 @@
typedef struct nrmessage_type { typedef struct nrmessage_type {
const struct message_type *mtype; const struct message_type *mtype;
const struct locale *lang;
char *string;
char *vars; char *vars;
struct nrmessage_type *next; struct nrmessage_type *next;
int level;
const char *section; const char *section;
} nrmessage_type; } nrmessage_type;

View File

@ -1368,6 +1368,8 @@ static int parse_messages(xmlDocPtr doc)
if (propSection == NULL) if (propSection == NULL)
propSection = BAD_CAST default_section; propSection = BAD_CAST default_section;
nrt_register(mtype, (const char *)propSection);
/* strings */ /* strings */
xpath->node = node; xpath->node = node;
result = xmlXPathEvalExpression(BAD_CAST "text", xpath); result = xmlXPathEvalExpression(BAD_CAST "text", xpath);
@ -1380,8 +1382,7 @@ static int parse_messages(xmlDocPtr doc)
xml_readtext(node, &lang, &propText); xml_readtext(node, &lang, &propText);
if (lang) { if (lang) {
xml_cleanup_string(propText); xml_cleanup_string(propText);
nrt_register(mtype, lang, (const char *)propText, 0, locale_setstring(lang, mtype->name, (const char *)propText);
(const char *)propSection);
} }
xmlFree(propText); xmlFree(propText);