How does a Parent Form can get its own Strings?

All announcements, questions and issues related to the TsiLang Components Suite.
Post Reply
Jean-Paul Brassard
Posts: 65
Joined: Thu May 08, 2008 7:46 pm

How does a Parent Form can get its own Strings?

Post by Jean-Paul Brassard »

I have a hierarchy of inherited Forms for which only the lowest Child is instanciated.
When the Child uses a function of its parent, we get wrong strings.
The Parent calls to siLangRT1.GetTextOrDefault('IDS_0'...);, returns the Child's translation for IDS_0, not the Parent's one.

For example:

Code: Select all

  TParentForm(TForm); 
    function ParentString: String;
      begin 
        Result := siLangRT1.GetTextOrDefault('IDS_0' (*Parent*));
      end;
      ...
  TChildForm(TParentForm); ...
    procedure FormShow;
      Caption := ParentString + ' > ' +
                 siLangRT1.GetTextOrDefault('IDS_0' (*Child*));
    ...
When Child's "FormShow" executes, its Caption gets the value
'Child > Child'
instead of
'Parent > Child'
This is because the Parent's function "ParentString" gets the Child's collection of strings instead of its own collection of strings!

Does the Parent's collection of strings available at Run Time, even if only the Child has been instanciated???

Thanks
Jean-Paul Brassard
Quebec, Canada
isiticov
Site Admin
Posts: 2383
Joined: Thu Nov 21, 2002 3:17 pm

Post by isiticov »

No, Parent's strings are not available at run-time. When you instantiate child form only its resources (DFM) is available for Delphi as well as for TsiLang. This is why you get only child's strings. If you use inheritance and wish toi re-use strings from parent just be sure don't override their values in child and then use new IDs in child forms for new strings.
Best regards,
Igor Siticov.
Jean-Paul Brassard
Posts: 65
Joined: Thu May 08, 2008 7:46 pm

How to access Parent's strings

Post by Jean-Paul Brassard »

isiticov wrote:No, Parent's strings are not available at run-time. When you instantiate child form only its resources (DFM) is available for Delphi as well as for TsiLang. This is why you get only child's strings. If you use inheritance and wish to re-use strings from parent just be sure don't override their values in child and then use new IDs in child forms for new strings.
I did succeed with the following method. However, I have to explore to see if this could cause any problem. Like setting the Internal variable to nil on Form Destroy.

Standard Parent code which does not work, after adding TsiLang, when run from its Children:

Code: Select all

var
  Parent_Form: TParent_Form;

implementation
  function ParentString: String; 
    begin 
      Result := siLangRT1.GetTextOrDefault('IDS_0' (*Parent*)); 
    end; 
Potential solution to help fixing this problem:

Code: Select all

type
  function Parent_Form: TParent_Form;

implementation
var
  InternalParent_Form: TParent_Form;

  function Parent_Form: TParent_Form;
  begin
    if NOT assigned(InternalParent_Form) then
      InternalParent_Form := TParent_Form.Create(Application);
    Result := InternalParent_Form;
  end;

  function ParentString: String; 
    begin 
      Result := Parent_Form.siLangRT1.GetTextOrDefault('IDS_0' (*Parent*)); 
    end; 
Jean-Paul Brassard
Quebec, Canada
Jean-Paul Brassard
Posts: 65
Joined: Thu May 08, 2008 7:46 pm

Re: How to access Parent's strings

Post by Jean-Paul Brassard »

Please forgot my previous "potential solution". Here is new info about that problem.
isiticov wrote:No, Parent's strings are not available at run-time. When you instantiate child form only its resources (DFM) is available for Delphi as well as for TsiLang...
Update: The Parent's StringsCollection is not available at run time, but its strings ARE available through Child's StringsCollection. Here is how it works. When a Child is instantiated, Delphi propagates the FormCreate to all its Parents. While the parents are read by Delphi, TsiLang captures all their strings and adds them to the Child's StringsCollection.

Then, when the Child runs some Parent's code, the later gets its own strings back from its call to siLangRT1.GetTextOrDefault('IDS_27').

N.B. There are some restrictions.
1) The Parents must refer to the Child's siLangRT component, not its own one (which is nil). In other word, do NOT add the Form Name to the calls to siLangRT.
2) The Parent's strings IDs must differs from those of all its Children.
Jean-Paul Brassard
Quebec, Canada
Jean-Paul Brassard
Posts: 65
Joined: Thu May 08, 2008 7:46 pm

Re: How to access Parent's strings

Post by Jean-Paul Brassard »

Jean-Paul Brassard wrote:Update: The Parent's StringsCollection is not available at run time, but its strings ARE available through Child's StringsCollection...
Oups! Does not work when the Strings are read from an SIL or SIB file.
It is only OK whsn the strings are read from the DFM.
Jean-Paul Brassard
Quebec, Canada
isiticov
Site Admin
Posts: 2383
Joined: Thu Nov 21, 2002 3:17 pm

Post by isiticov »

If you load translations from file it must contain all strings (parent's and child's) in child form section. Then they will be loaded into TsiLang when you load external file.
Best regards,
Igor Siticov.
Jean-Paul Brassard
Posts: 65
Joined: Thu May 08, 2008 7:46 pm

How to get Parent's Strings when its Child is instanciated

Post by Jean-Paul Brassard »

isiticov wrote:If you load translations from file it must contain all strings (parent's and child's) in child form section. Then they will be loaded into TsiLang when you load external file.
I coded the new utility "AddParentStrFromSIB" which reads the Parent's strings from the SIB file, into its Child's StringCollection. This way, when the Child uses one of its Parent's property, the Parent strings are there and translated.

I let TsiLang publish my solution, if its is OK with their copywrights.
Jean-Paul Brassard
Quebec, Canada
Post Reply