unit ULang;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, inifiles, TypInfo,UOsLang;
function InitStringFile():boolean;//初始化字串檔(解檔及取得檔名)
procedure LoadStringFile();//經由初始化過後,再讀取字串檔。
procedure SaveWording(var frm:TForm; sWordingFile:string);
procedure ReadWording(var frm:TForm; sWordingFile:string);
procedure InitGlobalVariable;
function GetWorkDirectory:String;
function GetTmpDirectory(): string ;
var
g_cLang :char;
g_saMsg :array[1..3600] of String; // 訊息字串表
g_sStrFile :string; // 字串檔名
g_sTempPath :string; // (WorkingPath)
g_sWorkPath :string; // (WorkingPath)
implementation
procedure InitGlobalVariable;
begin
g_sWorkPath:=GetWorkDirectory();
g_sTempPath:=GetTmpDirectory();
end;
//初始化字串檔(解檔及取得檔名) by v
function InitStringFile():boolean;
var
sStringFileName:string;
begin
//result:=false;
g_cLang := GetLanguage;// 取得系統語系
if not (g_cLang in ['0','1','2','3']) then
g_cLang := '2' ;// 目前只提供四種語言,內訂英文
sStringFileName:='wording' + g_cLang + '.txt' ;
g_sStrFile:=g_sWorkPath +sStringFileName;
//showmessage(g_sStrFile);
{
//檢查目前執行環境同目錄下,若無字串檔,則解檔案至 temp 路徑下
if not FileExists(g_sStrFile) then
begin
g_sStrFile:=g_sTempPath+sStringFileName;
if( StrIComp(PChar(ExtractDLL(g_sTempPath, sStringFileName, false)),PChar(g_sStrFile)) <> 0 ) then // 解壓縮文字檔
begin
ErrMsgBox('Extract wording file failed !');
exit;
end ;
end;
}
result:=true;
end;
//讀取訊息字串
procedure LoadStringFile();
var
ini:TIniFile;
i:integer;
//sReportLanguageFile:String;
begin
ini := TIniFile.Create(g_sStrFile);
try
for i:=Low(g_saMsg) to High(g_saMsg) do
g_saMsg[i] := ini.ReadString('Message', IntToStr(i), '');
finally
ini.Free;
end;
end;
{=============================================================================
=}
{ 函式敘
述: }
{ 儲存控制項字串
表 }
{=============================================================================
=}
procedure SaveWording(var frm:TForm; sWordingFile:string);
var
i: Integer;
ini: TIniFile;
proInfo: PPropInfo;
sStr: String;
begin
with frm do
begin
ini := TIniFile.Create(sWordingFile);
try
ini.WriteString(Name, Name, Caption);
for i:=0 to ComponentCount-1 do
begin
// caption
proInfo := GetPropInfo(Components[i].ClassInfo, 'Caption');
if (proInfo <> nil) then
begin
sStr := GetStrProp(Components[i], proInfo);
if( Length(sStr) <> 0 ) then
ini.WriteString(Name, Components[i].Name, sStr);
end;
end;
finally
ini.Free;
end;
end;
end;
{=============================================================================
=}
{ 函式敘
述: }
{ 讀取控制項字串表並設定控制項字
串 }
{=============================================================================
=}
procedure ReadWording(var frm:TForm; sWordingFile:string);
var
i: Integer;
ini: TIniFile;
proInfo: PPropInfo;
sStr: String;
FontObject: TFont;
begin
with frm do
begin
frm.PixelsPerInch := 96 ;
frm.Scaled := false ;
ini := TIniFile.Create(sWordingFile);
try
//讀入元件字串
Caption := ini.ReadString(Name, Name, '');
Font.Name:=Screen.MenuFont.Name; //940304 by v 新增
Font.Charset:=Screen.MenuFont.Charset;//940304 by v 新增
Font.Size:=10;
for i:=0 to ComponentCount-1 do
begin
proInfo := GetPropInfo(Components[i].ClassInfo, 'Caption');
if (proInfo <> nil) then
begin
sStr := ini.ReadString(Name, Components[i].Name, GetStrProp
(Components[i], proInfo));
SetStrProp(Components[i], proInfo, sStr);
end;
// 設定字型
proInfo := GetPropInfo(Components[i].ClassInfo, 'Font');
if (proInfo <> nil) then
begin
FontObject := TFont(GetObjectProp(Components[i], proInfo, TFont));
FontObject.Name := Screen.MenuFont.Name;
FontObject.Charset := Screen.MenuFont.Charset;
FontObject.Size := 10;
//為了解決eltree上半部頭會被截掉1pixel的問題 因此更改為12
//FontObject.Size := 10;
//FontObject.
//FontObject.Height := 14;
//FontObject.Size := 8;//20050909 by v 芝瓏說要改8 (效果很差)
end;
end;
finally
ini.Free;
end;
end;
end;
function GetWorkDirectory;
var
aBuf: array[0..260] of Char;
begin
GetModuleFileName(HINSTANCE, @aBuf[0], SizeOf(aBuf));
Result := IncludeTrailingBackslash(ExtractFilePath(StrPas(aBuf)));
end;
function GetTmpDirectory(): string ;
var
dwBuffSize : DWORD ;
cBuffer : array[0..MAX_PATH] of char ;
begin
dwBuffSize := MAX_PATH ;
GetTempPath( dwBuffSize, cBuffer) ;
Result := cBuffer ;
end;
end.
unit UOsLang;
interface
uses
Windows, IniFiles, SysUtils, Registry;
function CreateLogFile:Boolean;
function GetLanguage:Char;
function GetSystemDefaultFontName(FixedFont: Boolean=false): String;
implementation
(*-------------------------------------------------------------------------
取得OS主語系
*)
function PRIMARYLANGID(lgid: WORD): WORD;
begin
Result:=lgid and $03FF;
end;
(*-------------------------------------------------------------------------
取得OS副語系
*)
function SUBLANGID(lgid: WORD): WORD;
begin
Result:=lgid shr 10;
end;
(*-------------------------------------------------------------------------
傳回語系代碼
*)
function GetLanguage(): char;
var
wLangID: LANGID;
wPrimaryLangID, wSubLangID: WORD;
begin
//取得OS預設語系
wLangID:=GetSystemDefaultLangID();
wPrimaryLangID:=PRIMARYLANGID(wLangID);
wSubLangID:=SUBLANGID(wLangID);
case wPrimaryLangID of
//中文...
LANG_CHINESE:
begin
case wSubLangID of
SUBLANG_CHINESE_TRADITIONAL, //繁體
SUBLANG_CHINESE_HONGKONG: //香港也是用繁體
begin
Result:='0';
end;//break
SUBLANG_CHINESE_SIMPLIFIED: //簡體
begin
Result:='1';
end;//break
else
Result:='0'; //預設為繁體中文
end;//case end
end;
//英文...
LANG_ENGLISH:
begin
Result:='2';
end;
//日文...
LANG_JAPANESE:
begin
Result:='3';
end;
//韓文...
LANG_KOREAN:
begin
Result:='4';
end;
//泰文...
LANG_THAI:
begin
Result:='5';
end;
//法文...
LANG_FRENCH:
begin
Result:='9';
end;
//德文...
LANG_GERMAN:
begin
Result:='A';
end;
//義大利文...
LANG_ITALIAN:
begin
Result:='B';
end;
//西班牙文...
LANG_SPANISH:
begin
Result:='C';
end;
//葡萄牙文...
LANG_PORTUGUESE:
begin
Result:='D';
end;
//俄文...
LANG_RUSSIAN:
begin
Result:='F';
end;
//荷蘭文...
LANG_DUTCH:
begin
Result:='I';
end;
//預設語系是英文...
else //defualt
Result:='2';
end;
end;
{===========================================================================}
{ 函式敘述: }
{ 取得系統預設的字型名稱 }
{ 例如:繁體中文Windows => 新細明體 }
{===========================================================================}
function GetSystemDefaultFontName(FixedFont: Boolean=false): String;
const
OEM_CodePage = 'MIME\Database\Codepage\';
ProportionalFont = 'ProportionalFont';
FixWidthFont = 'FixedWidthFont';
var
reg: TRegistry;
CodePage: String;
begin
Result := 'System';
CodePage := IntToStr(GetOEMCP);
reg := TRegistry.Create;
try
reg.RootKey := HKEY_CLASSES_ROOT;
if reg.OpenKeyReadOnly(OEM_CodePage) then
begin
if reg.KeyExists(CodePage) and reg.OpenKeyReadOnly(CodePage) then
begin
if FixedFont then
Result := reg.ReadString(FixWidthFont)
else
if reg.ValueExists(ProportionalFont) then
Result := reg.ReadString(ProportionalFont)
else if reg.ValueExists(FixWidthFont) then
Result := reg.ReadString(FixWidthFont);
end;
end;
finally
reg.CloseKey;
reg.Free;
end;
end;
function CreateLogFile:Boolean;
const
_sLangFile='Language.ini';
var
ini: TIniFile;
begin
Result:=true;
ini:=TIniFile.Create(_sLangFile);
try
try
ini.WriteString('vars','language',GetLanguage);
except
Result:=false;
end;
finally
ini.Free;
end;
end;
end.
然後在你要讀取form的視窗內編寫
uses ULang
然後在form oncreate的地方加上
SaveWording(TForm(self), g_sStrFile);
讀出來已有大部分的元件標題,然後再開啟剛剛在同目錄下已產生的txt檔,並加上未能讀入的字串變數,以後供使用:
[Message]
1=請選擇ini檔案
2=檔案不存在!!!
3=檔案格式錯誤!!!
4=與DB連線失敗'
以上步驟已經存好多國語言的字串,你只要去翻譯這些字串,在另外的wording2.txt、wording3.txt檔案(按照設定的語言的檔案)。
要用時,只要將下列程式碼加入每個form視窗中的oncreate即可。
InitGlobalVariable; //加到主要Form即可
InitStringFile; //加到主要Form即可
LoadStringFile; //加到主要Form即可
ReadWording(TForm(self), g_sStrFile); //每個Form都要加
同時剛有宣告額外字串部分可用下列去呼叫出來:
g_saMsg[1] //其中數字1在[Message]的1的字串
沒有留言:
張貼留言