BCB多语言版本软件的实现

在BCB中实现多语言设计的方法可能有多种多样,在此我通过一个实例讲解一下我个人的实现思路: 

1、首先必须挖掘程序中所使用的到文字资源将其统一整理出来,再对应翻译成各种语言,我这里就以:中文简体,中文繁体,英语为例;建立语言资源文件:lang.ini  具体内容如下:

[ set ]
val
= 3

[chs]
title
= 程序重启测试(设计:彭学周)
topic
= 语言设置
option1
= 中文简体
option2
= 中文繁体
option3
= 英文
btn1
= 确 定
btn2
= 取 消

[cht]
title
= 程式重啟測試(設計:彭學周)
topic
= 語言設置
option1
= 中文簡體
option2
= 中文繁體
option3
= 英文
btn1
= 確 定
btn2
= 取 消

[eng]
title
= Program reload test(By:Favory)
topic
= Language Setting
option1
= Chinese Simplified
option2
= Chinese Traditional
option3
= English
btn1
= Confirm
btn2
= Cancel

2:在程序中设置读取,根据选择的语言,读取对应的文本内容,对界面中的现实元素进行赋值,以此来改变语言显示;代码如下:

TForm1  * Form1;
bool  g_bIsRunAgain  =   false ;
int  lval = 1 ;
TIniFile   
* pIniFile;
// ---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent *  Owner)
        : TForm(Owner)
{
  pIniFile
=new TIniFile(ChangeFileExt(ExtractFilePath(Application->ExeName), "lang.ini"));
  AnsiString  lang
="chs";
  lval
=pIniFile->ReadInteger("set","val",1);
  
switch (lval)
  
{
    
case 1 :
          rb1
->Checked=true;
          lang
="chs";
          
break;
    
case 2 :
          rb2
->Checked=true;
          lang
="cht";
          
break;
    
case 3 :
          rb3
->Checked=true;
          lang
="eng";
          
break;
  }

  
this->Caption=pIniFile->ReadString(lang,"title","程序重启测试");
  grp1
->Caption=pIniFile->ReadString(lang,"topic","语言设置");
  rb1
->Caption=pIniFile->ReadString(lang,"option1","中文简体");
  rb2
->Caption=pIniFile->ReadString(lang,"option2","中文繁体");
  rb3
->Caption=pIniFile->ReadString(lang,"option3","英文");
  btn1
->Caption=pIniFile->ReadString(lang,"btn1","确 定");
  btn2
->Caption=pIniFile->ReadString(lang,"btn2","取 消");
//  delete   pIniFile;
}

// ---------------------------------------------------------------------------

void  __fastcall TForm1::btn2Click(TObject  * Sender)
{
    Close();
}

// ---------------------------------------------------------------------------
void  __fastcall TForm1::btn1Click(TObject  * Sender)
{
     
if(rb1->Checked) lval=1;
     
if(rb2->Checked) lval=2;
     
if(rb3->Checked) lval=3;
     pIniFile
->WriteInteger("set","val",lval);
     g_bIsRunAgain 
= true;
     Close();
}

3.、由于语言的加载放在初始化中进行的,所以切换语言后必须重新启动程序,为了让程序在点击确定改变语言按钮时关闭后自动启动来实现跟新语言的方法,所以必须修改Project1.cpp中的WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)函数,代码如下:

WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR,  int )
{
        
try
        
{
                 Application
->Initialize();
                 Application
->CreateForm(__classid(TForm1), &Form1);
                 Application
->Run();
        }

        
catch (Exception &exception)
        
{
                 Application
->ShowException(&exception);
        }

        
catch (...)
        
{
                 
try
                 
{
                         
throw Exception("");
                 }

                 
catch (Exception &exception)
                 
{
                         Application
->ShowException(&exception);
                 }

        }

        
if(g_bIsRunAgain)
        
{
            AnsiString strPath;
            STARTUPINFO StartInfo;
            PROCESS_INFORMATION procStruct;
            memset(
&StartInfo, 0sizeof(STARTUPINFO));
            StartInfo.cb 
= sizeof(STARTUPINFO);
            strPath 
= Application->ExeName;
            
if(!::CreateProcess(
                    (LPCTSTR) strPath.c_str(),
                    NULL,
                    NULL,
                    NULL,
                    FALSE,
                    NORMAL_PRIORITY_CLASS,
                    NULL,
                    NULL,
                    
&StartInfo,
                    
&procStruct))
                
return 0;
        }

        
return 0;
}

4、运行效果:

BCB多语言版本软件的实现_第1张图片



BCB多语言版本软件的实现_第2张图片

 

你可能感兴趣的:(exception,测试,null,application,语言,winapi)