[Delphi] Aplikacja wielojęzyczna Ekspert 4/2006 - niepoprawna lista komponentów

0

Witam,

Właśnie zrobiłem proste 'środowisko IDE' dla Asamblera (Potrzebny mi był na zajęcia z ASK).
Chciałem dodać opcje zmiany języka i skorzystałem z propozycji Eksperta (4/2006).
Niestety gdy robiłem krop po kroku zgodnie z opisem natrafiłem już na samym początku na błąd:

for i:=0 to ComponentCount-1 do
    begin
       if isPublishedProp(components[i],'Caption') then
       info:=info+GetPropValue(Components[i],'Caption')+name+#13#10;
    end;
    showmessage(info);

Wyświetla mi listę z błędnymi nazwami komponentów:
http://www.fotosik.pl/showFullSize.php?id=1b38000483897820
Natomiast kiedy zrobiłem to w ten sposób:

for i:=0 to ComponentCount-1 do
    begin
       if isPublishedProp(components[i],'Caption') then
       info:=info+GetPropValue(Components[i],'Caption')+Form1.Components[i].Name+#13#10;
    end;
    showmessage(info);

Wyświetla mi listę z poprawnymi nazwami komponentów:
http://www.fotosik.pl/showFullSize.php?id=4665df27b9f5557d

Stąd moje pytanie jak poprawić plik LANG.PAS czy to może problem z nazewnictwem w moim programie?
Pytanie drugie: Jak zmienić plik LANG.PAS i funkcje generujplikjezyka() tak aby przeszukiwała WSZYSTKIE formatki w programie?

Pozdrawiam,
Andrzej Pichlinski

PS. Używam Delphi 7

PS2. pokaże jeszcze plik jaki jest tworzony z niewiadomych powodów:

C:TabSheet2=Log
F:OpenDialog1=x86 Assembly Files (*.asm)|*.asm|All files (*.*)|*.*
F:SaveDialog1=x86 Assembly Files (*.asm)|*.asm|All files (*.*)|*.*
F:MainMenu1=x86 Assembly Files (*.asm)|*.asm|All files (*.*)|*.*
C:Plik1=&Plik
F:Plik1=x86 Assembly Files (*.asm)|*.asm|All files (*.*)|*.*
C:MN_Nowy=&Nowy
F:MN_Nowy=x86 Assembly Files (*.asm)|*.asm|All files (*.*)|*.*
C:MN_Otowrz=Otwórz...
F:MN_Otowrz=x86 Assembly Files (*.asm)|*.asm|All files (*.*)|*.*
C:MN_Zapisz=Zapisz
F:MN_Zapisz=x86 Assembly Files (*.asm)|*.asm|All files (*.*)|*.*
C:MN_Zapiszjako=Zapisz jako...
F:MN_Zapiszjako=x86 Assembly Files (*.asm)|*.asm|All files (*.*)|*.*
C:N1=-
 
0

Dzięki problem rozwiązany. Polegał on na tym że okna dialogowe mają dwa filtry i wprowadzało to błąd.

Moje drugie pytanie nadal zostało bez odpowiedzi...

Więc mam taki sobie unit LANG.PAS:

 
unit Lang;

interface

uses Forms, SysUtils, Classes, Menus, TypInfo, IniFiles,Dialogs;


type TJezyki = class
     private
       fForm : TForm;                        
       fName : String;
       fPath : String;
       fLines: TStringList;
       procedure GenerujPlik(Ini : TIniFile);

     public
       constructor Create(Form : TForm; Path : String;Name : String='');
       destructor Destroy; override;
       procedure WczytajJezyk;
       procedure GenerujJezyk;
       procedure Menu(M : TMenuItem);
       procedure ZmianaClick(Sender : TObject);

       procedure WczytajPlikJezyka(Ini : TIniFile);

       procedure Dodaj(Nr : Integer;S : String);
       function Pobierz(Nr : Integer; S : String = '') : String;
       property Name : String read fName;
    end;

implementation
uses Unit1, Unit2;


