diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index 3a8bf03..e511edd 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -29,6 +29,8 @@ including the narrow-terminal fallback. - Full-screen help and COMMAND-mode help now live in a dedicated `help_text` module, keeping large bilingual help copy out of TUI and command flow code. +- Session language, unknown-command, suggestion, and continuation prompts now + use the shared i18n table instead of inline command-flow conditionals. ### Changed - NORMAL mode now opens at the latest visible messages instead of the oldest diff --git a/include/i18n.h b/include/i18n.h index 2a5ebcd..9d19e71 100644 --- a/include/i18n.h +++ b/include/i18n.h @@ -44,7 +44,14 @@ typedef enum { I18N_MUTE_JOINS_FORMAT, I18N_MUTE_JOINS_MUTED, I18N_MUTE_JOINS_UNMUTED, - I18N_CLEAR_DONE + I18N_CLEAR_DONE, + I18N_LANG_CURRENT_FORMAT, + I18N_LANG_SET_FORMAT, + I18N_LANG_UNSUPPORTED_FORMAT, + I18N_UNKNOWN_COMMAND_FORMAT, + I18N_DID_YOU_MEAN_FORMAT, + I18N_UNKNOWN_GUIDANCE, + I18N_CONTINUE_PROMPT } i18n_text_id_t; bool i18n_try_parse_lang(const char *value, help_lang_t *lang); diff --git a/src/commands.c b/src/commands.c index 6da7262..2fe2e3c 100644 --- a/src/commands.c +++ b/src/commands.c @@ -189,38 +189,21 @@ void commands_dispatch(client_t *client) { } if (!arg || arg[0] == '\0') { - if (client->help_lang == LANG_ZH) { - buffer_appendf(output, sizeof(output), &pos, - "当前语言: %s\n" - "用法: lang \n", - i18n_lang_code(client->help_lang)); - } else { - buffer_appendf(output, sizeof(output), &pos, - "Current language: %s\n" - "Usage: lang \n", - i18n_lang_code(client->help_lang)); - } + buffer_appendf(output, sizeof(output), &pos, + i18n_text(client->help_lang, + I18N_LANG_CURRENT_FORMAT), + i18n_lang_code(client->help_lang)); } else if (i18n_try_parse_lang(arg, &next_lang)) { client->help_lang = next_lang; - if (client->help_lang == LANG_ZH) { - buffer_appendf(output, sizeof(output), &pos, - "语言已切换为: %s\n", - i18n_lang_code(client->help_lang)); - } else { - buffer_appendf(output, sizeof(output), &pos, - "Language set to: %s\n", - i18n_lang_code(client->help_lang)); - } + buffer_appendf(output, sizeof(output), &pos, + i18n_text(client->help_lang, + I18N_LANG_SET_FORMAT), + i18n_lang_code(client->help_lang)); } else { - if (client->help_lang == LANG_ZH) { - buffer_appendf(output, sizeof(output), &pos, - "不支持的语言: %s\n" - "用法: lang \n", arg); - } else { - buffer_appendf(output, sizeof(output), &pos, - "Unsupported language: %s\n" - "Usage: lang \n", arg); - } + buffer_appendf(output, sizeof(output), &pos, + i18n_text(client->help_lang, + I18N_LANG_UNSUPPORTED_FORMAT), + arg); } } else if (strcmp(cmd, "msg") == 0 || strcmp(cmd, "w") == 0 || @@ -477,28 +460,22 @@ void commands_dispatch(client_t *client) { } else { const char *suggestion = suggest_command(cmd); buffer_appendf(output, sizeof(output), &pos, - client->help_lang == LANG_ZH ? - "未知命令: %s\n" : "Unknown command: %s\n", cmd); + i18n_text(client->help_lang, + I18N_UNKNOWN_COMMAND_FORMAT), + cmd); if (suggestion) { - if (client->help_lang == LANG_ZH) { - buffer_appendf(output, sizeof(output), &pos, - "你是想输入 :%s 吗?\n", suggestion); - } else { - buffer_appendf(output, sizeof(output), &pos, - "Did you mean :%s?\n", suggestion); - } + buffer_appendf(output, sizeof(output), &pos, + i18n_text(client->help_lang, + I18N_DID_YOU_MEAN_FORMAT), + suggestion); } - buffer_appendf(output, sizeof(output), &pos, - client->help_lang == LANG_ZH ? - "输入 :support 查看引导,或 :help 查看命令\n" : - "Type :support for guidance or :help for commands\n"); + buffer_appendf(output, sizeof(output), &pos, "%s", + i18n_text(client->help_lang, I18N_UNKNOWN_GUIDANCE)); } cmd_done: - buffer_appendf(output, sizeof(output), &pos, - client->help_lang == LANG_ZH ? - "\n按任意键继续..." : - "\nPress any key to continue..."); + buffer_appendf(output, sizeof(output), &pos, "%s", + i18n_text(client->help_lang, I18N_CONTINUE_PROMPT)); snprintf(client->command_output, sizeof(client->command_output), "%s", output); client->command_input[0] = '\0'; diff --git a/src/i18n.c b/src/i18n.c index 0611e30..d29bc02 100644 --- a/src/i18n.c +++ b/src/i18n.c @@ -154,6 +154,22 @@ const char *i18n_text(help_lang_t lang, i18n_text_id_t id) { return "已开启"; case I18N_CLEAR_DONE: return "命令输出已清空\n"; + case I18N_LANG_CURRENT_FORMAT: + return "当前语言: %s\n" + "用法: lang \n"; + case I18N_LANG_SET_FORMAT: + return "语言已切换为: %s\n"; + case I18N_LANG_UNSUPPORTED_FORMAT: + return "不支持的语言: %s\n" + "用法: lang \n"; + case I18N_UNKNOWN_COMMAND_FORMAT: + return "未知命令: %s\n"; + case I18N_DID_YOU_MEAN_FORMAT: + return "你是想输入 :%s 吗?\n"; + case I18N_UNKNOWN_GUIDANCE: + return "输入 :support 查看引导,或 :help 查看命令\n"; + case I18N_CONTINUE_PROMPT: + return "\n按任意键继续..."; } } @@ -241,6 +257,22 @@ const char *i18n_text(help_lang_t lang, i18n_text_id_t id) { return "unmuted"; case I18N_CLEAR_DONE: return "Command output cleared\n"; + case I18N_LANG_CURRENT_FORMAT: + return "Current language: %s\n" + "Usage: lang \n"; + case I18N_LANG_SET_FORMAT: + return "Language set to: %s\n"; + case I18N_LANG_UNSUPPORTED_FORMAT: + return "Unsupported language: %s\n" + "Usage: lang \n"; + case I18N_UNKNOWN_COMMAND_FORMAT: + return "Unknown command: %s\n"; + case I18N_DID_YOU_MEAN_FORMAT: + return "Did you mean :%s?\n"; + case I18N_UNKNOWN_GUIDANCE: + return "Type :support for guidance or :help for commands\n"; + case I18N_CONTINUE_PROMPT: + return "\nPress any key to continue..."; } return ""; diff --git a/tests/unit/test_i18n.c b/tests/unit/test_i18n.c index 691d945..4dc4780 100644 --- a/tests/unit/test_i18n.c +++ b/tests/unit/test_i18n.c @@ -90,6 +90,14 @@ TEST(text_lookup_matches_language) { "Search") != NULL); assert(strstr(i18n_text(LANG_ZH, I18N_SEARCH_HEADER_FORMAT), "搜索") != NULL); + assert(strstr(i18n_text(LANG_EN, I18N_LANG_CURRENT_FORMAT), + "Current language") != NULL); + assert(strstr(i18n_text(LANG_ZH, I18N_LANG_CURRENT_FORMAT), + "当前语言") != NULL); + assert(strstr(i18n_text(LANG_EN, I18N_UNKNOWN_COMMAND_FORMAT), + "Unknown command") != NULL); + assert(strstr(i18n_text(LANG_ZH, I18N_UNKNOWN_COMMAND_FORMAT), + "未知命令") != NULL); assert(strcmp(i18n_lang_code(LANG_EN), "en") == 0); assert(strcmp(i18n_lang_code(LANG_ZH), "zh") == 0); }