Delphi-PRAXiS
Seite 2 von 2     12   

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Sonstige Fragen zu Delphi (https://www.delphipraxis.net/19-sonstige-fragen-zu-delphi/)
-   -   Delphi multilingual mit ini datei ? (https://www.delphipraxis.net/69184-multilingual-mit-ini-datei.html)

bluescreen25 26. Jul 2007 14:45

Re: multilingual mit ini datei ?
 
Zitat:

Zitat von Sharky
Hai bluescreen25,

das "Problem" ist das ein TLabeledEdit ja ein "normales" Edit ist welches einfach noch ein TBoundLabel als "Unterkomponente" erzeugt. Darum geht das mit dem Code nicht. Aber man kann ihn ja erweitern um einfach die Unterkomponenten auch zu berücksichtigen.

Ich habe das mal für die Funktion WriteIni gemacht.



Das musst Du jetzt nur noch für WriteIni umsetzen und dann sollte es gehen.

Vielen Dank, ich wusste da jetzt keinen Ansatz. Aber gänzlich die Idee für die einfache Handhabe zur Erstellung der language-files ist einfach klasse. Ich brauche bei mir nur ein Setup-file auf diese Weise einmal nur über WriteIni schreiben lassen, habe sofort meine aktuellen Texte in der Ini.

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:

bluescreen25 26. Jul 2007 16:20

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:
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

Geri 24. Jul 2009 23:56

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

Die Muhkuh 25. Jul 2009 07:57

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.
Seite 2 von 2     12   

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