Feature: add a language reset button and fit toolbar labels
The entry dialog showed the external language-pack folder with no way to disable it. Reset now clears the LangDir configuration value and restores a built-in language; it does not read, move or delete any file on disk. The current language is kept when already built-in (zh_CN/zh_TW/en_US); an external pack falls back to en_US. Startup treats a missing LangDir as ./lang and an explicitly empty one as built-ins only; scanner, loader, encoding check and the zh_CN patch skip disk access when the directory is empty. The button is created at runtime, sized from the localized label (German "Zuruecksetzen" fits) and matching OK/Cancel height; the folder field shrinks. Reset and the result message are translated in en_US, zh_TW and de_DE. Toolbar buttons share one width taken from the longest label. At the 1400-px startup window en_US used 76px per button and zh_TW 85px, clipping the last icons; zh_CN was 72px and de_DE 64px. Shortening the longest en_US/zh_TW labels brings both to 64px, so all 19 icons fit. Verified with a Debug x64 build: resetting a German pack leaves de_DE.ini on disk while settings become LangDir= and Language=en_US; a built-in zh_TW UI stays zh_TW; after restart with LangDir empty, only 3 built-ins are listed. English and zh_TW screenshots show all 19 toolbar buttons at startup. Co-Authored-By: deepseek-flash
This commit is contained in:
1 parent
1d03cbeb3f
commit
3d2dd08be5
6 files changed
+226
-102
No files matched your search
+162
-65
@@ -69,19 +69,21 @@ public:
|
||||
langSet.insert(_T("en_US"));
|
||||
langSet.insert(_T("zh_TW"));
|
||||
|
||||
// 2. 扫描磁盘上的语言文件
|
||||
CString searchPath = m_langDir + _T("\\*.ini");
|
||||
WIN32_FIND_DATA fd;
|
||||
HANDLE hFind = FindFirstFile(searchPath, &fd);
|
||||
if (hFind != INVALID_HANDLE_VALUE) {
|
||||
do {
|
||||
CString filename(fd.cFileName);
|
||||
int dotPos = filename.ReverseFind(_T('.'));
|
||||
if (dotPos > 0) {
|
||||
langSet.insert(filename.Left(dotPos));
|
||||
}
|
||||
} while (FindNextFile(hFind, &fd));
|
||||
FindClose(hFind);
|
||||
// 2. 扫描磁盘上的语言文件(目录为空表示只用内嵌语言)
|
||||
if (!m_langDir.IsEmpty()) {
|
||||
CString searchPath = m_langDir + _T("\\*.ini");
|
||||
WIN32_FIND_DATA fd;
|
||||
HANDLE hFind = FindFirstFile(searchPath, &fd);
|
||||
if (hFind != INVALID_HANDLE_VALUE) {
|
||||
do {
|
||||
CString filename(fd.cFileName);
|
||||
int dotPos = filename.ReverseFind(_T('.'));
|
||||
if (dotPos > 0) {
|
||||
langSet.insert(filename.Left(dotPos));
|
||||
}
|
||||
} while (FindNextFile(hFind, &fd));
|
||||
FindClose(hFind);
|
||||
}
|
||||
}
|
||||
|
||||
// 转为 vector 返回
|
||||
@@ -108,6 +110,11 @@ public:
|
||||
return true;
|
||||
}
|
||||
|
||||
if (m_langDir.IsEmpty()) {
|
||||
TRACE("[LangEnc] lang dir disabled, reject disk language\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
CString langFile = m_langDir + _T("\\") + langCode + _T(".ini");
|
||||
TRACE("[LangEnc] Checking: %s\n", (LPCSTR)langFile);
|
||||
|
||||
@@ -190,9 +197,9 @@ public:
|
||||
|
||||
// 中文模式:检查是否有补丁文件
|
||||
if (langCode == _T("zh_CN") || langCode.IsEmpty()) {
|
||||
// 尝试加载中文补丁文件(可选)
|
||||
// 尝试加载中文补丁文件(可选);目录为空时不访问磁盘
|
||||
CString patchFile = m_langDir + _T("\\zh_CN.ini");
|
||||
if (GetFileAttributes(patchFile) != INVALID_FILE_ATTRIBUTES) {
|
||||
if (!m_langDir.IsEmpty() && GetFileAttributes(patchFile) != INVALID_FILE_ATTRIBUTES) {
|
||||
CIniParser ini;
|
||||
if (ini.LoadFile((LPCSTR)patchFile)) {
|
||||
const CIniParser::TKeyVal* pSection = ini.GetSection("Strings");
|
||||
@@ -210,9 +217,9 @@ public:
|
||||
// 1. 先从内嵌资源加载(英语和繁体中文)
|
||||
bool hasBuiltin = LoadFromResource(langCode);
|
||||
|
||||
// 2. 再从磁盘文件加载(覆盖内嵌翻译)
|
||||
// 2. 再从磁盘文件加载(覆盖内嵌翻译);目录为空时只用内嵌语言
|
||||
CString langFile = m_langDir + _T("\\") + langCode + _T(".ini");
|
||||
if (GetFileAttributes(langFile) != INVALID_FILE_ATTRIBUTES) {
|
||||
if (!m_langDir.IsEmpty() && GetFileAttributes(langFile) != INVALID_FILE_ATTRIBUTES) {
|
||||
CIniParser ini;
|
||||
// 如果有内嵌资源,使用追加模式覆盖;否则使用普通加载
|
||||
if (hasBuiltin) {
|
||||
@@ -324,12 +331,19 @@ public:
|
||||
return m_currentLang;
|
||||
}
|
||||
|
||||
// 语言包目录(Init 时确定;为空表示未设置,语言对话框据此显示"内置/预设")
|
||||
// 语言包目录(Init 时确定)。空表示已显式清空:不访问外部语言包,只用内置语言;
|
||||
// 仅当配置键缺失时,调用方才回退到 "./lang"。
|
||||
const CString& GetLangDir() const
|
||||
{
|
||||
return m_langDir;
|
||||
}
|
||||
|
||||
// 清空语言包目录变量:只保留内嵌语言,不再访问磁盘语言包。
|
||||
void ClearLangDir()
|
||||
{
|
||||
m_langDir.Empty();
|
||||
}
|
||||
|
||||
// 是否为中文模式(无需翻译)
|
||||
bool IsChinese() const
|
||||
{
|
||||
@@ -678,6 +692,8 @@ class CLangSelectDlg : public CDialog
|
||||
public:
|
||||
CString m_strSelectedLang;
|
||||
CComboBox m_comboLang;
|
||||
CButton m_btnReset; // 运行时创建,避免改动动态对话框模板的控件数
|
||||
bool m_bResetToBuiltin = false; // 用户点了“重置”:清空语言包目录变量并恢复内置语言
|
||||
|
||||
CLangSelectDlg(CWnd* pParent = NULL) : CDialog(), m_pParent(pParent) {}
|
||||
|
||||
@@ -688,20 +704,23 @@ public:
|
||||
}
|
||||
|
||||
// 静态方法:显示对话框并返回选择的语言代码
|
||||
// 返回空字符串表示用户取消
|
||||
static CString Show(CWnd* pParent = NULL)
|
||||
// 返回空字符串表示用户取消;reset 非空时回传是否点了“重置”。
|
||||
static CString Show(CWnd* pParent = NULL, bool* reset = nullptr)
|
||||
{
|
||||
CLangSelectDlg dlg(pParent);
|
||||
if (dlg.DoModal() == IDOK) {
|
||||
if (reset != nullptr) *reset = dlg.m_bResetToBuiltin;
|
||||
return dlg.m_strSelectedLang;
|
||||
}
|
||||
return _T("");
|
||||
}
|
||||
|
||||
protected:
|
||||
enum { IDC_LANG_RESET_CTRL = 0x4C52 };
|
||||
CWnd* m_pParent;
|
||||
std::vector<BYTE> m_templateBuffer;
|
||||
std::vector<CString> m_langCodes;
|
||||
CRect m_rcDirEdit; // 目录框原始位置(用于重置按钮自适应布局)
|
||||
|
||||
// 语言代码到显示名称的映射
|
||||
static CString GetLanguageDisplayName(const CString& langCode)
|
||||
@@ -713,6 +732,122 @@ protected:
|
||||
return langCode;
|
||||
}
|
||||
|
||||
// 语言包目录(配置可能为相对路径,展示前解析成绝对路径)
|
||||
CString ResolveLangDir() const
|
||||
{
|
||||
CString dir = g_Lang.GetLangDir();
|
||||
if (dir.IsEmpty())
|
||||
return dir;
|
||||
TCHAR full[MAX_PATH] = { 0 };
|
||||
const DWORD n = GetFullPathName(dir, MAX_PATH, full, NULL);
|
||||
if (n > 0 && n < MAX_PATH)
|
||||
return CString(full);
|
||||
return dir; // 超长路径或解析失败时保留原值,避免指到错误目录
|
||||
}
|
||||
|
||||
// 目录框右侧动态创建“重置”按钮(模板控件数不变,避免动态模板创建失败)。
|
||||
void CreateResetButton()
|
||||
{
|
||||
CWnd* pDir = GetDlgItem(1004);
|
||||
if (pDir == nullptr)
|
||||
return;
|
||||
pDir->GetWindowRect(&m_rcDirEdit);
|
||||
ScreenToClient(&m_rcDirEdit);
|
||||
CRect btn(m_rcDirEdit.right - 60, m_rcDirEdit.top, m_rcDirEdit.right, m_rcDirEdit.bottom);
|
||||
m_btnReset.Create(_T("Reset"), WS_CHILD | WS_VISIBLE | WS_TABSTOP | BS_PUSHBUTTON,
|
||||
btn, this, IDC_LANG_RESET_CTRL);
|
||||
m_btnReset.SetFont(GetFont());
|
||||
}
|
||||
|
||||
// 按当前语言的实际文本宽度布局:右侧按钮自适应,目录框相应收窄。
|
||||
void LayoutResetButton()
|
||||
{
|
||||
CWnd* pDir = GetDlgItem(1004);
|
||||
if (pDir == nullptr || m_btnReset.GetSafeHwnd() == nullptr)
|
||||
return;
|
||||
CClientDC dc(this);
|
||||
CFont* oldFont = dc.SelectObject(GetFont());
|
||||
const CSize sz = dc.GetTextExtent(_TR("重置"));
|
||||
dc.SelectObject(oldFont);
|
||||
int btnW = sz.cx + 24;
|
||||
if (btnW < 60)
|
||||
btnW = 60;
|
||||
const int gap = 5;
|
||||
CWnd* pOk = GetDlgItem(IDOK);
|
||||
CRect okRect(0, 0, 0, 14);
|
||||
if (pOk != nullptr) {
|
||||
pOk->GetWindowRect(&okRect);
|
||||
ScreenToClient(&okRect);
|
||||
}
|
||||
CRect edit = m_rcDirEdit;
|
||||
edit.right -= (btnW + gap);
|
||||
CRect btn(edit.right + gap, 0, edit.right + gap + btnW, 0);
|
||||
const int mid = m_rcDirEdit.top + m_rcDirEdit.Height() / 2;
|
||||
btn.top = mid - okRect.Height() / 2;
|
||||
btn.bottom = btn.top + okRect.Height();
|
||||
pDir->SetWindowPos(NULL, edit.left, edit.top, edit.Width(), edit.Height(), SWP_NOZORDER);
|
||||
m_btnReset.SetWindowPos(NULL, btn.left, btn.top, btn.Width(), btn.Height(), SWP_NOZORDER);
|
||||
}
|
||||
|
||||
// 按当前语言刷新对话框自身的文案(OnInitDialog 调用)。
|
||||
void SetUiTexts()
|
||||
{
|
||||
CString strTitle = _TR("切换语言");
|
||||
WCHAR wTitle[64] = { 0 };
|
||||
MultiByteToWideChar(936, 0, strTitle, -1, wTitle, 64);
|
||||
::SetWindowTextW(m_hWnd, wTitle);
|
||||
SetDlgItemText(1002, _TR("语言:"));
|
||||
SetDlgItemText(1003, _TR("语言包目录:"));
|
||||
SetDlgItemText(IDOK, _TR("确定"));
|
||||
SetDlgItemText(IDCANCEL, _TR("取消"));
|
||||
SetDlgItemText(IDC_LANG_RESET_CTRL, _TR("重置"));
|
||||
LayoutResetButton();
|
||||
}
|
||||
|
||||
// 重新填充语言下拉框(内置语言始终在列,磁盘包按目录扫描)。
|
||||
void PopulateLanguageCombo()
|
||||
{
|
||||
m_comboLang.ResetContent();
|
||||
m_langCodes.clear();
|
||||
int idx = m_comboLang.AddString(GetLanguageDisplayName(_T("zh_CN")));
|
||||
m_langCodes.push_back(_T("zh_CN"));
|
||||
m_comboLang.SetItemData(idx, 0);
|
||||
auto langs = g_Lang.GetAvailableLanguages();
|
||||
for (const auto& lang : langs) {
|
||||
if (lang == _T("zh_CN")) continue;
|
||||
CString displayName = GetLanguageDisplayName(lang);
|
||||
idx = m_comboLang.AddString(displayName);
|
||||
m_comboLang.SetItemData(idx, m_langCodes.size());
|
||||
m_langCodes.push_back(lang);
|
||||
}
|
||||
CString current = g_Lang.GetCurrentLanguage();
|
||||
if (current.IsEmpty()) current = _T("zh_CN");
|
||||
for (size_t i = 0; i < m_langCodes.size(); ++i) {
|
||||
if (m_langCodes[i] == current) {
|
||||
m_comboLang.SetCurSel((int)i);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 重置:确认后清空语言包,结果交回调用方落盘并提示;对话框以 IDOK 关闭。
|
||||
void OnResetLanguage()
|
||||
{
|
||||
// 只清空“语言包目录”这个配置变量;磁盘上的 ini 文件不动。
|
||||
// 由调用方落盘:当前语言若已是内置语言则保留,否则回退 en_US。
|
||||
m_bResetToBuiltin = true;
|
||||
EndDialog(IDOK);
|
||||
}
|
||||
|
||||
virtual BOOL OnCommand(WPARAM wParam, LPARAM lParam) override
|
||||
{
|
||||
if (LOWORD(wParam) == IDC_LANG_RESET_CTRL && HIWORD(wParam) == BN_CLICKED) {
|
||||
OnResetLanguage();
|
||||
return TRUE;
|
||||
}
|
||||
return CDialog::OnCommand(wParam, lParam);
|
||||
}
|
||||
|
||||
LPCDLGTEMPLATE CreateDialogTemplate()
|
||||
{
|
||||
const WORD DLG_WIDTH = 260;
|
||||
@@ -810,58 +945,20 @@ protected:
|
||||
CDialog::OnInitDialog();
|
||||
|
||||
// 模板为动态构造的 Unicode 模板,MBCS 下被 ANSI 创建路径误读,标题/标签/按钮文字会乱码,
|
||||
// 故在此显式设置(走线程 locale GBK,与其它对话框同法),不再对已乱码文本调用 TranslateDialog。
|
||||
// 标题含中文:非中文系统上 ANSI 消息按系统 ACP 解码会乱码,用 _TR 取翻译(中文→切换语言,
|
||||
// 英语→Switch Language)后,显式 GBK(936)→Unicode 转换再用 Unicode 消息设置(与工具栏同法)。
|
||||
CString strTitle = _TR("切换语言");
|
||||
WCHAR wTitle[64] = { 0 };
|
||||
MultiByteToWideChar(936, 0, strTitle, -1, wTitle, 64);
|
||||
::SetWindowTextW(m_hWnd, wTitle);
|
||||
SetDlgItemText(1002, _TR("语言:"));
|
||||
SetDlgItemText(IDOK, _TR("确定"));
|
||||
SetDlgItemText(IDCANCEL, _TR("取消"));
|
||||
// 故由 SetUiTexts 显式设置(标题按 936→Unicode,其余走当前语言)。
|
||||
CreateResetButton();
|
||||
SetUiTexts();
|
||||
|
||||
// 语言包目录(只读展示):目录不存在时说明只有内嵌语言可用。
|
||||
// 语言包目录(只读展示):目录为空/不存在时显示“内置/预设”。
|
||||
// 路径可能较长,控件带 ES_AUTOHSCROLL,可横向滚动并选中复制。
|
||||
SetDlgItemText(1003, _TR("语言包目录:"));
|
||||
CString langDir = g_Lang.GetLangDir();
|
||||
CString showDir;
|
||||
if (!langDir.IsEmpty() && GetFileAttributes(langDir) != INVALID_FILE_ATTRIBUTES) {
|
||||
// 配置里的默认值是相对路径 "./lang",展示时解析成绝对路径,避免看不出实际位置
|
||||
TCHAR full[MAX_PATH] = { 0 };
|
||||
if (GetFullPathName(langDir, MAX_PATH, full, NULL) > 0)
|
||||
showDir = full;
|
||||
else
|
||||
showDir = langDir;
|
||||
}
|
||||
if (!langDir.IsEmpty() && GetFileAttributes(langDir) != INVALID_FILE_ATTRIBUTES)
|
||||
showDir = ResolveLangDir();
|
||||
SetDlgItemText(1004, showDir.IsEmpty() ? _TR("内置/预设") : showDir);
|
||||
|
||||
m_comboLang.SubclassDlgItem(1001, this);
|
||||
|
||||
// 添加简体中文(显示为英语避免乱码)
|
||||
int idx = m_comboLang.AddString(GetLanguageDisplayName(_T("zh_CN")));
|
||||
m_langCodes.push_back(_T("zh_CN"));
|
||||
m_comboLang.SetItemData(idx, 0);
|
||||
|
||||
// 添加其他语言
|
||||
auto langs = g_Lang.GetAvailableLanguages();
|
||||
for (const auto& lang : langs) {
|
||||
if (lang == _T("zh_CN")) continue;
|
||||
CString displayName = GetLanguageDisplayName(lang);
|
||||
idx = m_comboLang.AddString(displayName);
|
||||
m_comboLang.SetItemData(idx, m_langCodes.size());
|
||||
m_langCodes.push_back(lang);
|
||||
}
|
||||
|
||||
// 选中当前语言
|
||||
CString currentLang = g_Lang.GetCurrentLanguage();
|
||||
if (currentLang.IsEmpty()) currentLang = _T("zh_CN");
|
||||
for (size_t i = 0; i < m_langCodes.size(); i++) {
|
||||
if (m_langCodes[i] == currentLang) {
|
||||
m_comboLang.SetCurSel((int)i);
|
||||
break;
|
||||
}
|
||||
}
|
||||
PopulateLanguageCombo();
|
||||
|
||||
CenterWindow();
|
||||
return TRUE;
|
||||
|
||||
Reference in new issue
Block a user