繁体   English   中英

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

[英]regex for parsing resource (.rc) files

最终,我只想从 .rc 文件中提取字符串,以便我可以翻译它们,但任何与 .rc 文件相关的内容都对我有用。

如果您的程序符合 GNU 许可证,我会考虑使用gettext.PO 文件

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

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

也许这有帮助? 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+""(.*)""";

如果是 rc,最好使用像http://www.soft-gems.net/index.php/java/windows-resource-file-parser-and-converter这样的高级解析器

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM