home *** CD-ROM | disk | FTP | other *** search
/ Chip 2002 March / Chip_2002-03_cd1.bin / zkuste / delphi / kompon / d56 / LNGSUPP.ZIP / LangINISupport / LngINISupp.pas < prev    next >
Pascal/Delphi Source File  |  2001-12-14  |  16KB  |  457 lines

  1. unit LngINISupp;
  2.  
  3. interface
  4.  
  5. uses      
  6.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
  7.   Dialogs, IniFiles, StdCtrls, menus, extctrls, chart, comctrls, buttons;
  8.  
  9. type
  10.   TLngINISupp = class(TComponent)
  11.   private
  12.     FFileName: TFileName;
  13.     FMenu: TMainMenu;
  14.     FPMenu: TPopupMenu;
  15.     procedure SetFileName(const Value: TFileName);
  16.     procedure CheckFile(FN: TFileName);
  17.     procedure FindCaptions;
  18.     procedure MenuOnClick(Sender: TObject);
  19.     procedure PMenuOnClick(Sender: TObject);
  20.     procedure ReadCaptions;
  21.     procedure FindLngFilesM;
  22.     procedure FindLngFilesP;
  23.     procedure VerifyValue(FormName, CompName, CapLab: String);
  24.     procedure SetMenu(const Value: TMainMenu);
  25.     procedure SetPMenu(const Value: TPopupMenu);
  26.   protected
  27.   public
  28.     procedure Open;
  29.     procedure Close;
  30.     procedure Read;
  31.     procedure Save;
  32.   published
  33.     property FileName: TFileName read FFileName write SetFileName;
  34.     property Menu: TMainMenu read FMenu write SetMenu;
  35.     property PopupMenu: TPopupMenu read FPMenu write SetPMenu;
  36.   end;
  37.  
  38. var
  39.   AppIni: TIniFile;
  40.   FNF: Bool = false;
  41.   FNP: Bool = false;
  42.   VV: Bool = false;
  43.   MC: Bool = false;
  44.   PC: Bool = false;
  45.   LngFiles: TStrings;
  46.   Lng, Child: TMenuItem;
  47.   PLng, PChild: TMenuItem;
  48.   Changed: Bool = false;
  49.  
  50. procedure Register;
  51.  
  52. implementation
  53.  
  54. uses MsgINISupp;
  55.  
  56. {$R LangINI.RES}
  57.  
  58. procedure Register;
  59. begin
  60.   RegisterComponents('LangINISupport', [TLngINISupp]);
  61. end;
  62.  
  63. { TLngINISupp }
  64.  
  65. procedure TLngINISupp.Open;
  66. begin
  67.   CheckFile(FFileName);
  68.   if FNP = true then abort;
  69.   VV := false;
  70.   FindCaptions;
  71.   ReadCaptions;
  72.   if ((FMenu <> nil) and (MC = false)) then
  73.     begin
  74.       LngFiles := TStringList.Create;
  75.       FindLngFilesM;
  76.       LngFiles.Free;
  77.     end;
  78.   if ((FPMenu <> nil) and (PC = false)) then
  79.     begin
  80.       LngFiles := TStringList.Create;
  81.       FindLngFilesP;
  82.       LngFiles.Free;
  83.     end;
  84. end;
  85.  
  86. procedure TLngINISupp.Read;
  87. begin
  88.   CheckFile(FFileName);
  89.   if FNP = true then abort;
  90.   VV := false;
  91.   FindCaptions;
  92.   ReadCaptions;
  93. end;
  94.  
  95. procedure TLngINISupp.Close;
  96. begin
  97.  FFileName := '';
  98. end;
  99.  
  100. procedure TLngINISupp.SetFileName(const Value: TFileName);
  101. begin
  102.   FFileName := ExtractFileDir(ParamStr(0)) + '\' + ExtractFileName(Value);
  103. end;
  104.  
  105. procedure TLngINISupp.SetPMenu(const Value: TPopupMenu);
  106. begin
  107.   FPMenu := Value;
  108. end;
  109.  
  110. procedure TLngINISupp.VerifyValue(FormName, CompName, CapLab: String);
  111. begin
  112.   AppIni := TIniFile.Create(FFileName);
  113.   if ((not AppIni.ValueExists(FormName, CompName)) or (VV = true)) then
  114.      AppIni.WriteString(FormName, CompName, CapLab);
  115.   AppIni.Free;
  116. end;
  117.  
  118. procedure TLngINISupp.SetMenu(const Value: TMainMenu);
  119. begin
  120.   FMenu := Value;
  121. end;
  122.  
  123. procedure TLngINISupp.MenuOnClick(Sender: TObject);
  124. var
  125.   MLFN: TFileName;
  126. begin
  127.   with Sender as TMenuItem do
  128.     begin
  129.       MLFN := Name + '.lng';
  130.       SetFileName(ExtractFileDir(ParamStr(0)) + '\' + MLFN);
  131.       Open;
  132.     end;
  133. end;
  134.  
  135. procedure TLngINISupp.PMenuOnClick(Sender: TObject);
  136. var
  137.   MLFN: String;
  138. begin
  139.   with Sender as TMenuItem do
  140.     begin
  141.       MLFN := Name + '.lng';
  142.       System.Delete(MLFN, 1, 1);
  143.       SetFileName(ExtractFileDir(ParamStr(0)) + '\' + MLFN);
  144.       Open;
  145.     end;
  146. end;
  147.  
  148. procedure TLngINISupp.FindCaptions;
  149. var
  150.   i,j,k:Integer;
  151.   Source: TComponent;
  152. begin
  153.   for i := 0 to self.Owner.Owner.ComponentCount - 1 do
  154.    begin
  155.     for j := 0 to self.Owner.Owner.Components[i].ComponentCount - 1 do
  156.      begin
  157.        Source := self.Owner.Owner.Components[i].Components[j];
  158.        if Source is TButton then                                     //Buttons
  159.        with Source as TButton do
  160.           VerifyValue(Owner.Name, Name, Caption)
  161.        else if Source is TSpeedButton then                           //SpeedButton
  162.        with Source as TSpeedButton do
  163.           VerifyValue(Owner.Name, Name, Caption)
  164.        else if Source is TLabel then                                 //Labels
  165.        with Source as TLabel do
  166.           VerifyValue(Owner.Name, Name, Caption)
  167.        else if Source is TOpenDialog then                            //Open & Save Dialogs
  168.        with Source as TOpenDialog do
  169.           VerifyValue(Owner.Name, Name, Title)
  170.        else if Source is TMenuItem then                              //Menus
  171.        with Source as TMenuItem do
  172.           VerifyValue(Owner.Name, Name, Caption)
  173.        else if Source is TCheckBox then                              //CheckBoxes
  174.        with Source as TCheckBox do
  175.           VerifyValue(Owner.Name, Name, Caption)
  176.        else if Source is TGroupBox then                              //GroupBoxes
  177.        with Source as TGroupBox do
  178.           VerifyValue(Owner.Name, Name, Caption)
  179.        else if Source is TRadioButton then                           //RadioButtons
  180.        with Source as TRadioButton do
  181.           VerifyValue(Owner.Name, Name, Caption)
  182.        else if Source is TRadioGroup then                            //RadioGroups
  183.        with Source as TRadioGroup do
  184.           VerifyValue(Owner.Name, Name, Caption)
  185.        else if Source is TPanel then                                 //Panels
  186.        with Source as TPanel do
  187.           VerifyValue(Owner.Name, Name, Caption)
  188.        else if Source is TChart then                                 //Charts
  189.        with Source as TChart do
  190.           VerifyValue(Owner.Name, Name,Title.Text.Strings[0])
  191.        else if Source is TTabSheet then                              //TabSheets
  192.        with Source as TTabSheet do
  193.           VerifyValue(Owner.Name, Name, Caption)
  194.        else if Source is TStatusBar then                             //TStatusBars
  195.        with Source as TStatusBar do
  196.            for k := 0 to Panels.Count - 1 do
  197.             VerifyValue(Owner.Name, Name + '.Panels[' + IntToStr(k)
  198.                 + ']', Panels[k].Text)
  199.        else if Source is TStaticText then                            //StaticTexts
  200.        with Source as TStaticText do
  201.           VerifyValue(Owner.Name, Name, Caption)
  202.      end;
  203.    end;
  204. end;
  205.  
  206. procedure TLngINISupp.ReadCaptions;
  207. var
  208. Sections, Values: TStrings;
  209. i, ii, j, jj, sbp, k: Integer;
  210. Source: TComponent;
  211. sbn: string;
  212. begin
  213.   Sections := TStringList.Create;
  214.   Values   := TStringList.Create;
  215.   AppIni := TIniFile.Create(FFileName);
  216.   AppIni.ReadSections(Sections);
  217.   for i := 0 to Sections.Count - 1 do
  218.     begin
  219.        AppIni.ReadSection(Sections[i], Values);
  220.        for j := 0 to Values.Count - 1 do
  221.          begin
  222.            for ii := 0 to self.Owner.Owner.ComponentCount - 1 do
  223.              begin
  224.                for jj := 0 to self.Owner.Owner.Components[ii].ComponentCount - 1 do
  225.                  begin
  226.                    Source := self.Owner.Owner.Components[ii].Components[jj];
  227.                    if self.Owner.Owner.Components[ii].Name = Sections[i] then             //Buttons
  228.                      if Source is TButton then
  229.                        if Source.Name = Values[j] then
  230.                          with Source as TButton do
  231.                            Caption := AppIni.ReadString(Sections[i], Values[j], 'ERROR');
  232.                    if self.Owner.Owner.Components[ii].Name = Sections[i] then             //SpeedButtons
  233.                      if Source is TSpeedButton then
  234.                        if Source.Name = Values[j] then
  235.                          with Source as TSpeedButton do
  236.                            Caption := AppIni.ReadString(Sections[i], Values[j], 'ERROR');
  237.                    if self.Owner.Owner.Components[ii].Name = Sections[i] then             //Labels
  238.                      if Source is TLabel then
  239.                        if Source.Name = Values[j] then
  240.                          with Source as TLabel do
  241.                            Caption := AppIni.ReadString(Sections[i], Values[j], 'ERROR');
  242.                    if self.Owner.Owner.Components[ii].Name = Sections[i] then             //Open & Save Dialogs
  243.                      if Source is TOpenDialog then
  244.                        if Source.Name = Values[j] then
  245.                          with Source as TOpenDialog do
  246.                            Title := AppIni.ReadString(Sections[i], Values[j], 'ERROR');
  247.                    if self.Owner.Owner.Components[ii].Name = Sections[i] then             //Menus
  248.                      if Source is TMenuItem then
  249.                        if Source.Name = Values[j] then
  250.                          with Source as TMenuItem do
  251.                            Caption := AppIni.ReadString(Sections[i], Values[j], 'ERROR');
  252.                    if self.Owner.Owner.Components[ii].Name = Sections[i] then             //CheckBoxes
  253.                      if Source is TCheckBox then
  254.                        if Source.Name = Values[j] then
  255.                          with Source as TCheckBox do
  256.                            Caption := AppIni.ReadString(Sections[i], Values[j], 'ERROR');
  257.                    if self.Owner.Owner.Components[ii].Name = Sections[i] then             //GroupBoxes
  258.                      if Source is TGroupBox then
  259.                        if Source.Name = Values[j] then
  260.                          with Source as TGroupBox do
  261.                            Caption := AppIni.ReadString(Sections[i], Values[j], 'ERROR');
  262.                    if self.Owner.Owner.Components[ii].Name = Sections[i] then             //RadioButtons
  263.                      if Source is TRadioButton then
  264.                        if Source.Name = Values[j] then
  265.                          with Source as TRadioButton do
  266.                            Caption := AppIni.ReadString(Sections[i], Values[j], 'ERROR');
  267.                    if self.Owner.Owner.Components[ii].Name = Sections[i] then             //RadioGroups
  268.                      if Source is TRadioGroup then
  269.                        if Source.Name = Values[j] then
  270.                          with Source as TRadioGroup do
  271.                            Caption := AppIni.ReadString(Sections[i], Values[j], 'ERROR');
  272.                    if self.Owner.Owner.Components[ii].Name = Sections[i] then             //Panels
  273.                      if Source is TPanel then
  274.                        if Source.Name = Values[j] then
  275.                          with Source as TPanel do
  276.                            Caption := AppIni.ReadString(Sections[i], Values[j], 'ERROR');
  277.                    if self.Owner.Owner.Components[ii].Name = Sections[i] then             //Charts
  278.                      if Source is TChart then
  279.                        if Source.Name = Values[j] then
  280.                          with Source as TChart do
  281.                            Title.Text.Strings[0] := AppIni.ReadString(Sections[i], Values[j], 'ERROR');
  282.                    if self.Owner.Owner.Components[ii].Name = Sections[i] then             //TabSheets
  283.                      if Source is TTabSheet then
  284.                        if Source.Name = Values[j] then
  285.                          with Source as TTabSheet do
  286.                            Caption := AppIni.ReadString(Sections[i], Values[j], 'ERROR');
  287.                    if self.Owner.Owner.Components[ii].Name = Sections[i] then             //StaticTexts
  288.                      if Source is TStaticText then
  289.                        if Source.Name = Values[j] then
  290.                          with Source as TStaticText do
  291.                            Caption := AppIni.ReadString(Sections[i], Values[j], 'ERROR');
  292.                    if self.Owner.Owner.Components[ii].Name = Sections[i] then             //CheckBoxes
  293.                      if Source is TStatusBar then
  294.                        begin
  295.                          sbn := '';
  296.                          sbp := 0;
  297.                          for k := 1 to Length(Values[j]) do
  298.                            begin
  299.                              if Values[j][k] = '.' then
  300.                                break
  301.                              else
  302.                                sbn := sbn + Values[j][k];
  303.                            end;
  304.                          for k := 1 to Length(Values[j]) do
  305.                            begin
  306.                              if Values[j][k] = '[' then
  307.                                sbp := StrToInt(Values[j][k + 1]);
  308.                            end;
  309.                          if Source.Name = sbn then
  310.                            with Source as TStatusBar do
  311.                              Panels[sbp].Text := AppIni.ReadString(Sections[i], Values[j], 'ERROR');
  312.                        end;
  313.                  end;
  314.              end;
  315.          end;
  316.     end;
  317.   AppIni.Free;
  318.   Values.Free;
  319.   Sections.Free;
  320. end;
  321.  
  322. procedure TLngINISupp.CheckFile(FN: TFileName);
  323. var
  324.  strm: TMemoryStream;
  325. begin
  326.   strm := TMemoryStream.Create;
  327.   if FN = '' then
  328.     begin
  329.       ShowMessage('ERROR!!!' + #13#10 + 'File not preset!');
  330.       FNP := true;
  331.     end
  332.   else
  333.     begin
  334.       try
  335.         FNP := false;
  336.         strm.LoadFromFile(FN);
  337.        except
  338.         ShowMessage('WARNING!!!' + #13#10 + ' Language File Not Found! Its now creating a new!');
  339.         FNF := true;
  340.       end;
  341.     end;
  342.   strm.Free;
  343. end;
  344.  
  345. procedure TLngINISupp.Save;
  346. begin
  347.   CheckFile(FFileName);
  348.   if FNP = true then abort;
  349.   FNF := false;
  350.   VV := true;
  351.   FindCaptions;
  352. end;
  353.  
  354. procedure TLngINISupp.FindLngFilesM;
  355. var
  356.   iii: Integer;
  357.   sr: TSearchRec;
  358.   FileAttrs: Integer;
  359.   lfn: string;
  360. begin
  361.   FileAttrs := faReadOnly;
  362.   FileAttrs := FileAttrs + faHidden;
  363.   FileAttrs := FileAttrs + faSysFile;
  364.   FileAttrs := FileAttrs + faVolumeID;
  365.   FileAttrs := FileAttrs + faDirectory;
  366.   FileAttrs := FileAttrs + faArchive;
  367.   FileAttrs := FileAttrs + faAnyFile;
  368.   if FindFirst(ExtractFileDir(ParamStr(0))+'\*.lng', FileAttrs, sr) = 0 then
  369.   begin
  370.     with LngFiles do
  371.     begin
  372.       if (sr.Attr and FileAttrs) = sr.Attr then
  373.       begin
  374.         Add(sr.Name);
  375.       end;
  376.       while FindNext(sr) = 0 do
  377.       begin
  378.         if (sr.Attr and FileAttrs) = sr.Attr then
  379.         begin
  380.         Add(sr.Name);
  381.         end;
  382.       end;
  383.       FindClose(sr);
  384.     end;
  385.   end;
  386.   Lng := TMenuItem.Create(self);
  387.   Lng.Name := 'LngItem';
  388.   Lng.Caption := 'Language';
  389.   FMenu.Items.Insert(FMenu.Items.Count, Lng);
  390.   for iii := 0 to LngFiles.Count - 1 do
  391.    begin
  392.      lfn := LngFiles[iii];
  393.      AppIni := TIniFile.Create(ExtractFileDir(ParamStr(0)) + '\' + lfn);
  394.      delete(lfn, pos('.', lfn), 4);
  395.      Child := TMenuItem.Create(self);
  396.      Child.Name := lfn;
  397.      Child.Caption := AppIni.ReadString('Language', 'Language', 'ERROR');
  398.      Child.OnClick := MenuOnClick;
  399.      Lng.Add(Child);
  400.      AppIni.Free;
  401.    end;
  402.   MC := true;
  403. end;
  404.  
  405. procedure TLngINISupp.FindLngFilesP;
  406. var
  407.   iii: Integer;
  408.   sr: TSearchRec;
  409.   FileAttrs: Integer;
  410.   lfn: string;
  411. begin
  412.   FileAttrs := faReadOnly;
  413.   FileAttrs := FileAttrs + faHidden;
  414.   FileAttrs := FileAttrs + faSysFile;
  415.   FileAttrs := FileAttrs + faVolumeID;
  416.   FileAttrs := FileAttrs + faDirectory;
  417.   FileAttrs := FileAttrs + faArchive;
  418.   FileAttrs := FileAttrs + faAnyFile;
  419.   if FindFirst(ExtractFileDir(ParamStr(0))+'\*.lng', FileAttrs, sr) = 0 then
  420.   begin
  421.     with LngFiles do
  422.     begin
  423.       if (sr.Attr and FileAttrs) = sr.Attr then
  424.       begin
  425.         Add(sr.Name);
  426.       end;
  427.       while FindNext(sr) = 0 do
  428.       begin
  429.         if (sr.Attr and FileAttrs) = sr.Attr then
  430.         begin
  431.         Add(sr.Name);
  432.         end;
  433.       end;
  434.       FindClose(sr);
  435.     end;
  436.   end;
  437.   PLng := TMenuItem.Create(self);
  438.   PLng.Name := 'PLngItem';
  439.   PLng.Caption := 'Language';
  440.   FPMenu.Items.Insert(FPMenu.Items.Count, PLng);
  441.   for iii := 0 to LngFiles.Count - 1 do
  442.    begin
  443.      lfn := LngFiles[iii];
  444.      AppIni := TIniFile.Create(ExtractFileDir(ParamStr(0)) + '\' + lfn);
  445.      delete(lfn, pos('.', lfn), 4);
  446.      PChild := TMenuItem.Create(self);
  447.      PChild.Name := 'P' + lfn;
  448.      PChild.Caption := AppIni.ReadString('Language', 'Language', 'ERROR');
  449.      PChild.OnClick := PMenuOnClick;
  450.      PLng.Add(PChild);
  451.      AppIni.Free;
  452.    end;
  453.   PC := true;
  454. end;
  455.  
  456. end.
  457.