Einzelnen Beitrag anzeigen

bluescreen25

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

Re: multilingual mit ini datei ?

  Alt 25. Jul 2007, 21:09
Zitat von Sharky:
Hai ihr,

ich habe mir da auch mal etwas zum spielen gebastelt. Ich lese/schreibe die Komponentennamen und den Wert für ein Stringproperty in die INI (aufgeteilt nach Klasse und Property).

Das ganze sieht dann so aus:
Delphi-Quellcode:
uses
  TypInfo, IniFiles;

var
  apppath: string;

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 WriteIni(aForm: TForm; aType: TClass; const aProperty: string = 'Text');
var
  CurrentCompo: TComponent;
  ndx: Integer;
  TranslateIni: TMemIniFile;
  CurrentText: string;
begin
  TranslateIni := TMemIniFile.Create(apppath + '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;

procedure ReadIni(aForm: TForm; aType: TClass; const aProperty: string = 'Text');
var
  CurrentCompo: TComponent;
  ndx: Integer;
  TranslateIni: TMemIniFile;
  NewText: string;
begin
  TranslateIni := TMemIniFile.Create(apppath + '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;

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


procedure TDemoForm.FormDestroy(Sender: TObject);
begin
  WriteIni(self, TEdit, 'Text');
  WriteIni(self, TLabel, 'Caption');
end;
und so die INI für diesen Fall:
Code:
[TEdit-Text]
Edit1=Ein Text
Edit2=noch einer
Edit3=
Edit4=*blubb*
[TLabel-Caption]
Label1=Ich bin ein Label
Label2=Ich auch
Label3=*blubb*
Der Vorteil ist das ich nicht für jedes Label und jeden Button usw. aus der INI lesen muss.

Im Angang mal ein DemoProjekt. Zur Zeit bin ich noch daran die Funktion in eine eigene Klasse zu wuchten.
Das funktioniert prima, gerade bei einem kleinen Project eine gute Stütze.

Eine Frage habe ich aber dennoch: Wie erreiche ich ein LabeledEdit.Editlabel.Caption ???

Wenn ich WriteLangIni(self, TLabeledEdit, 'Editlabel.Caption'); anwende, kennt er die property nicht !

Gruß, bluescreen25
...und ich dachte, Delphi ist ein Programmgenerator mit nur einem Button......tzzz
  Mit Zitat antworten Zitat