Implement the interface without files with ini files

zhaozj2021-02-08  198

Document content is more and more programs use multi-language switch, although Delphi comes with multiple language packages, but that method will flash when the language is switched, and it is very troublesome, I am introduced here. It is to read the language text of the interface using the INI file, which is not only simple and easy, but will not blink the interface when switching. We started from an example to see how to switch between language. First establish a new project. Component is placed in the above chart: MainMenu1: TMainMenu; File1: TMenuItem; Exit1: TMenuItem; Label1: TLabel; Button1: TButton; CheckBox1: TCheckBox; CheckBox2: TCheckBox; Button2: TButton; Label2: TLabel; ComboBox1: TComboBox; Label3: TLabel; Since INI file is read, add Declaration inIfiles in UseS; then set the button1 and butt2 properties to true; where we use ComboBox1 to display the available languages ​​and to select language. We edited the following chinese gb.ini files in the program:; /// ;; Translated some rules :; Before translation, copy Chinese Gb.ini Rename to YourLanguage.ini; Just translate symbol '=' [Translations]; label1.caption = text 1 label2.caption = text 2 label3.caption = language Button1.caption = button 1 Button2.caption = button 2 Button1.hint = button 1_ 提 button 2.hint = button 2_ prompt Checkbox1.caption = check box 1 checkbox2.caption = check box 2 file1.caption = file EXIT1.CAPTION = Exit; [Messages]; m1 = information box test; // The same method Edited a named English.ini The file will change the text on the left of "=" to English.

For example: Label1.caption = Label1 program runs, we look for all language profiles (* .ini) in the current directory, in order to achieve this, I have written the following function search directory all language configuration files, then remove the ini file name extension to hold the return: function TForm1.SearchLanguagePack: TStrings; var ResultStrings: TStrings; DosError: integer; SearchRec: TsearchRec; begin ResultStrings: = TStringList.Create; DosError: = FindFirst (ExtractFilePath (ParamStr (0) ) '*. INI', FAANYFILE, SearchRec); While Doserror = 0 Do Begin {Return file name and go to the end of the .ini character} results.add (searchrec.name, ''); doserror: = FindNext (SearchRec); end; findclose (searchRec); result: = resultStrings; end; add code in Form-established events, add all the language file names in the directory to the selection list box. Procedure TForm1.FormCreate (Sender: TOBJECT); Begin ComboBox1.Items.addstrings (SearchLanguagePack); END; Program focus on how to switch language, switching in the ONCHANGE event of ComboBox1. Here I wrote the setActiveLanguage process for implementing this.

procedure TForm1.ComboBox1Change (Sender: TObject); begin SetActiveLanguage (ComboBox1.Text); end; wherein SetActiveLanguage code is as follows: procedure TForm1.SetActiveLanguage (LanguageName: string); const Translations = 'Translations'; Messages = 'Messages'; var frmComponent : TComponent; i: Integer; begin with TInifile.Create (ExtractFilePath (ParamStr (0)) LanguageName 'ini.') do begin for i: = 0 to ComponentCount-1 do {Form traverse assembly} begin frmComponent: = components [ I]; if fmcomponent is trabel dam {If the component is TLABEL type as TLabel processing, the following} begin (frMcomponent As TLabel) .caption: = readstring (Translations, frmcomponent.name '. caption ", (FRMComponent As TLabel) ) .Caption); end; if frmComponent is TCheckBox then begin (frmComponent as TCheckBox) .Caption: = ReadString (Translations, frmComponent.Name '. Caption', (frmComponent as TCheckBox) .Caption); end; if frmComponent is TButton Then Begin (FRM) Component as TButton) .Caption: = ReadString (Translations, frmComponent.Name 'Caption.', (FrmComponent as TButton) .Caption); (frmComponent as TButton) .Hint:. = ReadString (Translations, frmComponent.Name 'Hint ', (frmComponent as TButton) .Hint); end; if frmComponent is TMenuItem then begin (frmComponent as TMenuItem) .Caption: = ReadString (Translations, frmComponent.Name ' Caption ', (frmComponent as TMenuItem.) .Caption); End; end; m1: = readstring (messages, 'm1', m1); end; end; in this process,

转载请注明原文地址:https://www.9cbs.com/read-2865.html

New Post(0)