6

In Delphi you can declare strings to be stored in the String Table of the module's resource section.

resourcestring
  rsExample = 'Example';

At compile-time, Delphi will assigns an ID for it and stores it in the String Table.

Is there a way to retrieve the ID of a string that is declared as a resourcestring?

The reason is that I use a package that works just like gnugettext. Some functions in System.pas (like LoadResString) are hooked, so when I use a resourcestring in an expression, it will be replaced by a different string (the translation). Of course, this is very handy, but sometimes I need the original (untranslated) text of the resourcestring.

When I am able to retrieve the resource id of the string, I can call the LoadString API to get the original text, instead of the translated text.

RRUZ
  • 130,998
  • 15
  • 341
  • 467
R. Beiboer
  • 692
  • 7
  • 20

1 Answers1

10

To get the resource id of a resourcestring, you can cast the address of the string to the PResStringRec type then access the Identifier value.

Try this sample

{$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.
RRUZ
  • 130,998
  • 15
  • 341
  • 467
  • Ah thank you! I didn't know the resourcestring was actually a pointer to a TResStringRec record. I could not find that anywhere in the documentation. Of course, that could be my fault, but do you think this is documented somewhere? – R. Beiboer Jul 16 '13 at 18:26
  • The [`TResStringRec`](http://docwiki.embarcadero.com/Libraries/XE4/en/System.TResStringRec) documentation says `...TResStringRec represents a string resource. TResStringRec is a structure that contains the module and the identifier of a string resource. ` – RRUZ Jul 16 '13 at 18:37
  • I do not see the link to strings declared as `resourcestring` in the [TResStringRec](http://docwiki.embarcadero.com/Libraries/XE4/en/System.TResStringRec) documentation. It says `Variables of type TResStringRec are used by the System routines to locate and load string resources at run time.` Maybe it speaks about resourcestrings in this sentence, but that was not clear to me. Anyway, you have send me to the right direction! – R. Beiboer Jul 16 '13 at 19:12