// Author: cxg
{For example:
Procedure tform1.combobox1change (Sender: tobject );
Begin
Setactivelanguage (combobox1.text );
End;
Procedure tform1.formcreate (Sender: tobject );
Begin
Combobox1.items. addstrings (searchmediaagepack );
End;
}
Unit ulanguage;
Interface
Uses
Windows, messages, sysutils, classes, graphics, controls, forms, dialogs,
Stdctrls, menus, inifiles, comctrls, extctrls,
Cxbuttons
;
VaR
G_hash: thashedstringlist;
// Search for the INI file generation language list
Function searchmediaagepack: tstrings;
// Set the attributes of the specified form and its child controls based on the selected language
Procedure setactivelanguage (owner: tform; const extends agename: string );
// Write the attributes of the specified form and its child controls to the INI file.
Procedure writeproperty (owner: tform; const consume agename: string );
Function gethash: thashedstringlist;
Function gethashstr (const key: string): string;
Procedure setlanguage (owner: tform );
Procedure writelangini (owner: tform );
Implementation
Function gethash: thashedstringlist;
VaR
INI: Tinifile;
Begin
INI: = Tinifile. Create (extractfilepath (application. exename) + 'System. ini ');
Result: = thashedstringlist (thandle (INI. readinteger ('pointer ', 'hash', 0 )));
INI. Free;
End;
Function gethashstr (const key: string): string;
Begin
Result: = gethash. Values [Key];
End;
Function _ writelangini: Boolean;
VaR
INI: Tinifile;
Begin
INI: = Tinifile. Create (extractfilepath (application. exename) + 'System. ini ');
Result: = ini. readbool ('language ', 'writelangini', false );
INI. Free;
End;
Procedure writelangini (owner: tform );
Begin
If _ writelangini then
Ulanguage. writeproperty (owner, 'Chinese (GB )');
End;
Procedure setlanguage (owner: tform );
VaR
INI: Tinifile;
Begin
INI: = Tinifile. Create (extractfilepath (application. exename) + 'System. ini ');
Ulanguage. setactivelanguage (owner, INI. readstring ('language', 'currentlanguage', 'Chinese (GB )'));
INI. Free;
End;
Function searchmediaagepack: tstrings;
VaR
Resultstrings: tstrings;
Doserror: integer;
Searchrec: tsearchrec;
Begin
Resultstrings: = tstringlist. Create;
Doserror: = findfirst (extractfilepath (application. exename) + 'language/'+' *. ini ', faanyfile, searchrec );
While doserror = 0 do
Begin
{Name of the returned file and remove the. ini character at the end}
Resultstrings. Add (changefileext (searchrec. Name ,''));
Doserror: = findnext (searchrec );
End;
Findclose (searchrec );
Result: = resultstrings;
End;
Procedure writeproperty (owner: tform; const consume agename: string );
VaR
C: tcomponent;
I: integer;
Begin
If owner = nil then exit;
With Tinifile. Create (extractfilepath (application. exename) + 'language/'+ languagename +'. ini ') Do
Begin
Writestring (owner. Name, owner. Name + '. Caption', owner. Caption );
For I: = 0 to owner. componentcount-1 do
Begin
C: = owner. components [I];
If C is tlabel then
Begin
Writestring (owner. Name, C. Name + '. Caption', (C as tlabel). Caption );
End else
If C is tlabelededit then
Begin
Writestring (owner. Name, C. Name + '. editlabel. Caption', (C as tlabelededit). editlabel. Caption );
End else
If C is tcheckbox then
Begin
Writestring (owner. Name, C. Name + '. Caption', (C as tcheckbox). Caption );
End else
If C is tbutton then
Begin
Writestring (owner. Name, C. Name + '. Caption', (C as tbutton). Caption );
Writestring (owner. Name, C. Name + '. hint', (C as tbutton). Hint );
End else
If C is tcxbutton then
Begin
Writestring (owner. Name, C. Name + '. Caption', (C as tcxbutton). Caption );
Writestring (owner. Name, C. Name + '. hint', (C as tcxbutton). Hint );
End else
If C is tmenuitem then
Begin
Writestring (owner. Name, C. Name + '. Caption', (C as tmenuitem). Caption );
End else
If C is ttoolbutton then
Begin
Writestring (owner. Name, C. Name + '. Caption', (C as ttoolbutton). Caption );
Writestring (owner. Name, C. Name + '. hint', (C as ttoolbutton). Hint );
End else
If C is ttabsheet then
Begin
Writestring (owner. Name, C. Name + '. Caption', (C as ttabsheet). Caption );
End;
End;
End;
End;
Procedure setactivelanguage (owner: tform; const extends agename: string );
Const
Messages = 'messages '; // string variable section
VaR
C: tcomponent;
I: integer;
INI: Tinifile;
Begin
If owner = nil then exit;
INI: = Tinifile. Create (extractfilepath (application. exename) + 'language/'+ languagename +'. ini ');
With ini do
Begin
Owner. Caption: = readstring (owner. Name, owner. Name + '. Caption', owner. Caption );
For I: = 0 to owner. componentcount-1 do
Begin
C: = owner. components [I];
If C is tlabel then
Begin
(C as tlabel). Caption: = readstring (owner. Name, C. Name + '. Caption', (C as tlabel). Caption );
End else
If C is tlabelededit then
Begin
(C as tlabelededit). editlabel. Caption: = readstring (owner. Name, C. Name + '. editlabel. Caption', (C as tlabelededit). editlabel. Caption );
End else
If C is tcheckbox then
Begin
(C as tcheckbox). Caption: = readstring (owner. Name, C. Name + '. Caption', (C as tcheckbox). Caption );
End else
If C is tbutton then
Begin
(C as tbutton). Caption: = readstring (owner. Name, C. Name + '. Caption', (C as tbutton). Caption );
(C as tbutton). Hint: = readstring (owner. Name, C. Name + '. hint', (C as tbutton). Hint );
End else
If C is tcxbutton then
Begin
(C as tcxbutton). Caption: = readstring (owner. Name, C. Name + '. Caption', (C as tcxbutton). Caption );
(C as tcxbutton). Hint: = readstring (owner. Name, C. Name + '. hint', (C as tcxbutton). Hint );
End else
If C is tmenuitem then
Begin
(C as tmenuitem). Caption: = readstring (owner. Name, C. Name + '. Caption', (C as tmenuitem). Caption );
End else
If C is ttoolbutton then
Begin
(C as ttoolbutton). Caption: = readstring (owner. Name, C. Name + '. Caption', (C as ttoolbutton). Caption );
(C as ttoolbutton). Hint: = readstring (owner. Name, C. Name + '. hint', (C as ttoolbutton). Hint );
End else
If C is ttabsheet then
Begin
(C as ttabsheet). Caption: = readstring (owner. Name, C. Name + '. Caption', (C as ttabsheet). Caption );
End;
End;
Readsectionvalues ('messages ', g_hash );
End;
INI. Free;
INI: = Tinifile. Create (extractfilepath (application. exename) + 'System. ini ');
INI. writeinteger ('pointer ', 'hash', thandle (g_hash ));
INI. Free;
End;
Initialization
G_hash: = thashedstringlist. Create;
Finalization
Freeandnil (g_hash );
End.