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

來源:互聯網
上載者:User

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;

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.