//将某个近件的名称及Caption存到数据库中的某个表中,当取Caption时从这个表读取就行了
ADOConnection属性:指定当前的数据连接
TableName属性:就是上述存放Caption的表,如以下数据
ID
|
Frm_Nme
|
Ctrl_Nme
|
Txt_CN
|
Txt_TW
|
Txt_EN
|
1
|
frmMain
|
frmMain
|
人力资源管理系统
|
人力資源管理系統
|
Human Resource Management System
|
3
|
frmNewDept
|
frmNewDept
|
新增/编辑部门资料
|
新增/編輯部門資料
|
Add/Edit Department information
|
4
|
frmNewDept
|
Label1
|
部门名称
|
部門名稱
|
Department Name
|
5
|
frmNewDept
|
btnOK
|
确定
|
确定
|
OK
|
6
|
frmNewDept
|
btnCancel
|
取消
|
取消
|
Cancel
|
7
|
frmDepartment
|
frmDepartment
|
部门资料管理
|
部門資料管理
|
Department Management
|
8
|
frmDepartment
|
tbNewComp
|
新建公司
|
新建公司
|
Create Company
|
53
|
DM
|
ads_countryCountry_Nme
|
国籍
|
國籍
|
Country
|
54
|
DM
|
ads_titletitle_Nme
|
职务
|
職務
|
Title
|
55
|
DM
|
ads_levelLevel_Nme
|
级别
|
級別
|
Level
|
LanguageField是指当前系统要抓取的语言字段,如果是简体中文,按上表应该就是Txt_CN,如果是繁体中文,则为Txt_TW
组件提供两个方法来设置控件的文本属性
//设置窗口上控件的文本属性
procedure SetControlCaption(theForm: string);
//设置DataModule里的每一个字段的显示文本。(我习惯将数据相关的组件放在TDataModule里)
procedure SetFieldDispLabel(ADM: TDataModule);
//********************调用代码*************************//
//窗口的调用
procedure TBaseForm.FormCreate(Sender: TObject);
begin
DM.MultiLang.SetControlCaption(Self.Name);
end;
//DataModule的调用
procedure TDM.DataModuleCreate(Sender: TObject);
begin
MultiLang.LanguageField := GetSysLanguage;
ADOConnection.Connected := True;
MultiLang.SetFieldDispLabel(Self);
end;
//****************************************************//
//控件源码如下:
unit MultiLang;
interface
uses
SysUtils, Classes, DB, ADODB, Forms, Dialogs, Controls, Menus, ExtCtrls;
type
TMultiLang = class(TComponent)
private
FADOConnection: TADOConnection;
FTableName: string;
FLanguageField: string;
FFormName: string;
procedure SetFormName(const Value: string);
procedure SetLanguageField(const Value: string);
procedure SetTableName(const Value: string);
procedure SetADOConnection(const Value: TADOConnection);
{ Private declarations }
protected
{ Protected declarations }
function GetValue(Command: string): string;
public
{ Public declarations }
procedure SetControlCaption(theForm: string);
procedure SetFieldDispLabel(ADM: TDataModule);
published
{ Published declarations }
property ADOConnection: TADOConnection read FADOConnection write SetADOConnection;
//property FormName: string read FFormName write SetFormName;
property TableName: string read FTableName write SetTableName;
property LanguageField: string read FLanguageField write SetLanguageField;
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('Standard', [TMultiLang]);
end;
{ TComponent1 }
function TMultiLang.GetValue(Command: string): string;
var
tmp: TADOQuery;
begin
tmp := TADOQuery.Create(Self);
try
tmp.Connection := Self.ADOConnection;
with tmp do
begin
Close;
SQL.Clear;
SQL.Add(Command);
//ShowMessage(Command);
Open;
Result := Fields[0].AsString;
end;
finally
tmp.Free;
end;
end;
procedure TMultiLang.SetADOConnection(const Value: TADOConnection);
begin
FADOConnection := Value;
end;
procedure TMultiLang.SetControlCaption(theForm: string);
var
I, J: integer;
AForm: TForm;
Capt, ctrl_nme, S: string;
begin
if not Assigned(FADOConnection) then
begin
MessageDlg('Missing ADOConnection property', mtError, [MBOK], 0);
Exit;
end;
if FTableName = '' then
begin
MessageDlg('Missing TableName property', mtError, [MBOK], 0);
Exit;
end;
{if FFormName = '' then
begin
MessageDlg('Missing FormName property', mtError, [MBOK], 0);
Exit;
end; }
if FLanguageField = '' then
begin
MessageDlg('Missing LanguageField property', mtError, [MBOK], 0);
Exit;
end;
//窗口標題
S := GetValue('SELECT ' + FLanguageField + ' FROM '
+ FTableName + ' WHERE Frm_Nme=' + QuotedStr(theForm)
+' and Ctrl_Nme=' + QuotedStr(theForm));
//
for I := 0 to Screen.FormCount - 1 do
begin
if UpperCase(Screen.Forms[I].Name) = UpperCase(theForm) then
begin
AForm := Screen.Forms[I];
if S <> '' then
//ShowMessage(S);
AForm.Caption := S;
for J := 0 to AForm.ComponentCount - 1 do
begin
Ctrl_Nme := AForm.Components[J].Name;
Capt := GetValue('SELECT ' + FLanguageField + ' FROM '
+ FTableName + ' WHERE Frm_Nme=' + QuotedStr(theForm)
+' and Ctrl_Nme=' + QuotedStr(Ctrl_Nme));
//ShowMessage(ctrl_nme+':'+capt);
if Capt <> '' then
begin
if AForm.Components[J] is TMenuItem then
TMenuItem(AForm.Components[J]).Caption := Capt
else
if AForm.Components[J] is TField then
TField(AForm.Components[J]).DisplayLabel := Capt
else
//if AForm.Components[J] is TImageList then
TControl(AForm.Components[J]).SetTextBuf(Pchar(Capt));
end;
end;
Exit;
end;
end;
end;
procedure TMultiLang.SetFieldDispLabel(ADM: TDataModule);
var
I: integer;
Nme: string;
Capt: string;
begin
for I := 0 to ADM.ComponentCount - 1 do
begin
if ADM.Components[I] is TField then
begin
Nme := TField(ADM.Components[I]).Name;
Capt := GetValue('SELECT ' + FLanguageField + ' FROM '
+ FTableName + ' WHERE Frm_Nme=' + QuotedStr(ADM.Name)
+' and Ctrl_Nme=' + QuotedStr(Nme));
if Capt <> '' then
TField(ADM.Components[I]).DisplayLabel := Capt;
end;
end;
end;
procedure TMultiLang.SetFormName(const Value: string);
begin
FFormName := Value;
end;
procedure TMultiLang.SetLanguageField(const Value: string);
begin
FLanguageField := Value;
end;
procedure TMultiLang.SetTableName(const Value: string);
begin
FTableName := Value;
end;
end.