2008-09-10 80 views
1

Ulrichly我只是想從.rc文件中提取字符串,所以我可以翻譯它們,但任何與.rc文件一起使用的東西都適用於我。解析資源(.rc)文件的正則表達式

+0

我沒有任何的.rc文件,可以:通過gettext和每個對話使用這樣的功能轉換在啓動的字符串PO文件你給我一個樣本,以便我可以爲它創建一個正則表達式? – Teifion 2008-09-10 08:37:01

回答

0

這聽起來像是一個SED腳本的工作。

運行以下命令行:sed.exe -n -f sed.txt test.rc

以下SED腳本將提取所有引用的字符串從輸入test.rc文件:

# Run Script Using This Command Line 
# 
# sed.exe -n -f sed.txt test.rc 
# 

# Check for lines that contain strings 
/\".*\"/ { 
    # print the string part of the line only 
    s/\(.*\)\(\".*\"\)\(.*\)/\2/ p 
} 
1

對於真實世界的項目,你會想,不僅提取它們也將它們放回,並跟蹤更新。除了最微不足道的應用程序之外,這對於任何事情都是難以管理的。看看像appTranslator(www.apptranslator.com)這樣的專業軟件。不是免費的,但一些正則表達式解析不會削減它 - 我知道,我已經嘗試了很多。

1

儘管rc文件似乎是一個明顯的翻譯起點,但事實並非如此。 開發人員的工作是確保應用程序是可翻譯的。這不是管理翻譯。從exe開始翻譯,雖然有點反直覺,但這是一個更好的主意。 閱讀更多關於它的地方:http://www.apptranslator.com/misconceptions.html

-2

ResxCrunch有時會很快出來。 它將在一張表中編輯多種語言的多個資源文件。

1

我會考慮的gettext.PO files使用,如果你的程序符合GNU許可證

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(串),你應該考慮字符「被表示爲‘’,並且也有字符像\ 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)然後在程序啓動時,您應該加載。

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