Delphi如何从resourcestring单元中找到资源名称 [英] Delphi How can i find a resource name from resourcestring unit
问题描述
情况是:
unit abc;
interface
resourcestring
aabbcc = 'my text here';
implementation
end.
从我的应用程序中,我收到了错误代码 aabbcc。可以使用错误代码访问资源名称aabbcc吗?
From my application I received error code 'aabbcc'. Is possible to access with error code the resource name aabbcc ?
推荐答案
我想您要的是带有以下签名的函数行为:
I imagine you're asking for a function with the following signature and behavior:
function GetNamedResourceString(const Name: string): string;
Assert(GetNamedResourceString('aabbcc') = 'my text here');
资源字符串存储在程序中的字符串表资源中,其中每个字符串都有一个数字索引。您在程序中使用的命名标识符在运行时不会保留在任何地方。因此,没有内置的方法来获取文本'aabbcc'
并发现与之关联的字符串资源。
Resource strings are stored in your program in a string-table resource, where each string has a numeric index. The named identifier you use in your program is not kept anywhere at run time. Therefore, there is no built-in way to take the text 'aabbcc'
and discover which string resource it's associated with.
但是,有一种 方法可以在代码中获取Delphi 资源字符串
标识符并发现其数字ID。将表达式 @aabbcc
键入到 PResStringRec
,然后读取其标识符
字段。在 LoadResString
中 System.pas 来查看RTL如何使用此信息。
However, there is a way to take the Delphi resourcestring
identifier in your code and discover its numeric ID. Type-cast the expression @aabbcc
to PResStringRec
, and then read its Identifier
field. Look at LoadResString
in System.pas to see how the RTL uses this information.
您可以使用 PResStringRec
值来构建 TDictionary< string,PResStringRec> ;
,然后使用该字典来实现上面概述的假设的 GetNamedResourceString
函数。
You could use the PResStringRec
values to build a TDictionary<string, PResStringRec>
at run time, and then use that dictionary in implementing the hypothetical GetNamedResourceString
function outlined above.
NamedResources := TDictionary<string, PResStringRec>.Create;
NamedResources.Add('aabbcc', PResStringRec(@aabbcc));
function GetNamedResourceString(const Name: string): string;
begin
Result := LoadResString(NamedResources[Name]);
end;
如果我在一个真实的项目中这样做,我可能会使用预构建脚本来解析输入文件以自动生成对 NamedResources.Add
的调用,该调用来自相应的 resourcestring
声明。
If I were doing this for a real project, I would probably use a pre-build script to parse an input file to automatically generate the calls to NamedResources.Add
from the corresponding resourcestring
declarations.
这篇关于Delphi如何从resourcestring单元中找到资源名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!