简体   繁体   English

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

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

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

I'd consider usage of gettext and .PO files , if your program fits GNU license如果您的程序符合 GNU 许可证,我会考虑使用gettext.PO 文件

1) I'd suggest extracting from .rc files using state machine algorithm. 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) When extracting string inside ExtractString() you should consider that char " is represented as "", and there are also chars like \\t \\n \\r. So state machine is also a good option here. 2)在ExtractString()中提取字符串时,应该考虑char "表示为"",还有\\t \\n \\r这样的字符,所以状态机在这里也是一个不错的选择。

The following string:以下字符串:

LTEXT           "Mother has washed ""Sony"", then \taquarium\\shelves\r\nand probably floors",IDC_TEXT1,24,14,224,19

represents such label on a dialog:在对话框上表示这样的标签:

Mother has washed "Sony", then aquarium\shelves
and probably floors

3) Then on program startup you should load .po file via gettext and for each dialog translate its string on startup using a function like this: 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;
}

Although rc files seems an obvious starting point for translation, it's not.尽管 rc 文件似乎是翻译的明显起点,但事实并非如此。 The job of developers is to make sure the app is translatable.开发人员的工作是确保应用程序是可翻译的。 It's not to manage translations.这不是管理翻译。 Starting translations from the exe, although somewhat counter-intuitive, is a way better idea.从 exe 开始翻译,虽然有点违反直觉,但却是一个更好的主意。 Read more about it here: http://www.apptranslator.com/misconceptions.html在此处阅读更多相关信息: http : //www.apptranslator.com/misconceptions.html

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

They are using the following regex to find the stringtable in the rc source:他们使用以下正则表达式在 rc 源代码中查找字符串表:

(?<=\\bSTRINGTABLE\\s+BEGIN\\s+).*?(?=\\s+END\\b) (?<=\\bSTRINGTABLE\\s+BEGIN\\s+).*?(?=\\s+END\\b)

Edit - And you can read the key values pairs with the following statement with the MultiLine option:编辑 - 您可以使用 MultiLine 选项使用以下语句读取键值对:

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

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

This sounds like a job for a SED script.这听起来像是SED脚本的工作。

By running this command line: sed.exe -n -f sed.txt test.rc通过运行此命令行: sed.exe -n -f sed.txt test.rc

The following SED script will extract all the quoted strings from the input test.rc file:以下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 will be out sometimes soon. ResxCrunch有时很快就会发布。 It will edit multiple resource files in multiple languages in one single table.它将在一个表中编辑多种语言的多个资源文件。

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

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