Thema: Delphi Sprache ändern mit ?

Einzelnen Beitrag anzeigen

Benutzerbild von Sharky
Sharky

Registriert seit: 29. Mai 2002
Ort: Frankfurt
8.252 Beiträge
 
Delphi 2006 Professional
 
#10

Re: Sprache ändern mit ?

  Alt 22. Apr 2006, 08:02
Hai ihr,

da ich das vor kurzem selber gebraucht habe hatte ich mir mal eine Lösung überlegt.
So könnte man das ganze zum Beispiel machen. Das ganze ist nur als Denkanstoss gedacht.
Ich bringe die Funktion gerade in eine Klasse um das etwas einfacher zu machen.

Eigentlich ganz einfach das ganze. Für jede Komponente wird ein Abschnitt in der Ini erzeugt (besser wäre dann TMemIniFiles) und dort wird der Komponenten name und der Text des gewählten Propertys gespeichert (WriteInit()) bzw. gelesen (ReadIni()).

Delphi-Quellcode:
uses
  TypInfo, IniFiles;

var
  apppath: string;

function MyGetWideStrProp(Instance: TObject; const PropName: string): Widestring;
resourcestring
  SUnknownProperty = 'Eigenschaft %s existiert nicht.';
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);
resourcestring
  SUnknownProperty = 'Eigenschaft %s existiert nicht.';
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 WriteIni(aForm: TForm; aType: TClass; const aProperty: string = 'Text');
var
  CurrentCompo: TComponent;
  ndx: Integer;
  TranslateIni: TIniFile;
  CurrentText: string;
begin
  TranslateIni := TIniFile.Create(apppath + 'data.ini');
  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, CurrentCompo.Name, CurrentText);
      end;
    end;
  finally
    TranslateIni.Free;
  end;
end;

procedure ReadIni(aForm: TForm; aType: TClass; const aProperty: string = 'Text');
var
  CurrentCompo: TComponent;
  ndx: Integer;
  TranslateIni: TIniFile;
  NewText: string;
begin
  TranslateIni := TIniFile.Create(apppath + 'data.ini');
  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, CurrentCompo.Name, '');
        MySetWideStrProp(CurrentCompo, aProperty, NewText);
      end;
    end;
  finally
    TranslateIni.Free;
  end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  apppath := IncludeTrailingBackslash(ExtractFilePath(ParamStr(0)));
  ReadIni(self, TEdit, 'Text');
  ReadIni(self, TLabel, 'Caption');
end;


procedure TForm1.FormDestroy(Sender: TObject);
begin
  WriteIni(self, TEdit, 'Text');
  WriteIni(self, TLabel, 'Caption');
end;
Stephan B.
  Mit Zitat antworten Zitat