Use the INI file to implement non-flashing multi-language switching

Source: Internet
Author: User

{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
, Advglowbutton, advnavbar, advofficepager
;
// 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 );

Implementation

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 owners. 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 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 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 tadvglowbutton then
Begin
Writestring (owner. Name, C. Name + '. Caption', (C as tadvglowbutton). Caption );
Writestring (owner. Name, C. Name + '. hint', (C as tadvglowbutton). Hint );
End else
If C is tadvofficepage then
Begin
Writestring (owner. Name, C. Name + '. Caption', (C as tadvofficepage). Caption );
Writestring (owner. Name, C. Name + '. hint', (C as tadvofficepage). Hint );
End else
If C is tadvnavbarpanel then
Begin
Writestring (owner. Name, C. Name + '. Caption', (C as tadvnavbarpanel). Caption );
End;
End;
// Process string variables
// M1: = readstring (messages, 'm1 ', M1 );
End;
End;

Procedure setactivelanguage (owner: tform; const extends agename: string );
Const
Messages = 'messages '; // string variable section
VaR
C: tcomponent;
I: integer;
Begin
If owner = nil then exit;
With Tinifile. Create (extractfilepath (application. exename) + 'language/'+ languagename +'. ini ') Do
Begin
Owner. Caption: = readstring (owner. Name, owner. Name + '. Caption', owner. Caption );
For I: = 0 to owners. 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 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 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 tadvglowbutton then
Begin
(C as tadvglowbutton). Caption: = readstring (owner. Name, C. Name + '. Caption', (C as tadvglowbutton). Caption );
(C as tadvglowbutton). Hint: = readstring (owner. Name, C. Name + '. hint', (C as tadvglowbutton). Hint );
End else
If C is tadvofficepage then
Begin
(C as tadvofficepage). Caption: = readstring (owner. Name, C. Name + '. Caption', (C as tadvofficepage). Caption );
(C as tadvofficepage). Hint: = readstring (owner. Name, C. Name + '. hint', (C as tadvofficepage). Hint );
End else
If C is tadvnavbarpanel then
Begin
(C as tadvnavbarpanel). Caption: = readstring (owner. Name, C. Name + '. Caption', (C as tadvnavbarpanel). Caption );
End;
End;
// Process string variables
// M1: = readstring (messages, 'm1 ', M1 );
End;
End;

End.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.