i18n: centralize command guidance text

This commit is contained in:
m1ngsama 2026-05-23 19:45:53 +08:00
parent 4fb531771b
commit 0cf8ac6759
5 changed files with 73 additions and 47 deletions

View file

@ -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

View file

@ -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);

View file

@ -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 <en|zh>\n",
i18n_lang_code(client->help_lang));
} else {
buffer_appendf(output, sizeof(output), &pos,
"Current language: %s\n"
"Usage: lang <en|zh>\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 <en|zh>\n", arg);
} else {
buffer_appendf(output, sizeof(output), &pos,
"Unsupported language: %s\n"
"Usage: lang <en|zh>\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';

View file

@ -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 <en|zh>\n";
case I18N_LANG_SET_FORMAT:
return "语言已切换为: %s\n";
case I18N_LANG_UNSUPPORTED_FORMAT:
return "不支持的语言: %s\n"
"用法: lang <en|zh>\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 <en|zh>\n";
case I18N_LANG_SET_FORMAT:
return "Language set to: %s\n";
case I18N_LANG_UNSUPPORTED_FORMAT:
return "Unsupported language: %s\n"
"Usage: lang <en|zh>\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 "";

View file

@ -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);
}