Einzelnen Beitrag anzeigen

bluescreen25

Registriert seit: 27. Nov 2005
Ort: Geldern
229 Beiträge
 
Delphi 7 Enterprise
 
#12

Re: multilingual mit ini datei ?

  Alt 26. Jul 2007, 17:20
Ich habe das mal ein wenig abgewandet, weil writeIni SubCompo.name beim schreiben in die ini immer gleich ist. Es wurde dann ein Einzeiler (immer überschrieben)

Hier mal zusammenhängend für alle die dieses nützliche Tool gebrauchen können.

Delphi-Quellcode:
unit Language;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ComCtrls;

procedure ReadLangIni(aForm: TForm; aType: TClass; const aProperty: string = 'Text'); overload;
procedure ReadLangIni(aForm: TForm; aType, ASubType: TClass; const aProperty: string = 'Text'); overload;
procedure WriteLangIni(aForm: TForm; aType: TClass; const aProperty: string = 'Text'); overload;
procedure WriteLangIni(aForm: TForm; aType, ASubType: TClass; const aProperty: string = 'Text'); overload;


resourcestring
  SUnknownProperty = 'Eigenschaft %s existiert nicht.';

implementation

uses
  TypInfo, IniFiles, frmSetup;



function MyGetWideStrProp(Instance: TObject; const PropName: string): Widestring;
var
  PropInfo: PPropInfo;
begin
  PropInfo := GetPropInfo(Instance, PropName);
  if PropInfo = NIL then
  begin
    raise EPropertyError.CreateResFmt(@SUnknownProperty, [PropName]);
  end;
  result := GetWideStrProp(Instance, PropName);
end;



procedure MySetWideStrProp(Instance: TObject; const PropName: string; const Value: Widestring);

var
  PropInfo: PPropInfo;
begin
  PropInfo := GetPropInfo(Instance, PropName);
  if PropInfo = NIL then
  begin
    raise EPropertyError.CreateResFmt(@SUnknownProperty, [PropName]);
  end;
  SetWideStrProp(Instance, PropInfo, Value);
end;



procedure WriteLangIni(aForm: TForm; aType: TClass; const aProperty: string = 'Text');
var
  CurrentCompo: TComponent;
  ndx: Integer;
  TranslateIni: TMemIniFile;
  CurrentText: string;
begin
  TranslateIni := TMemIniFile.Create(Programmpfad + '\default.lng');
  try
    for ndx := 0 to Pred(aForm.ComponentCount) do
    begin
      CurrentCompo := aForm.Components[ndx];
      if (CurrentCompo is aType) then
      begin
        CurrentText := MyGetWideStrProp(CurrentCompo, aProperty);
        TranslateIni.WriteString(CurrentCompo.ClassName + '-' + aProperty,
          CurrentCompo.Name, CurrentText);
      end;
    end;
    TranslateIni.UpdateFile;
  finally
    TranslateIni.Free;
  end;
end;


// Das ist die überladene Funktion mit der Unterkompo
procedure WriteLangIni(aForm: TForm; aType, aSubType: TClass; const aProperty: string = 'Text');
var
  CurrentCompo: TComponent;
  SubCompo: TComponent;
  ndx: Integer;
  subndx: Integer;
  TranslateIni: TMemIniFile;
  CurrentText: string;
begin
  TranslateIni := TMemIniFile.Create(ProgrammPfad + '\default.lng');
  try
    for ndx := 0 to Pred(aForm.ComponentCount) do
    begin
      CurrentCompo := aForm.Components[ndx];
      if (CurrentCompo is aType) then
      begin
        for subndx := 0 to Pred(currentcompo.ComponentCount) do // Hier suche ich die Unterkomponente
        begin
          SubCompo := CurrentCompo.Components[subndx];
          if (SubCompo is aSubType) then
          begin
            CurrentText := MyGetWideStrProp(SubCompo, aProperty);
            TranslateIni.WriteString(CurrentCompo.ClassName + '-' + SubCompo.ClassName + '-' + aProperty,
              CurrentCompo.Name, CurrentText);
          end;
        end;
      end;
    end;
    TranslateIni.UpdateFile;
  finally
    TranslateIni.Free;
  end;
