Page 1 of 1

Importing Multiples SIL(SIB) Files at runtime (DELPHI)

Posted: Fri Dec 14, 2007 12:42 pm
by Pedro.Hollanda
Hi everybody,
my name is Pedro de Hollanda, im from Brazil, we use the TsiLang Components Suite here (in my work), and i had search for a method to use Multiples Sil files at runtime, for a separated translation of forms and files, but i dont have any sucess to find this, than me and one friend made a code to do this! And now i will put this here to help who has the same problem than i (that why i register myself in this forum).
The code is a simple Procedure, that merge all sil files into one sil file, it search in a directory (.\Langs) for sil files and get values from one by one putting in one separated and full sil file, at runtime and is basicly the first thing that have to do at before all the creating of forms. The procedure creates an INI File to manage the modifications ("MODIFICACOES" in the INI File) of the Sil files, so this is for help to not merge the sil files to one ever the program runs. Well and then here go the code (it is tested on Delphi 5, but i think will run on all delphi versions):

Code: Select all

{ An example of execution of the Procedure:
...

var
    SilFile : String;

...

begin

...

  SilFile:= GetCurrentDir + '\' + 'si' +
               StringReplace(ExtractFileName(Application.ExeName), '.exe','',[]) +
               'Lang.sil';

  ProcessSilFiles(GetCurrentDir+'\Langs', SilFile);

  siLangDispatcher1.FileName := SilFile;
  siLangDispatcher1.LoadAllFromFile(siLangDispatcher1.FileName);

...  
   }

procedure ProcessSilFiles(aSerachDirectory: TFilename; const aFinalSILFile: string; ExecuteCompleteMerge: Boolean = false);
  procedure SplitKeyValue(const ValueSection: String; var aKey, aValue: String);
  var equal: integer;
  begin
       equal:= pos('=',ValueSection);
       aKey:= copy(ValueSection,1,equal-1);
       aValue:= copy(ValueSection,equal+1,Length(ValueSection));
  end;
var
    sr: TSearchRec;
    DetectedSections, CurrentSection: TStringList;
    CurrentSil, FinalSil, LangINI: TINIFile;
    i: Word;
    j: cardinal;
    Key, Value: String;
    CanMakeMerge: Boolean;
    LastModification: integer;
    Splash: TForm;
    PanelSplash: TPanel;
begin
    CanMakeMerge:= (not FileExists(aFinalSILFile) ) or ExecuteCompleteMerge
                    or (not FileExists(GetCurrentDir+'\Langs.ini'));
    try
      ChDir(aSerachDirectory);
      Splash:= TForm.Create(Application);
      with Splash do
      begin
        Width:= 200;
        Height:=100;
        BorderStyle:=bsnone;
        Position:= poScreenCenter;
        PanelSplash:= TPanel.Create(Splash);
        PanelSplash.Parent:= Splash;
        PanelSPlash.Align:=AlClient;
        PanelSplash.Caption:='Inicializando... / Initializing...';
        PanelSplash.Font.Style:= [fsbold];
        PanelSplash.Font.Size:=9;
        PanelSPlash.BevelInner:= bvRaised;
        PanelSplash.BevelOuter:= bvLowered;
        Screen.Cursor:= crAppStart;
        Show;
      end;
      if FindFirst(aSerachDirectory+'\*.sil', faAnyFile,  sr) = 0 then
      begin
        FinalSil := TINIFile.Create(ExpandFileName(aFinalSILFile));
        LangINI := TINIFile.Create(ExtractFilePath(aFinalSILFile)+'\Langs.ini');
        Repeat
          LastModification:= LangINI.ReadInteger('MODIFICACOES',sr.Name,0);
          if CanMakeMerge or (LastModification < sr.Time) then
          try
            LangINI.WriteInteger('MODIFICACOES',sr.name,sr.time);
            CurrentSil := TINIFile.Create(expandFileName(sr.Name));
            DetectedSections := TStringList.Create;
            CurrentSil.ReadSections(DetectedSections);
            if DetectedSections.count > 0 then
              for i:=0 to pred(DetectedSections.Count) do
              try
                CurrentSection := TStringList.Create;
                CurrentSil.ReadSectionValues(DetectedSections[i], CurrentSection);
                if CurrentSection.count > 0 then
                  for j:= 0 to pred(CurrentSection.count) do
                  begin
                    application.ProcessMessages;
                    SplitKeyValue(CurrentSection[j],Key, Value);
                    FinalSil.WriteString(DetectedSections[i],Key,Value);
                  end;
                Splash.Update;
              finally
                 CurrentSection.free;
              end;
          finally
            CurrentSil.Free;
          end;
        Until FindNext(sr) <> 0;
        FinalSil.free;
        LangINI.free;
      end;
    finally
      FindClose(sr);
      FreeAndNil(Splash);
      Screen.Cursor:= crDefault;
    end;
end;
Bye for everybody and sorry for my poor english,
Pedro de Hollanda do R. B. Neto.
[]'s

Posted: Fri Dec 14, 2007 1:30 pm
by isiticov
Hello Pedro,

Thank you for your post. I'm sure it will be useful for others.