teetranslate.pas

来自「Delphi TeeChartPro.6.01的源代码」· PAS 代码 · 共 698 行 · 第 1/2 页

PAS
698
字号
{**********************************************}
{   Automatic Translation Unit  -Internal-     }
{   Copyright (c) 2001-2003 by David Berneda   }
{**********************************************}
unit TeeTranslate;
{$I TeeDefs.inc}

interface

uses {$IFNDEF LINUX}
     Windows, 
     {$ENDIF}
     {$IFDEF CLX}
     QGraphics, QForms, QControls, QStdCtrls, QButtons, QExtCtrls,
     {$ELSE}
     Graphics, Forms, Controls, StdCtrls, Buttons, ExtCtrls,
     {$ENDIF}
     SysUtils, Classes;

type
  TAskLanguage = class(TForm)
    OKBtn: TButton;
    CancelBtn: TButton;
    LBLangs: TListBox;
    Label1: TLabel;
    procedure FormCreate(Sender: TObject);
    procedure OKBtnClick(Sender: TObject);
    procedure LBLangsDblClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

{.$DEFINE TEEFINDMISSING}  { <-- internal use }

{$IFDEF TEEFINDMISSING}
{ internal use }
Procedure StartMissing;
Procedure EndMissing;

var Missing:TStringList=nil;
    MissingHelp:TStringList=nil;
    HelpIDList:TStringList=nil;
    MaxHelpContext:Integer=0;

Procedure StartExtract;
Procedure EndExtract;

var Extract:TStringList=nil;
{$ENDIF}

Procedure TeeTranslateControl(AControl:TControl);
Function TeeCanTranslate(Var S,HotKeyList:String):Boolean;

{ Show a dialog to change current Language. Returns True if changed. }
Function TeeAskLanguage:Boolean;

{ Returns the TeeChart Language stored at the Registry }
Function TeeLanguageRegistry:Integer;

{ Stores "Index" TeeChart Language into Registry }
Procedure TeeLanguageSaveRegistry(LanguageNum:Integer);

implementation

Uses {$IFNDEF LINUX}
     Registry,
     {$ELSE}
     IniFiles,
     {$ENDIF}
     {$IFDEF CLX}
     QComCtrls, QMenus,
     {$ELSE}
     ComCtrls, Menus,
     {$ENDIF}
     {$IFDEF TEEFINDMISSING}
     Math,
     {$ENDIF}
     TeCanvas, TeeProcs, TeeConst;

{$IFNDEF CLX}
{$R *.DFM}
{$ELSE}
{$R *.xfm}
{$ENDIF}

Function TeeAskLanguage:Boolean;
begin
  result:=False;
  with TAskLanguage.Create(nil) do
  try
    if ShowModal=mrOk then
       result:=True;
  finally
    Free;
  end;
end;

{$IFDEF TEEFINDMISSING}
{ internal use }
Procedure StartMissing;
const ContextEditorFile='f:\kylix\Docs\helpv6\Context_Editor.txt';

var t: Integer;
begin
  Missing:=TStringList.Create;
  MissingHelp:=TStringList.Create;
  HelpIDList:=TStringList.Create;
  if FileExists(ContextEditorFile) then
     HelpIDList.LoadFromFile(ContextEditorFile);
  MaxHelpContext:=0;
  
  if Assigned(TeeLanguage) then
  for t:=0 to TeeLanguage.Count-1 do
      TeeLanguage.Objects[t]:=Pointer(0);
end;

Procedure EndMissing;
var t: Integer;
begin
  Missing.Add('');
  Missing.Add('Unused:');
  Missing.Add('-------');

  if Assigned(TeeLanguage) then
  for t:=0 to TeeLanguage.Count-1 do
      if Integer(TeeLanguage.Objects[t])=0 then
         Missing.Add(TeeLanguage[t]);

  Missing.Add('-------');

  Missing.SaveToFile('c:\teemissing.txt');
  MissingHelp.Add('');
  MissingHelp.Add('Maximum helpcontext = '+IntToStr(MaxHelpContext));
  MissingHelp.SaveToFile('c:\teemissinghelp.txt');
  FreeAndNil(Missing);
  FreeAndNil(MissingHelp);
  FreeAndNil(HelpIDList);
end;

Procedure StartExtract;
begin
  Extract:=TStringList.Create;
end;

Procedure EndExtract;
begin
  Extract.SaveToFile('c:\teeextract.txt');
  Extract.Free;
end;

Function IsNumber(Const S:String):Boolean;
var Value : Double;
begin
 result:=TryStrToFloat(S,Value);
end;
{$ENDIF}

{$IFNDEF D5}
Const cHotKeyPrefix = '&';
{$ENDIF}

Function TeeCanTranslate(Var S,HotKeyList:String):Boolean;

{$IFNDEF D5}
// From Delphi sources. Delphi 4 does not has GetHotKey function.
function GetHotkey(const Text: string): string;
var
  I, L: Integer;
begin
  Result := '';
  I := 1;
  L := Length(Text);
  while I <= L do
  begin
    if Text[I] in LeadBytes then
      Inc(I)
    else if (Text[I] = cHotkeyPrefix) and
            (L - I >= 1) then
    begin
      Inc(I);
      if Text[I] <> cHotkeyPrefix then
        Result := Text[I]; // keep going there may be another one
    end;
    Inc(I);
  end;
