diff options
author | Haru <haru@dotalux.com> | 2019-01-03 23:46:03 +0100 |
---|---|---|
committer | Dastgir <dastgirp@gmail.com> | 2019-10-02 12:15:36 +0530 |
commit | 953efc5da879df5048e42265fa30c4aaae58288c (patch) | |
tree | 6f9a4e4876a19f3f9366e5eac5538b40e04af541 /src/map | |
parent | ddb11b5ea7997173128f00796b1b90a0b1985c9e (diff) | |
download | hercules-953efc5da879df5048e42265fa30c4aaae58288c.tar.gz hercules-953efc5da879df5048e42265fa30c4aaae58288c.tar.bz2 hercules-953efc5da879df5048e42265fa30c4aaae58288c.tar.xz hercules-953efc5da879df5048e42265fa30c4aaae58288c.zip |
Load translations from a directory rather than a file
Diffstat (limited to 'src/map')
-rw-r--r-- | src/map/script.c | 82 | ||||
-rw-r--r-- | src/map/script.h | 5 |
2 files changed, 61 insertions, 26 deletions
diff --git a/src/map/script.c b/src/map/script.c index c6f1c9a12..967954fee 100644 --- a/src/map/script.c +++ b/src/map/script.c @@ -5189,8 +5189,8 @@ static void script_load_translations(void) size = libconfig->setting_length(translations); for(i = 0; i < size; i++) { - const char *translation_file = libconfig->setting_get_string_elem(translations, i); - total += script->load_translation(translation_file, ++lang_id); + const char *translation_dir = libconfig->setting_get_string_elem(translations, i); + total += script->load_translation(translation_dir, ++lang_id); } libconfig->destroy(&translations_conf); @@ -5227,39 +5227,39 @@ static void script_load_translations(void) } /** - * Generates a language name from a translation filename. + * Generates a language name from a translation directory name. * - * @param file The filename. + * @param directory The directory name. * @return The corresponding translation name. */ -static const char *script_get_translation_file_name(const char *file) +static const char *script_get_translation_dir_name(const char *directory) { const char *basename = NULL, *last_dot = NULL; - nullpo_retr("Unknown", file); + nullpo_retr("Unknown", directory); - basename = strrchr(file, '/');; + basename = strrchr(directory, '/'); #ifdef WIN32 { - const char *basename_windows = strrchr(file, '\\'); + const char *basename_windows = strrchr(directory, '\\'); if (basename_windows > basename) basename = basename_windows; } #endif // WIN32 if (basename == NULL) - basename = file; + basename = directory; else basename++; // Skip slash Assert_retr("Unknown", *basename != '\0'); last_dot = strrchr(basename, '.'); if (last_dot != NULL) { - static char file_name[200]; + static char dir_name[200]; if (last_dot == basename) return basename + 1; - safestrncpy(file_name, basename, last_dot - basename + 1); - return file_name; + safestrncpy(dir_name, basename, last_dot - basename + 1); + return dir_name; } return basename; @@ -5340,18 +5340,18 @@ static bool script_load_translation_addstring(const char *file, uint8 lang_id, c /** * Parses an individual translation file. * - * @param file The filename to parse. + * @param directory The directory structure to read. * @param lang_id The language identifier. * @return The amount of strings loaded. */ -static int script_load_translation(const char *file, uint8 lang_id) +static int script_load_translation_file(const char *file, uint8 lang_id) { - int translations = 0; char line[1024]; - char msgctxt[NAME_LENGTH*2+1] = { 0 }; - FILE *fp; - int lineno = 0; + char msgctxt[NAME_LENGTH*2+1] = ""; struct script_string_buf msgid, msgstr; + int translations = 0; + int lineno = 0; + FILE *fp; nullpo_ret(file); @@ -5363,10 +5363,6 @@ static int script_load_translation(const char *file, uint8 lang_id) VECTOR_INIT(msgid); VECTOR_INIT(msgstr); - script->add_language(script->get_translation_file_name(file)); - if (lang_id >= atcommand->max_message_table) - atcommand->expand_message_table(); - while (fgets(line, sizeof(line), fp) != NULL) { int len = (int)strlen(line); int i; @@ -5477,10 +5473,47 @@ static int script_load_translation(const char *file, uint8 lang_id) VECTOR_CLEAR(msgid); VECTOR_CLEAR(msgstr); - ShowStatus("Done reading '"CL_WHITE"%d"CL_RESET"' translations in '"CL_WHITE"%s"CL_RESET"'.\n", translations, file); return translations; } +struct load_translation_data { + uint8 lang_id; + int translation_count; +}; + +static void script_load_translation_sub(const char *filename, void *context) +{ + nullpo_retv(context); + + struct load_translation_data *data = context; + + data->translation_count += script->load_translation_file(filename, data->lang_id); +} + +/** + * Loads a translations directory + * + * @param directory The directory structure to read. + * @param lang_id The language identifier. + * @return The amount of strings loaded. + */ +static int script_load_translation(const char *directory, uint8 lang_id) +{ + struct load_translation_data data = { 0 }; + data.lang_id = lang_id; + + nullpo_ret(directory); + + script->add_language(script->get_translation_dir_name(directory)); + if (lang_id >= atcommand->max_message_table) + atcommand->expand_message_table(); + + findfile(directory, ".po", script_load_translation_sub, &data); + + ShowStatus("Done reading '"CL_WHITE"%d"CL_RESET"' translations in '"CL_WHITE"%s"CL_RESET"'.\n", data.translation_count, directory); + return data.translation_count; +} + /** * **/ @@ -27128,12 +27161,13 @@ void script_defaults(void) script->string_dup = script_string_dup; script->load_translations = script_load_translations; script->load_translation_addstring = script_load_translation_addstring; + script->load_translation_file = script_load_translation_file; script->load_translation = script_load_translation; script->translation_db_destroyer = script_translation_db_destroyer; script->clear_translations = script_clear_translations; script->parse_cleanup_timer = script_parse_cleanup_timer; script->add_language = script_add_language; - script->get_translation_file_name = script_get_translation_file_name; + script->get_translation_dir_name = script_get_translation_dir_name; script->parser_clean_leftovers = script_parser_clean_leftovers; script->run_use_script = script_run_use_script; diff --git a/src/map/script.h b/src/map/script.h index 62950ba8d..2aefaba4e 100644 --- a/src/map/script.h +++ b/src/map/script.h @@ -1005,12 +1005,13 @@ struct script_interface { int (*string_dup) (char *str); void (*load_translations) (void); bool (*load_translation_addstring) (const char *file, uint8 lang_id, const char *msgctxt, const struct script_string_buf *msgid, const struct script_string_buf *msgstr); - int (*load_translation) (const char *file, uint8 lang_id); + int (*load_translation_file) (const char *file, uint8 lang_id); + int (*load_translation) (const char *directory, uint8 lang_id); int (*translation_db_destroyer) (union DBKey key, struct DBData *data, va_list ap); void (*clear_translations) (bool reload); int (*parse_cleanup_timer) (int tid, int64 tick, int id, intptr_t data); uint8 (*add_language) (const char *name); - const char *(*get_translation_file_name) (const char *file); + const char *(*get_translation_dir_name) (const char *directory); void (*parser_clean_leftovers) (void); void (*run_use_script) (struct map_session_data *sd, struct item_data *data, int oid); void (*run_item_equip_script) (struct map_session_data *sd, struct item_data *data, int oid); |