end;




procedure ReadLangIni(aForm: TForm; aType: TClass; const aProperty: string = 'Text');
var
  CurrentCompo: TComponent;
  ndx: Integer;
  TranslateIni: TMemIniFile;
  NewText: string;
begin
  TranslateIni := TMemIniFile.Create(ProgrammPfad + '\default.lng');
  try
    for ndx := 0 to Pred(aForm.ComponentCount) do
    begin
      CurrentCompo := aForm.Components[ndx];
      if (CurrentCompo is aType) then
      begin
        NewText := TranslateIni.ReadString(CurrentCompo.ClassName + '-' +
          aProperty, CurrentCompo.Name, '');
        MySetWideStrProp(CurrentCompo, aProperty, NewText);
      end;
    end;
  finally
    TranslateIni.Free;
  end;
end;


// Das ist die überladene Funktion mit der Unterkompo
procedure ReadLangIni(aForm: TForm; aType, ASubType: TClass; const aProperty: string = 'Text');
var
  CurrentCompo: TComponent;
  SubCompo: TComponent;
  ndx: Integer;
  subndx: Integer;
  TranslateIni: TMemIniFile;
  NewText: string;
begin
  TranslateIni := TMemIniFile.Create(ProgrammPfad + '\default.lng');
  try
    for ndx := 0 to Pred(aForm.ComponentCount) do
    begin
      CurrentCompo := aForm.Components[ndx];
      if (CurrentCompo is aType) then
      begin
        for subndx := 0 to Pred(currentcompo.ComponentCount) do // Hier suche ich die Unterkomponente
        begin
          SubCompo := CurrentCompo.Components[subndx];
          if (SubCompo is aSubType) then
          begin
            NewText := TranslateIni.ReadString(CurrentCompo.ClassName + '-' + SubCompo.ClassName + '-' + aProperty, CurrentCompo.Name, '');
            MySetWideStrProp(SubCompo, aProperty, NewText);
          end;
        end;
      end;
    end;
    TranslateIni.UpdateFile;
  finally
    TranslateIni.Free;
  end;
end;


end.
Aufruf:

Delphi-Quellcode:
//Language-file-read
    ReadLangIni(self, TButton, 'Caption');
    ReadLangIni(self, TEdit, 'Text');
    ReadLangIni(self, TLabel, 'Caption');
    ReadLangIni(self, TGroupBox, 'Caption');
    ReadLangIni(self, TCheckBox, 'Caption');
    ReadLangIni(self, TOpenDialog, 'Title');
    ReadLangIni(self, TLabeledEdit,TBoundLabel,'Caption');
    ...

//Language-file-write
    WriteLangIni(self, TButton, 'Caption');
    WriteLangIni(self, TEdit, 'Text');
    WriteLangIni(self, TLabel, 'Caption');
    WriteLangIni(self, TGroupBox, 'Caption');
    WriteLangIni(self, TCheckBox, 'Caption');
    WriteLangIni(self, TLabeledEdit,TBoundLabel, 'Caption');
    ...
Ergebnis Auszugsweise default.lng :

Delphi-Quellcode:
[TButton-Caption]
btnAudiopfad=Select
btnPlaypfad=Select
btnBilderpfad=Select

[TLabel-Caption]
labShortVor=selection forward
labShortBack=selection backwards
labShortEnter=selection execute

[TGroupBox-Caption]
gbDatei=rootfolder setup

[TLabeledEdit-TBoundLabel-Caption]
edtAudiopfad=music rootfolder
edtPlaypfad=playlist rootfolder
edtBilderpfad=photo rootfolder
Gruß bluescreen25
...und ich dachte, Delphi ist ein Programmgenerator mit nur einem Button......tzzz
  Mit Zitat antworten Zitat