delphiinternationalizationdelphi-xe2resourcestring

How to get the string table resource id of a Delphi resourcestring?


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.


Solution

  • 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.