如果您的程序符合 GNU 许可证,我会考虑使用 gettext 和 .PO files
1) 我建议使用状态机算法从 .rc 文件中提取。
void ProcessLine(const char * str)
{
if (strstr(str, " DIALOG"))
state = Scan;
else if (strstr(str, " MENU"))
state = Scan;
else if (strstr(str, " STRINGTABLE"))
state = Scan;
else if (strstr(str, "END"))
state = DontScan;
if (state == Scan)
{
const char * cur = sLine;
string hdr = ...// for example "# file.rc:453"
string msgid;
string msgid = "";
while (ExtractString(sLine, cur, msgid))
{
if (msgid.empty())
continue;
if (IsPredefined(msgid))
continue;
if (msgid.find("IDB_") == 0 || msgid.find("IDC_") == 0)
continue;
WritePoString(hdr, msgid, msgstr);
}
}
}
2) 在ExtractString()中提取字符串的时候要考虑char "表示为"",还有像\t \n \r这样的字符,所以状态机在这里也是一个不错的选择。
以下字符串:
LTEXT "Mother has washed ""Sony"", then \taquarium\\shelves\r\nand probably floors",IDC_TEXT1,24,14,224,19
在对话框上表示这样的标签:
Mother has washed "Sony", then aquarium\shelves
and probably floors
3) 然后在程序启动时,您应该通过 gettext 加载 .po 文件,并为每个对话框在启动时使用如下函数翻译其字符串:
int TranslateDialog(CWnd& wnd)
{
int i = 0;
CWnd *pChild;
CString text;
//Translate Title
wnd.GetWindowText(text);
LPCTSTR translation = Translate(text);
window.SetWindowText(translation);
//Translate child windows
pChild=wnd.GetWindow(GW_CHILD);
while(pChild)
{
i++;
Child->GetWindowText(Text);//including NULL
translation = Translate(Text);
pChild->SetWindowText(translation);
pChild = pChild->GetWindow(GW_HWNDNEXT);
}
return i;
}