Einzelnen Beitrag anzeigen

Benutzerbild von Sharky
Sharky

Registriert seit: 29. Mai 2002
Ort: Frankfurt
8.251 Beiträge
 
Delphi 2006 Professional
 

Re: multilingual mit ini datei ?

  Alt 11. Mai 2006, 15:00
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.
Angehängte Dateien
Dateityp: zip translate2_212.zip (9,5 KB, 36x aufgerufen)
Stephan B.
  Mit Zitat antworten Zitat