![]() |
Re: multilingual mit ini datei ?
Zitat:
Danach kann ich das Write ausklammern und die ini kann mit diversen Sprachen vervielfältigt werden. Mal sehn vielleicht mache ich das noch umschaltbar im laufenden Betrieb. Hmm da fällt mir gerade ein, ein Ansatz für die Items (können ja mehrere sein) einer TRadioGroup hast evt auch noch im Kopf ? Ich denke mit den Möglichkeiten dann kann man alle anderen Kompos wohl erschlagen. Sagenhaft... :dancer: |
Re: multilingual mit ini datei ?
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) :wink:
Hier mal zusammenhängend für alle die dieses nützliche Tool gebrauchen können.
Delphi-Quellcode:
Aufruf:
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.
Delphi-Quellcode:
Ergebnis Auszugsweise default.lng :
//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'); ...
Delphi-Quellcode:
Gruß bluescreen25
[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 |
Re: multilingual mit ini datei ?
Hallo
Vielen Dank, eine absolut tolle und einfache Library! Wie würdet ihr nun mit einer gesamten Applikation umgehen. Es kann ja gut sein, dass es z.B. in zwei verschiedenen Dialogen einen Button namens TButton1 gibt. Möglich wäre meiner Ansicht nach vielleicht für jedes Formular eine Datei anzulegen oder zu den Namen als Prefix den Formularnamen hinzufügen
Delphi-Quellcode:
procedure TTfrmLanguageTest.Button1Click(Sender: TObject);
var j: integer; Form:TForm; begin for j := 0 to -1 + Screen.FormCount do Begin Form:=Screen.Forms[j]; WriteLangIni(Form, TButton, 'Caption'); WriteLangIni(Form, TMenuitem, 'Caption'); WriteLangIni(Form, TEdit, 'Text'); WriteLangIni(Form, TLabel, 'Caption'); WriteLangIni(Form, TGroupBox, 'Caption'); WriteLangIni(Form, TCheckBox, 'Caption'); End; end; Gibt es auch eine Möglichkeit um Ressourcenstrings auf diese weise auszulesen? Vielleicht hier nicht sinnvoll für mich aber trotzdem aber interessant. Oder noch viel besser. In meinen Programmen habe ich vielfach direkt Texte übergeben. S.w. sollte man dies vermeiden:) Beispiel raise Exception.create('Unknown class in Audiofile defintion ' + t.ClassName); oder in Messageboxen etc.:) Kann man solche "hart" codierte Strings mit ähnlichen Mitteln auch aufspüren? Beste Grüsse und vielen Dank für die Infos Geri |
Re: multilingual mit ini datei ?
Hi,
von einer Ini-Lösung würde ich Dir abraten, Geri. Bernhard Geyer hat in Beitrag #7 GNU GetText genannt, mit dem geht das ganze viel einfacher und Du brauchst Dir um solche Sachen wie "zwei Buttons1" keine Gedanken machen. |
Alle Zeitangaben in WEZ +1. Es ist jetzt 04:24 Uhr. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz