Recent

Author Topic: [Solved] Modify resourcestring at runtime  (Read 5999 times)

tk

  • Sr. Member
  • ****
  • Posts: 388
[Solved] Modify resourcestring at runtime
« on: February 17, 2014, 08:01:17 pm »
Hello,
I need a counterpart for Delphi feature which allows to set resourcestring value at runtime.
Delphi has the LoadResString function which supports the modified resourcestring value (TResStringRec record).
I am not able to find corresponding solution for Lazarus.

The reason is I need to override resourcestring values at runtime.
Any idea?

TK
« Last Edit: February 19, 2014, 10:00:05 am by tk »

JD

  • Hero Member
  • *****
  • Posts: 1916
Re: Modify resourcestring at runtime
« Reply #1 on: February 18, 2014, 11:42:02 am »
Hi TK,

See this wiki.freepascal.org/Using_resourcestrings

By the way thanks a million for releasing a new version of your Kcontrols suite. As far as I'm concerned KGrid is the best Lazarus grid I know. I use it extensively. However could you please update the documentation or add some more examples demonstrating its usage.

JD
Linux Mint - Lazarus 4.8/FPC 3.2.2,
Windows - Lazarus 4.8/FPC 3.2.2

mORMot 2, PostgreSQL & MariaDB.

tk

  • Sr. Member
  • ****
  • Posts: 388
Re: Modify resourcestring at runtime
« Reply #2 on: February 18, 2014, 04:44:17 pm »
Hi,
I know about that general information but it does not solve my problem.
I tried the following:

Code: [Select]
type
  PResStringRec = ^TResStringRec;
  TResStringRec = record
    DefStr: string;
    NewStr: string;
  end;

function ResModIterator(Name, Value: AnsiString; Hash: Longint; arg:pointer): AnsiString;
begin
  if Value = PResStringRec(arg).DefStr then
    Result := PResStringRec(arg).NewStr
  else
    Result := '';
end;

procedure ResMod(const Res: string; const NewValue: string);
var
  Rec: TResStringRec;
begin
  if Res <> '' then
  begin
    Rec.DefStr := Res;
    Rec.NewStr := NewValue;
    SetResourceStrings(ResModIterator, @Rec);
  end;
end;

//calling in code:

resourcestring
  sResString = 'test string';

begin
  ModRes(sResString, 'new test string');
end;


Calling ResMod seems to modify the resourcestring - line "Result := PResStringRec(arg).NewStr"
is called. But later in program I still see the default text...

Because of KGrid we started a new project where we need some other features as TTreeColumn so there will be
most certainly further development.

TK
« Last Edit: February 18, 2014, 05:13:52 pm by tk »

Bart

  • Hero Member
  • *****
  • Posts: 5757
    • Bart en Mariska's Webstek
Re: Modify resourcestring at runtime
« Reply #3 on: February 18, 2014, 11:02:51 pm »
Can't you use the translations unit?

Bart

tk

  • Sr. Member
  • ****
  • Posts: 388
Re: Modify resourcestring at runtime
« Reply #4 on: February 19, 2014, 09:59:10 am »
Thank you for hint, the translations unit uses the same interface from objpas.pas.
The above works for me now, because yesterday I unfortunately tested this with a published string property which was overriden back to the default state from the LFM file.

TK

 

TinyPortal © 2005-2018