code templates for automatisation

My project is supposed to be used in different countries, so I need to make sure it can be translated.
This requires heavy use of resource strings. you can take a look at the documentation:
https://wiki.freepascal.org/Step-by-step_instructions_for_creating_multi-language_applications
so I declare the string like this: rsTotal = ‘Total’;
Then I will add this to a stringlist, where I can use a “key value pair”
rsStrings.Append(‘rsTotal=’+rsTotal);
This typing is tedious, so I have created a code template:

All need now os set the caption on the label to rsTotal, and copy it to the clipboard.
Now typing rs^j , will do the typing for me.

Using these procedures in the onCreate event will then translate everything on the form for me:

procedure TransCaption(MyForm: TForm; rsStrings: TStringList);
var
  i: integer;
begin
  MyForm.Caption:=chgCap(Myform.Caption,rsStrings);
  for i := 0 to Pred(Myform.ComponentCount) do
  begin
    if MyForm.Components[i] is TLabel then
    (Myform.Components[i] as TLabel).Caption := chgCap((Myform.Components[i] as TLabel).Caption,rsStrings);

    if MyForm.Components[i] is TAction then
       (Myform.Components[i] as TAction).Caption := chgCap((Myform.Components[i] as TAction).Caption,rsStrings);

    if MyForm.Components[i] is TMenuItem then
    (Myform.Components[i] as TMenuItem).Caption := chgCap((Myform.Components[i] as TMenuItem).Caption,rsStrings);

    if MyForm.Components[i] is TButton then
      (Myform.Components[i] as TButton).Caption := chgCap((Myform.Components[i] as TButton).Caption,rsStrings);


    if MyForm.Components[i] is TRadioGroup then
      (Myform.Components[i] as TRadioGroup).Caption := chgCap((Myform.Components[i] as TRadioGroup).Caption,rsStrings);

    if MyForm.Components[i] is TTabsheet then
      (Myform.Components[i] as TTabsheet).Caption := chgCap((Myform.Components[i] as TTabsheet).Caption,rsStrings);

  end;
end;

Function ChgCap(MyCap: TCaption; rsStrings: TStringList):TCaption;
begin
  if Copy(MyCap, 1, 2) = 'rs' then
    MyCap := rsStrings.values[MyCap];
  Result := MyCap;
end;

This entry was posted in Uncategorized and tagged , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *