用于解析资源(.rc)文件的正则表达式

时间:2008-09-10 08:10:11

标签: c++ regex mfc internationalization

我只是想从.rc文件中提取字符串,以便我可以翻译它们,但是.rc文件的任何内容都适用于我。

6 个答案:

答案 0 :(得分:2)

如果您的程序符合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 \ n之类的字符。所以状态机在这里也是一个不错的选择。

以下字符串:

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

答案 1 :(得分:1)

也许这会有帮助吗? (http://social.msdn.microsoft.com/forums/en-US/regexp/thread/5e87fce9-ec73-42eb-b2eb-c821e95e0d31/

他们使用以下正则表达式来查找rc源代码中的字符串表:

(小于?= \ bSTRINGTABLE \ S + BEGIN \ S +)*(= \ S + END \ B')

。?

编辑 - 您可以使用MultiLine选项使用以下语句读取键值对:

@ “\ S +(*?)\ S +” “” “”(。*);

答案 2 :(得分:1)

虽然rc文件似乎是翻译的明显起点,但事实并非如此。 开发人员的工作是确保应用程序可以翻译。这不是管理翻译。从exe开始翻译虽然有点违反直觉,但却是一种更好的想法。 在此处阅读更多相关信息:http://www.apptranslator.com/misconceptions.html

答案 3 :(得分: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
}

答案 4 :(得分:0)

如果是rc,最好使用像http://www.soft-gems.net/index.php/java/windows-resource-file-parser-and-converter

这样的高级解析器

答案 5 :(得分:-2)

ResxCrunch有时很快会出局。 它将在一个表中编辑多种语言的多个资源文件。