【问题标题】:regex for parsing resource (.rc) files用于解析资源 (.rc) 文件的正则表达式
【发布时间】:2010-09-08 09:22:43
【问题描述】:

最后我只是想从 .rc 文件中提取字符串以便我可以翻译它们,但是任何与 .rc 文件相关的东西都适合我。

【问题讨论】:

  • 我没有任何 .rc 文件,你能给我一个示例,以便我可以为它创建一个正则表达式吗?
  • 也许是RC-WinTrans Software Localizer ?免责声明:我与他们无关,但在我工作的公司中,人们使用旧版本进行翻译。

标签: c++ regex mfc internationalization


【解决方案1】:

【讨论】:

    【解决方案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这样的字符,所以状态机在这里也是一个不错的选择。

    以下字符串:

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

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        【解决方案4】:

        这听起来像是 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
        }
        

        【讨论】:

          【解决方案5】:

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

          【讨论】:

            【解决方案6】:

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

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

            (?

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

            @"\s+(.*?)\s+""(.*)""";

            【讨论】:

              猜你喜欢
              • 2010-12-13
              • 1970-01-01
              • 2022-01-25
              • 1970-01-01
              • 2013-12-31
              • 2012-07-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多