【问题标题】:How to get the string table resource id of a Delphi resourcestring?如何获取Delphi资源字符串的字符串表资源ID?
【发布时间】:2023-04-21 10:26:01
【问题描述】:

在 Delphi 中,您可以声明要存储在模块资源部分的字符串表中的字符串。

resourcestring
  rsExample = 'Example';

在编译时,Delphi 会为其分配一个 ID 并将其存储在字符串表中。

有没有办法检索声明为资源字符串的字符串的 ID?

原因是我使用了一个像 gnugettext 一样工作的包。 System.pas 中的某些函数(如 LoadResString)是挂钩的,因此当我在表达式中使用资源字符串时,它将被不同的字符串(翻译)替换。当然,这很方便,但有时我需要资源字符串的原始(未翻译)文本。

当我能够检索到字符串的资源 id 时,我可以调用 LoadString API 来获取原始文本,而不是翻译文本。

【问题讨论】:

    标签: delphi internationalization delphi-xe2 resourcestring


    【解决方案1】:

    要获取资源字符串的资源 id,可以将字符串的地址转换为 PResStringRec 类型,然后访问 Identifier 值。

    试试这个示例

    {$APPTYPE CONSOLE}
    
    {$R *.res}
    
    uses
      System.SysUtils;
    
    resourcestring
      rsExample  = 'Example';
    begin
      try
        Writeln(rsExample);
        Writeln(PResStringRec(@rsExample)^.Identifier);
      except
        on E: Exception do
          Writeln(E.ClassName, ': ', E.Message);
      end;
      readln;
    end.
    

    【讨论】:

    • 啊,谢谢!我不知道资源字符串实际上是指向 TResStringRec 记录的指针。我在文档中的任何地方都找不到。当然,这可能是我的错,但你认为这在某处有记录吗?
    • TResStringRec 文档说 ...TResStringRec represents a string resource. TResStringRec is a structure that contains the module and the identifier of a string resource.
    • 我在TResStringRec 文档中看不到声明为resourcestring 的字符串的链接。它说Variables of type TResStringRec are used by the System routines to locate and load string resources at run time. 也许它在这句话中谈到了资源字符串,但这对我来说并不清楚。不管怎样,你已经把我送到了正确的方向!
    最近更新 更多