//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::LoadCaptions(TWinControl *Container, TIniFile *LangFile) //利用控件数组实现文字转换
{
for (int i = 0; i < Container->ControlCount; i++)
{
if (dynamic_cast <TButton*>(Container->Controls[i])) // 包含在Form上如果是TButton按钮
{
TButton& ref_obj= dynamic_cast<TButton&>(*Container->Controls[i]);
String NewStr = LangFile->ReadString(Container->Name, ref_obj.Name, "");
if (strcmp(NewStr.c_str(), "")) ref_obj.Caption = NewStr;
}
if (dynamic_cast <TLabel*>(Container->Controls[i])) // 包含在Form上如果是 TLabel标签
{
TLabel& ref_obj = dynamic_cast<TLabel&>(*Container->Controls[i]);
String NewStr = LangFile->ReadString(Container->Name, ref_obj.Name, "");
if (strcmp(NewStr.c_str(), "")) ref_obj.Caption = NewStr;
}
else if (dynamic_cast <TGroupBox*>(Container->Controls[i])) //包含在GroupBox上的控件
{ // if it's a TGroupBox then load the panel's caption if any...
TGroupBox& ref_obj = dynamic_cast<TGroupBox&>(*Container->Controls[i]);
String NewStr = LangFile->ReadString(Container->Name, ref_obj.Name, "");
if (strcmp(NewStr.c_str(), "")) ref_obj.Caption = NewStr;
// and then call LoadCaptions() recursively to iterate through any
// objects contained in the GroupBox and update their captions or text...
LoadCaptions(&ref_obj, LangFile);
}
}
}
void __fastcall TForm1::RefreshCaptions(void)
{ if(Language=="中文") Language="Chinese";
else if(Language=="英文") Language="English";
ComponentNames->Clear();
Captions->Clear();
TIniFile * NewCaptions = NULL;
// Build our new captions filename...
String CaptionsFile = ExtractFilePath(Application->ExeName)+ Language + "_Captions.con";
// check to see if it exists in our application directory...
if (FileExists(CaptionsFile))
{
// if the file exists then create an instance of the inifile class
// and open the file for reading...
try
{
NewCaptions = new TIniFile(CaptionsFile);
}
catch(...)
{
ShowMessage("Error opening the " + Language + " captions file!");
}
}
else
{
// if the file doesn't exist then notify the user...
MessageDlg("Could not find " + Language + "_Captions.con" + " !!!",
mtError, TMsgDlgButtons() << mbOK, 0);
}
// if NewCaptions is valid then...
if (NewCaptions)
{
// Get the mainform and menu captions...
Form1->Caption = NewCaptions->ReadString("Form1", "FormCaption",
"Localizer"); //默认窗口标题Localizer FormCaption作为一个变量,详见.con文件内
LangMenu->Caption = NewCaptions->ReadString("Form1", "LangMenu",
"Language"); //默认语言菜单项标题Language
Form2->Caption=NewCaptions->ReadString("Form2","FormCaption","test2");
LangMenu->Items[0]->Caption=NewCaptions->ReadString("Form1", "ChildMenuName0","English");
LangMenu->Items[1]->Caption=NewCaptions->ReadString("Form1", "ChildMenuName1","Chinese");
// Step through all components on the form. If they have an entry in the
// NewCaptions file then load the value from the file into the caption of
// the component...
LoadCaptions(Form1, NewCaptions);
LoadCaptions(Form2, NewCaptions);
delete NewCaptions;
}
}
void __fastcall TForm1::OnLanguageSelection(TObject *Sender)
{
Language = ((TMenuItem*)Sender)->Caption;
RefreshCaptions();
}
void __fastcall TForm1::FindLanguages(void)
{
// searches the default directory for language files and
// creates a new menu item for each language it finds...
TMenuItem *LangMenu, *NewLangSelection;
TSearchRec sr;
// This is really unnecessary. Since we only have one top level menu item
// we could just set LangMenu = MainMenu1->Items->Items[0];
LangMenu = MainMenu1->Items->Items[MainMenu1->Items->Count - 1];
if (FindFirst("*_Captions.con", 0, sr) == 0)
{
// Create a new menu item...
NewLangSelection = new TMenuItem(LangMenu);
// Set it's caption to the first part of the language file's filename.
// (the part up to the underscore).
NewLangSelection->Caption = sr.Name.SubString(1, sr.Name.Pos("_") -1);
// se the new menu item's OnClick event to OnLanguageSelection.
NewLangSelection->OnClick = OnLanguageSelection;
// Insert the new menu item into the MainMenu
LangMenu->Insert(LangMenu->Count, NewLangSelection);
while (FindNext(sr) == 0)
{
// Do it all over again until we run out of language files.
NewLangSelection = new TMenuItem(LangMenu);
NewLangSelection->Caption = sr.Name.SubString(1, sr.Name.Pos("_") -1);
NewLangSelection->OnClick = OnLanguageSelection;
LangMenu->Insert(LangMenu->Count, NewLangSelection);
}
FindClose(sr);
}
}
void __fastcall TForm1::FormCreate(TObject *Sender)
{ ComponentNames = new TStringList;
Captions = new TStringList;