procedure TJezyki.Dodaj(Nr : Integer;S : String);
begin
  while (Nr>=fLines.Count) do fLines.Add('');
  fLines[Nr]:=S;
end;

function TJezyki.Pobierz(Nr : Integer; S : String = '') : String;
begin
  if (Nr<0) or (Nr>fLines.Count-1) then Result:=S else Result:=fLines[Nr];
end;


//-----------------------------------------------------------

constructor TJezyki.Create(Form : TForm; Path: String; Name : String='');
begin
    inherited Create;
    if Form=nil then Exit;
    fForm:=Form;
    fPath:=Path;
    fLines:=TStringList.Create;
    if Name='' then fName:=fForm.Name else fName:=Name;
end;

destructor TJezyki.Destroy;
begin
   fLines.Free;
end;

//-----------------------------------------------------------

procedure TJezyki.GenerujJezyk;
var i : Integer;
    Ini : TIniFile;
begin
     if (fName='') then exit;

     Ini:=TIniFile.Create(fPath+fName+'.lng');
     GenerujPlik(Ini);
     for i:=0 to fLines.Count-1 do Ini.WriteString(fName,'_T'+IntToStr(i),fLines[i]);
     Ini.Free;
end;

//********************************************************************
//najwazniejsza funkcja przeszukujaca fForm!
//*******************************************************************
procedure TJezyki.GenerujPlik(Ini : TIniFile);
var i : Integer;
    C : TComponent;
    N,WC,WH,WT,WF : String;
begin
     Ini.WriteString(fName,'C:',fForm.Caption);

     For i:=0 to fForm.ComponentCount-1 do
     begin
       C:=fForm.Components[i];

       N:=C.Name;
       WC:='';
       WH:='';
       WT:='';

       if Copy(N,1,6)='MN_DYN' then Continue;

       if IsPublishedProp(C,'Caption') then WC:=GetPropValue(C,'Caption');
       if IsPublishedProp(C,'Text') then WT:=GetPropValue(C,'Text');
       if IsPublishedProp(C,'Hint') then WH:=GetPropValue(C,'Hint');
      // if IsPublishedProp(C,'Filter') then WF:=GetPropValue(C,'Filter');


       if WC<>'' then Ini.WriteString(fName,'C:'+N,WC);
       if WH<>'' then Ini.WriteString(fName,'H:'+N,WH);
       if WT<>'' then Ini.WriteString(fName,'T:'+N,WT);
     //  if WF<>'' then Ini.WriteString(fName,'F:'+N,WF);
     end;
 end;

//------------------------------------------------------------------------------

procedure TJezyki.WczytajPlikJezyka(Ini : TIniFile);
var i : Integer;
    K : TComponent;
    A,B,C : String;
    TP : TStringList;
begin
     fForm.Caption:=Ini.ReadString(fName,'C:',fForm.Caption);

     TP:=TStringList.Create;
     Ini.ReadSection(fName,TP);
     for i:=0 to TP.Count-1 do
     begin
         A:=TP[i];                      //cala linijka
         B:=Ini.ReadString(fName,A,''); //sama wartosc
         C:=A[1];                       //typ
         Delete(A,1,2);                 //usuwamy z A typ - mamy nazwe

         if (B='') or (A='') then Continue;

         K:=fForm.FindComponent(A);
         if Assigned(K) then
         begin
           if (C='C') and IsPublishedProp(K,'Caption') then SetPropValue(K,'Caption',B);
           if (C='T') and IsPublishedProp(K,'Text') then SetPropValue(K,'Text',B);
           if (C='H') and IsPublishedProp(K,'Hint') then SetPropValue(K,'Hint',B);
           if (C='F') and IsPublishedProp(K,'Filter') then SetPropValue(K,'Filter',B);
         end;
     end;
     TP.Free;
end;


procedure TJezyki.WczytajJezyk;
var Ini : TIniFile;
    i   : Integer;