end;
{$ENDIF}

  // Optimized version of TStringList.Values[].
  Function GetTranslatedString(Const St:String):String;
  var t    : Integer;
      i    : Integer;
      tmpS : string;
  begin
    if Assigned(TeeLanguage) then
    with TeeLanguage do
    for t:=0 to Count-1 do
    begin
      tmpS:=Strings[t];
      i:=Pos('=',tmpS);
      if (i>0) and (Copy(tmpS,1,i-1)=St) then
      begin
        result:=Copy(tmpS,i+1,MaxInt);
        {$IFDEF TEEFINDMISSING}
        Objects[t]:=Pointer(Integer(Objects[t])+1);
        {$ENDIF}
        exit;
      end;
    end;
    result:='';
  end;

  {$IFDEF TEEFINDMISSING}
  Function FoundInLanguage(const s:String):Boolean;
  var t:Integer;
  begin
    with TeeLanguage do
    for t:=0 to Count-1 do
    if Pos('='+s,UpperCase(Strings[t]))>0 then
    begin
      result:=true;
      exit;
    end;
    result:=False;
  end;
  {$ENDIF}

var s2         : String;
    s3         : String;
    s4         : String;
    tmpHot     : String;
    HasColon   : Boolean;
    HasHotKey  : Boolean;
    HasEllipse : Boolean;
    t          : Integer;
begin
  result:=False;
  if {$IFDEF TEEFINDMISSING}Assigned(TeeLanguage) and{$ENDIF}
     (s<>'?') and (s<>'%') and (s<>'0') then
  begin
    s:=Trim(s);
    if s<>'' then
    begin
      HasEllipse:=False;
      HasColon:=False;

      { remove hot-key if exists... }
      s4:=StripHotkey(s);
      HasHotKey:=s4<>s;
      if TeeLanguageHotKeyAtEnd and HasHotKey then tmpHot:=GetHotkey(s);

      { ':' }
      s2:=UpperCase(s4);
      if Copy(s2,Length(s2),1)=':' then
      begin
        HasColon:=True;
        s2:=Copy(s2,1,Length(s2)-1);
      end;

      { ... }
      if Copy(s2,Length(s2)-2,3)='...' then
      begin
        HasEllipse:=True;
        s2:=Copy(s2,1,Length(s2)-3);
      end;

      { find new translated string }
      // Optimization of: s3:=TeeLanguage.Values[s2];  { 5.03 }
      s3:=GetTranslatedString(s2);

      if s3<>'' then
      begin
        s:=s3;
        if HasHotKey then { add ampersand (hotkey) to string }
        begin
          if TeeLanguageHotKeyAtEnd then
          begin
            s:=s+'('+cHotKeyPrefix+UpperCase(tmpHot)+')';
          end
          else { default }
          for t:=1 to Length(s) do
          if s[t] in ['a'..'z','A'..'Z','0'..'9'] then
             if Pos(s[t],HotKeyList)=0 then { HotKey char not yet in list }
             begin
               HotKeyList:=HotKeyList+s[t];
               s:=Copy(s,1,t-1)+cHotKeyPrefix+Copy(s,t,Length(s)); { add hotkey }
               break;
             end;
        end;

        { after HotKey, check Ellipse and Colon }
        if HasEllipse then s:=s+'...';
        if HasColon then s:=s+':';
        result:=True;
      end
      else
      begin
       {$IFDEF TEEFINDMISSING}
       // string not found in translation, add to "missing" list...
       if Assigned(Missing) then
          if (s2<>'+') and (s2<>'-') and (s2<>'') and (not IsNumber(s2)) then
             if not FoundInLanguage(s2) then
             begin
               s2:=''''+s2+'=''#13+';
               if Missing.IndexOf(s2)=-1 then
                  Missing.Add(s2);
             end;
       {$ENDIF}
      end;
    end;
  end;
end;

type TControlAccess=class(TControl);

Procedure TeeTranslateControl(AControl:TControl);

 {$IFDEF TEEFINDMISSING}
 Procedure CheckExtract(AComp:TComponent; Const S:String);
 var tmp : String;
 begin
   if Assigned(Extract) and (S<>'') and (not IsNumber(S)) then
   begin
     if AComp.Owner=nil then tmp:='nil'
                        else tmp:=AComp.Owner.ClassName;
      Extract.Add(tmp+#9+AComp.ClassName+#9+AComp.Name+#9+S);
   end;
 end;

 Procedure CheckDuplicates;
 var Num,i,t,tt:Integer;
     s,s2:String;
 begin
   if Assigned(Missing) and (Missing.IndexOf('Duplicates:')=-1)
      and Assigned(TeeLanguage) then
   begin
     Missing.Add('');
     Missing.Add('Duplicates:');
     Missing.Add('---------------');
     for t:=0 to TeeLanguage.Count-1 do
     begin
       Num:=0;
       s:=TeeLanguage.Strings[t];
       i:=Pos('=',s);
       if i>0 then s:=Copy(s,1,i-1);
       for tt:=0 to TeeLanguage.Count-1 do
       begin

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?