help: use shared localized string helper

This commit is contained in:
m1ngsama 2026-05-24 16:22:23 +08:00
parent ed92aeb1e6
commit 6c8ea56e8d
3 changed files with 124 additions and 106 deletions

View file

@ -62,6 +62,8 @@
localized-string helper and English fallback path. localized-string helper and English fallback path.
- The concise `:help` manual text now uses the shared localized-string helper - The concise `:help` manual text now uses the shared localized-string helper
around the command-catalog rows. around the command-catalog rows.
- The full-screen key reference now uses the shared localized-string helper
around the command-catalog rows.
- Documented i18n and user-facing text rules for English-first source text, - Documented i18n and user-facing text rules for English-first source text,
stable command syntax, concise help copy, and translation-only localization. stable command syntax, concise help copy, and translation-only localization.

View file

@ -1,115 +1,116 @@
#include "help_text.h" #include "help_text.h"
#include "command_catalog.h" #include "command_catalog.h"
#include "i18n.h"
void help_text_append_full(char *buffer, size_t buf_size, size_t *pos, void help_text_append_full(char *buffer, size_t buf_size, size_t *pos,
ui_lang_t lang) { ui_lang_t lang) {
if (lang == UI_LANG_EN) { static const i18n_string_t before_commands = I18N_STRING(
buffer_appendf(buffer, buf_size, pos, "TNT KEY REFERENCE\n"
"TNT KEY REFERENCE\n" "\n"
"\n" "OPERATING MODES:\n"
"OPERATING MODES:\n" " INSERT - Type and send messages (default)\n"
" INSERT - Type and send messages (default)\n" " NORMAL - Browse message history\n"
" NORMAL - Browse message history\n" " COMMAND - Execute commands\n"
" COMMAND - Execute commands\n" "\n"
"\n" "INSERT MODE KEYS:\n"
"INSERT MODE KEYS:\n" " ESC - Enter NORMAL mode\n"
" ESC - Enter NORMAL mode\n" " Enter - Send message\n"
" Enter - Send message\n" " Backspace - Delete character\n"
" Backspace - Delete character\n" " Ctrl+W - Delete last word\n"
" Ctrl+W - Delete last word\n" " Ctrl+U - Delete line\n"
" Ctrl+U - Delete line\n" " Ctrl+C - Enter NORMAL mode\n"
" Ctrl+C - Enter NORMAL mode\n" "\n"
"\n" "NORMAL MODE KEYS:\n"
"NORMAL MODE KEYS:\n" " Opens at latest messages\n"
" Opens at latest messages\n" " Follows latest until you scroll up\n"
" Follows latest until you scroll up\n" " i - Return to INSERT mode\n"
" i - Return to INSERT mode\n" " : - Enter COMMAND mode\n"
" : - Enter COMMAND mode\n" " j/k - Scroll down/up one line\n"
" j/k - Scroll down/up one line\n" " Ctrl+D/U - Scroll half page down/up\n"
" Ctrl+D/U - Scroll half page down/up\n" " Ctrl+F/B - Scroll full page down/up\n"
" Ctrl+F/B - Scroll full page down/up\n" " PgDn/PgUp - Scroll full page down/up\n"
" PgDn/PgUp - Scroll full page down/up\n" " End/Home - Jump to bottom/top\n"
" End/Home - Jump to bottom/top\n" " g/G - Jump to top/bottom\n"
" g/G - Jump to top/bottom\n" " ? - Show full key reference\n"
" ? - Show full key reference\n" " Ctrl+C - Exit chat\n"
" Ctrl+C - Exit chat\n" "\n"
"\n" "AVAILABLE COMMANDS:\n",
"AVAILABLE COMMANDS:\n"); "TNT 按键参考\n"
command_catalog_append_full(buffer, buf_size, pos, lang); "\n"
buffer_appendf(buffer, buf_size, pos, "操作模式:\n"
"\n" " INSERT - 输入和发送消息(默认)\n"
"COMMAND OUTPUT KEYS:\n" " NORMAL - 浏览消息历史\n"
" q, ESC - Close output\n" " COMMAND - 执行命令\n"
" j/k - Scroll down/up\n" "\n"
" Ctrl+D/U - Scroll half page down/up\n" "INSERT 模式按键:\n"
" Ctrl+F/B - Scroll full page down/up\n" " ESC - 进入 NORMAL 模式\n"
" g/G - Jump to top/bottom\n" " Enter - 发送消息\n"
"\n" " Backspace - 删除字符\n"
"SPECIAL MESSAGES:\n" " Ctrl+W - 删除上个单词\n"
" /me <action> - Send action (e.g. /me waves)\n" " Ctrl+U - 删除整行\n"
" @username - Mention user (bell + highlight)\n" " Ctrl+C - 进入 NORMAL 模式\n"
"\n" "\n"
"HELP SCREEN KEYS:\n" "NORMAL 模式按键:\n"
" q, ESC - Close help\n" " 默认停在最新消息\n"
" j/k - Scroll down/up\n" " 未向上翻阅时自动跟随最新消息\n"
" Ctrl+D/U - Scroll half page down/up\n" " i - 返回 INSERT 模式\n"
" Ctrl+F/B - Scroll full page down/up\n" " : - 进入 COMMAND 模式\n"
" g/G - Jump to top/bottom\n" " j/k - 向下/上滚动一行\n"
" l - Cycle UI language\n"); " Ctrl+D/U - 向下/上滚动半页\n"
return; " Ctrl+F/B - 向下/上滚动整页\n"
} " PgDn/PgUp - 向下/上滚动整页\n"
" End/Home - 跳到底部/顶部\n"
" g/G - 跳到顶部/底部\n"
" ? - 显示完整按键参考\n"
" Ctrl+C - 退出聊天\n"
"\n"
"可用命令:\n"
);
static const i18n_string_t after_commands = I18N_STRING(
"\n"
"COMMAND OUTPUT KEYS:\n"
" q, ESC - Close output\n"
" j/k - Scroll down/up\n"
" Ctrl+D/U - Scroll half page down/up\n"
" Ctrl+F/B - Scroll full page down/up\n"
" g/G - Jump to top/bottom\n"
"\n"
"SPECIAL MESSAGES:\n"
" /me <action> - Send action (e.g. /me waves)\n"
" @username - Mention user (bell + highlight)\n"
"\n"
"HELP SCREEN KEYS:\n"
" q, ESC - Close help\n"
" j/k - Scroll down/up\n"
" Ctrl+D/U - Scroll half page down/up\n"
" Ctrl+F/B - Scroll full page down/up\n"
" g/G - Jump to top/bottom\n"
" l - Cycle UI language\n",
"\n"
"命令输出按键:\n"
" q, ESC - 关闭输出\n"
" j/k - 向下/上滚动\n"
" Ctrl+D/U - 向下/上滚动半页\n"
" Ctrl+F/B - 向下/上滚动整页\n"
" g/G - 跳到顶部/底部\n"
"\n"
"特殊消息:\n"
" /me <action> - 发送动作 (如 /me waves)\n"
" @username - 提及用户 (响铃+高亮)\n"
"\n"
"帮助界面按键:\n"
" q, ESC - 关闭帮助\n"
" j/k - 向下/上滚动\n"
" Ctrl+D/U - 向下/上滚动半页\n"
" Ctrl+F/B - 向下/上滚动整页\n"
" g/G - 跳到顶部/底部\n"
" l - 切换界面语言\n"
);
buffer_appendf(buffer, buf_size, pos, buffer_appendf(buffer, buf_size, pos, "%s",
"TNT 按键参考\n" i18n_string(before_commands, lang));
"\n"
"操作模式:\n"
" INSERT - 输入和发送消息(默认)\n"
" NORMAL - 浏览消息历史\n"
" COMMAND - 执行命令\n"
"\n"
"INSERT 模式按键:\n"
" ESC - 进入 NORMAL 模式\n"
" Enter - 发送消息\n"
" Backspace - 删除字符\n"
" Ctrl+W - 删除上个单词\n"
" Ctrl+U - 删除整行\n"
" Ctrl+C - 进入 NORMAL 模式\n"
"\n"
"NORMAL 模式按键:\n"
" 默认停在最新消息\n"
" 未向上翻阅时自动跟随最新消息\n"
" i - 返回 INSERT 模式\n"
" : - 进入 COMMAND 模式\n"
" j/k - 向下/上滚动一行\n"
" Ctrl+D/U - 向下/上滚动半页\n"
" Ctrl+F/B - 向下/上滚动整页\n"
" PgDn/PgUp - 向下/上滚动整页\n"
" End/Home - 跳到底部/顶部\n"
" g/G - 跳到顶部/底部\n"
" ? - 显示完整按键参考\n"
" Ctrl+C - 退出聊天\n"
"\n"
"可用命令:\n");
command_catalog_append_full(buffer, buf_size, pos, lang); command_catalog_append_full(buffer, buf_size, pos, lang);
buffer_appendf(buffer, buf_size, pos, buffer_appendf(buffer, buf_size, pos, "%s",
"\n" i18n_string(after_commands, lang));
"命令输出按键:\n"
" q, ESC - 关闭输出\n"
" j/k - 向下/上滚动\n"
" Ctrl+D/U - 向下/上滚动半页\n"
" Ctrl+F/B - 向下/上滚动整页\n"
" g/G - 跳到顶部/底部\n"
"\n"
"特殊消息:\n"
" /me <action> - 发送动作 (如 /me waves)\n"
" @username - 提及用户 (响铃+高亮)\n"
"\n"
"帮助界面按键:\n"
" q, ESC - 关闭帮助\n"
" j/k - 向下/上滚动\n"
" Ctrl+D/U - 向下/上滚动半页\n"
" Ctrl+F/B - 向下/上滚动整页\n"
" g/G - 跳到顶部/底部\n"
" l - 切换界面语言\n");
} }

View file

@ -49,10 +49,25 @@ TEST(full_help_matches_language) {
assert_ascii_angle_placeholders(zh); assert_ascii_angle_placeholders(zh);
} }
TEST(full_help_falls_back_to_english) {
char text[8192] = {0};
size_t pos = 0;
help_text_append_full(text, sizeof(text), &pos, (ui_lang_t)99);
assert(strstr(text, "TNT KEY REFERENCE") != NULL);
assert(strstr(text, "AVAILABLE COMMANDS") != NULL);
assert(strstr(text, "COMMAND OUTPUT KEYS") != NULL);
assert(strstr(text, "Cycle UI language") != NULL);
assert(strstr(text, "TNT 按键参考") == NULL);
assert(strstr(text, "切换界面语言") == NULL);
}
int main(void) { int main(void) {
printf("Running help text unit tests...\n\n"); printf("Running help text unit tests...\n\n");
RUN_TEST(full_help_matches_language); RUN_TEST(full_help_matches_language);
RUN_TEST(full_help_falls_back_to_english);
printf("\n✓ All %d tests passed!\n", tests_passed); printf("\n✓ All %d tests passed!\n", tests_passed);
return 0; return 0;