begin
     if FileExists(fPath+fName+'.lng') then
     begin
       Ini:=TIniFile.Create(fPath+fName+'.lng');
       WczytajPlikJezyka(Ini);
       for i:=0 to fLines.Count-1 do fLines[i]:=Ini.ReadString(fName,'_T'+IntToStr(i),fLines[i]);
       Ini.Free;
     end;
end;

//------------------------------------------------------------------------------

procedure TJezyki.Menu(M : TMenuItem);
var n : TMenuItem;
    i : Integer;
    SearchRec : TSearchRec;
begin
  i:=1;
  FindFirst(fPath+'*.lng',faAnyFile, SearchRec);
  repeat
        n := TMenuItem.Create(fForm);
        n.Hint:=Copy(SearchRec.Name,1,Length(SearchRec.Name)-4);
        n.Caption:=n.Hint;
        if n.Hint=fName then n.Checked:=TRUE;
        n.Name:='MN_DYN_Jezyk_'+IntToStr(i);
        n.OnClick:=ZmianaClick;
        M.Add(n);
        Inc(i);
  until FindNext(SearchRec)<>0;
  FindClose(SearchRec);
end;

procedure TJezyki.ZmianaClick(Sender : TObject);
var K : TComponent;
    i : Integer;
begin
  if fForm=nil then exit;
  fName:=TMenuItem(Sender).Hint;
  for i:=1 to 20 do
  begin
    K:=fForm.FindComponent('MN_DYN_Jezyk_'+IntToStr(i));
    if Assigned(K) and (K is TMenuItem) then TMenuItem(K).Checked:=FALSE;
  end;
  WczytajJezyk;
  TMenuItem(Sender).Checked:=TRUE; 
end;

end.

Jak można to przerobić by przeszukiwane były wszystkie formy aplikacji lub po prostu podane formy aplikacji.

0
procedure TJezyki.GenerujPlik(Forma: TForm; Ini: TIniFile);
var
 i: Integer;
 C: TComponent;
 N, WC, WH, WT, WF: string;
begin
 Ini.WriteString(fName,'C:',Forma.Caption);

 for i := 0 to Forma.ComponentCount-1 do
   begin
    C  := Forma.Components[i];
    N  := C.Name;
    WC := '';
    WH := '';
    WT := '';
    //if Copy(N,1,6)='MN_DYN' then Continue;

    if IsPublishedProp(C,'Caption') then WC := GetPropValue(C,'Caption');
    if IsPublishedProp(C,'Text') then WT := GetPropValue(C,'Text');
    if IsPublishedProp(C,'Hint') then WH := GetPropValue(C,'Hint');
    // if IsPublishedProp(C,'Filter') then WF:=GetPropValue(C,'Filter');
    if WC<>'' then Ini.WriteString(fName,'C:'+N,WC);
    if WH<>'' then Ini.WriteString(fName,'H:'+N,WH);
    if WT<>'' then Ini.WriteString(fName,'T:'+N,WT);
    //  if WF<>'' then Ini.WriteString(fName,'F:'+N,WF);
   end;
end;


//wykorzystanie
Plik := ExtractFilePath(Application.ExeName) + 'polski.ini';
GenerujPlik(FormaNazwaABC, Plik);
GenerujPlik(FormaCosTam, Plik);
GenerujPlik(FormaPelnyEkran, Plik);
GenerujPlik(FormaUstawienia, Plik);
 
0
Opi napisał(a)
//wykorzystanie
Plik := ExtractFilePath(Application.ExeName) + 'polski.ini';
GenerujPlik(FormaNazwaABC, Plik);
GenerujPlik(FormaCosTam, Plik);
GenerujPlik(FormaPelnyEkran, Plik);
GenerujPlik(FormaUstawienia, Plik);
 

tak też powinno zadziałać (oczywiście dla formularzy tworzonych statycznie):

 Plik := ExtractFilePath(Application.ExeName) + 'polski.ini';
 for i:=0 to Application.ComponentCount - 1 do
 begin
   if Application.Components[i] is TForm then
     GenerujPlik(TForm(Application.Components[i]), Plik);
 end;
 

1 użytkowników online, w tym zalogowanych: 0, gości: 1