枚舉並設置界面的Caption之多語言方法

uses
  ..., TypInfo;

procedure TForm1.FormCreate(Sender: TObject);
var
  i: Integer;
  aComponent: TComponent;
  PropInfo: PPropInfo;
  ViewText, ViewHint: string;
begin
  ViewText := 'Hello world'; // for debug use, you should comment these 2 lines
  ViewHint := 'the hint';    //   and then implement GetInfoByName method.

  for i := 0 to ComponentCount - 1 do
  begin
    aComponent := Components[i];
    PropInfo := GetPropInfo(aComponent.ClassInfo, 'Caption');
    if not Assigned(PropInfo) then // Caption property of aComponent NOT exists
    begin
      // turn to search Text property
      PropInfo := GetPropInfo(aComponent.ClassInfo, 'Text');
      if not Assigned(PropInfo) then // Text property of aComponent NOT exists
        Continue;
    end;

    // comes here, either Caption or Text property must exists
    // first, find the ViewText and ViewHint of the aComponent by the name
    // GetInfoByName(aComponent.Name, ViewText, ViewHint);
    SetStrProp(aComponent, PropInfo, ViewText);

    // then, search Hint property
    PropInfo := GetPropInfo(aComponent.ClassInfo, 'Hint');
    if Assigned(PropInfo) then
      SetStrProp(aComponent, PropInfo, ViewHint);
  end;
end;


****************************************************************************************
Function IsAPropExist(AInst: TObject; const PropName: string): Boolean;
var
  PropInfo: PPropInfo;
begin
  Result := False;
  PropInfo := GetPropInfo(AInst, PropName);
  if PropInfo <> nil then
    Result := PropInfo.Name = PropName;
end;


procedure SetLang;
var
  i: Integer;
  StrValue: string;
  StrObj: string;
  StrCaption :String;
begin
  for i := 0 to Self.ComponentCount - 1 do
  begin
    if IsAPropExist(Self.Components[i], 'Caption') then
    begin
      StrValue := VarToStr(GetPropValue(self.Components[i], 'Caption'));
      if (StrValue = '') or (StrValue = '-') then
        Continue;
      StrCaption := rwIni1.iReadString(self.ClassName, Self.Components[i].Name,'');
      if Trim(strCaption) <> '' then Continue;
      rwIni1.iWriteString(self.ClassName, Self.Components[i].Name, StrValue);
    end;
  end;
end;

procedure ReadLang;
var
  i: Integer;
  StrValue: string;
  StrObj: string;
  StrCaption: string;
begin
  for i := 0 to Self.ComponentCount - 1 do
  begin
    if IsAPropExist(Self.Components[i], 'Caption') then
    begin
      StrValue := VarToStr(GetPropValue(self.Components[i], 'Caption'));
      StrObj := Trim(Self.Components[i].Name);
      if (StrObj = '') or (strObj = '-') then
        Continue;
      StrCaption := rwIni1.iReadString(self.ClassName, Self.Components[i].Name, StrValue);
      if Trim(strCaption)='' then Continue;
      SetPropValue(Self.Components[i], 'Caption', StrCaption);
    end;
  end;
end;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值