简体   繁体   English

Delphi如何从resourcestring单元中找到资源名称

[英]Delphi How can i find a resource name from resourcestring unit

The situation is: 情况是:

unit abc;

interface

resourcestring
  aabbcc = 'my text here';

implementation

end.

From my application I received error code 'aabbcc'. 从我的应用程序中,我收到了错误代码“ aabbcc”。 Is possible to access with error code the resource name aabbcc ? 可以使用错误代码访问资源名称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');

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. 因此,没有内置方法来获取文本'aabbcc'并发现与之关联的字符串资源。

However, there is a way to take the Delphi resourcestring identifier in your code and discover its numeric ID. 然而, 一种方法采取德尔福resourcestring在你的代码标识,并发现它的数字ID。 Type-cast the expression @aabbcc to PResStringRec , and then read its Identifier field. 将表达式@aabbcc类型@aabbccPResStringRec ,然后读取其Identifier字段。 Look at LoadResString in System.pas to see how the RTL uses this information. LoadResStringSystem.pas看到RTL如何使用这些信息。

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. 您可以使用PResStringRec值在运行时构建TDictionary<string, PResStringRec> ,然后在实现上述假设的GetNamedResourceString函数时使用该字典。

NamedResources := TDictionary<string, PResStringRec>.Create;
NamedResources.Add('aabbcc', PResStringRec(@aabbcc));

function GetNamedResourceString(const Name: string): string;
begin
  Result := LoadResString(NamedResources[Name]);
end;

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. 如果我是在实际项目中执行此操作,则可能会使用预构建脚本来解析输入文件,以自动从相应的resourcestring NamedResources.Add声明中生成对NamedResources.Add的调用。

Your resourcestring is compiled into a string table resource. 您的resourcestring被编译为字符串表资源。 These are identified by a numeric identifier. 这些由数字标识符标识。 The compiler maintains a map between your declared resourcestring instances, and the numeric identifiers. 编译器维护声明的resourcestring字符串实例与数字标识符之间的映射。 When you access a resourcestring the compiler knows the numeric identifier, and emits code that uses that identifier. 当您访问resourcestring字符串时,编译器会知道数字标识符,并发出使用该标识符的代码。 Essentially what you are hoping to do is to be able to map from the name of your resourcestring and the numeric identifier. 本质上,您希望执行的操作是能够从您的resourcestring字符串的名称和数字标识符进行映射。 Unfortunately that map only exists during compilation. 不幸的是,该映射仅在编译期间存在。 It is not contained in the executable. 它不包含在可执行文件中。

Your only other hope would be for the compiler to generate RTTI information for resource strings. 您唯一的希望是编译器为资源字符串生成RTTI信息。 However, it does not do this, unless I am very much mistaken. 但是,除非我非常误解,否则它不会这样做。

Given these constraints you are going to need to come up with your own mechanism to map between names and resource strings. 考虑到这些限制,您将需要提出自己的机制来在名称和资源字符串之间进行映射。 One possibility is to avoid using the built-in resourcestring , and manage the string table resources, and their identifiers, by your own mechanisms. 一种可能性是避免使用内置的resourcestring ,并通过自己的机制来管理字符串表资源及其标识符。

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

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