📄 jvqtranslator.pas
字号:
{******************************************************************************}
{* WARNING: JEDI VCL To CLX Converter generated unit. *}
{* Manual modifications will be lost on next release. *}
{******************************************************************************}
{-----------------------------------------------------------------------------
The contents of this file are subject to the Mozilla Public License
Version 1.1 (the "License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.mozilla.org/MPL/MPL-1.1.html
Software distributed under the License is distributed on an "AS IS" basis,
WITHOUT WARRANTY OF ANY KIND, either expressed or implied. See the License for
the specific language governing rights and limitations under the License.
The Original Code is: JvTranslator.PAS, released on 2002-06-03
The Initial Developer of the Original Code is S閎astien Buysse [sbuysse att buypin dott com]
Portions created by S閎astien Buysse are Copyright (C) 2001 S閎astien Buysse.
All Rights Reserved.
Contributor(s): _________________________________.
You may retrieve the latest version of this file at the Project JEDI's JVCL home page,
located at http://jvcl.sourceforge.net
Known Issues:
-----------------------------------------------------------------------------}
// $Id: JvQTranslator.pas,v 1.18 2005/02/06 14:06:17 asnepvangers Exp $
unit JvQTranslator;
{$I jvcl.inc}
interface
uses
SysUtils, Classes, IniFiles,
QForms, QComCtrls, QMenus, QDialogs,
JvQSimpleXml, JvQComponent;
type
TJvTranslator = class(TJvComponent)
private
FXML: TJvSimpleXml;
FSkipList: TList;
function IsObject(const Obj: TClass; const ClassName: string): Boolean;
protected
function FindItemNamed(Root: TJvSimpleXMLElem; const AName: string;
ARecurse: Boolean = False): TJvSimpleXMLElem; virtual;
procedure TranslateComponent(const Component: TComponent; const Elem: TJvSimpleXMLElem); virtual;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
// Call SkipClass to register a class to skip when reading/writing
procedure SkipClass(AClass: TClass);
// Call UnskipClass to unregister a class so it won't be skip when reading/writing
procedure UnskipClass(AClass: TClass);
// Call SkipProperty to register a class property to skip when reading/writing
// If UnskipClass has already been called for this class, does nothing
procedure SkipProperty(AClass: TClass; const PropName: string);
// Call UnskipProperty to unregister a class property so it won't be skipped when reading/writing
// If SkipClass has already been called for this class, does nothing
procedure UnskipProperty(AClass: TClass; const PropName: string);
// Returns True if the specifed class/object/property is in the skip list
function InSkipList(AClass: TClass): Boolean; overload;
function InSkipList(Obj: TObject): Boolean; overload;
function InSkipList(AClass: TClass; const PropName: string): Boolean; overload;
function InSkipList(Obj: TObject; const PropName: string): Boolean; overload;
procedure ClearSkipList;
// ComponentToXML converts a TComponent and, optionally, it's owned components to an XML string
// and returns it
function ComponentToXML(const AComponent: TComponent; Recurse: Boolean): string;
// Translate the entire Application using the file Filename
procedure Translate(const FileName: string); overload;
// Translate the entire Application using a stream
procedure Translate(const Stream: TStream); overload;
// Translate the entire Application using a string
procedure TranslateString(const S: string); overload;
// Translate a form using the file Filename
procedure Translate(const FileName: string; const Form: TCustomForm); overload;
// Translate a form using a stream
procedure Translate(const Stream: TStream; const Form: TCustomForm); overload;
// Translate a form using a string
procedure TranslateString(const S: string; const Form: TCustomForm); overload;
// Translate a form using the currently loaded XML (wherever it came from)
procedure Translate(const Form: TCustomForm); overload;
// Translates all form instances owned by the global screen object using the file Filename
procedure TranslateScreen(const FileName: string); overload;
// Translates all form instances owned by the global screen object using a stream
procedure TranslateScreen(const Stream: TStream); overload;
// Translates all form instances owned by the global screen object using a string
procedure TranslateScreenString(const S: string);
// Returns the value of a node or a property value of a node based on certain search criteria.
// To find the value, the method first searches the root for a subnode with the name in Category.
// If found, Category is searched for a subnode with the name in Item. If found, either the value
// of Item or the value of a property named "Value" in Item is returned.
// Structurally it should look something like this:
// <Root>
// <Category>
// <Item Value="PropValue">Value</Item>
// </Category>
// ....
// This method returns either Value or, if not found, PropValue or, if not found, an empty string
function Translate(const Category, Item: string): string; overload;
property XML: TJvSimpleXml read FXML;
end;
TJvTranslatorStrings = class(TJvComponent)
private
FList: THashedStringList;
function GetString(Index: Integer): string;
procedure SetString(Index: Integer; const Value: string);
function GetCount: Integer;
function GetValue(Index: Integer): string;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
function IndexOf(const Name: string): Integer;
function Add(const Name: string; var Value: string): Integer;
// (p3) this is weird: GetString returns the *Name* but SetString sets the *Value*...
property Strings[Index: Integer]: string read GetString write SetString; default;
property Value[Index: Integer]: string read GetValue;
property Count: Integer read GetCount;
end;
implementation
uses
{$IFDEF UNITVERSIONING}
JclUnitVersioning,
{$ENDIF UNITVERSIONING}
TypInfo,
JvQConsts;
const
cName = 'Name';
cItem = 'Item';
cIndex = 'Index';
cColumn = 'Column';
cValue = 'Value';
cVariables = 'Variables';
cTTreeNodes = 'TTreeNodes';
cTListItems = 'TListItems';
cTStrings = 'TStrings';
cTCollection = 'TCollection';
cTComponent = 'TComponent';
cTJvTranslatorStrings = 'TJvTranslatorStrings';
cNewline = '\n';
type
PSkipPropRec = ^TSkipPropRec;
TSkipPropRec = record
AClass: TClass;
AProps: TStringList;
end;
function InternalGetWideStrProp(Instance: TObject; const PropName: string): WideString; overload;
begin
Result := GetWideStrProp(Instance, PropName);
end;
function InternalGetPropList(AObject: TObject; out PropList: PPropList): Integer;
begin
Result := GetPropList(AObject, PropList);
end;
//=== { TJvTranslator } ======================================================
constructor TJvTranslator.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FXML := TJvSimpleXml.Create(nil);
SkipProperty(TComponent, cName);
end;
destructor TJvTranslator.Destroy;
begin
FXML.Free;
ClearSkipList;
FreeAndNil(FSkipList);
inherited Destroy;
end;
function TJvTranslator.FindItemNamed(Root: TJvSimpleXMLElem; const AName: string; ARecurse: Boolean): TJvSimpleXMLElem;
var
I: Integer;
begin
Result := nil;
if Root = nil then
Root := FXML.Root;
if AnsiSameText(Root.Name, AName) then
Result := Root
else
if not ARecurse then
Result := Root.Items.ItemNamed[AName]
else
for I := 0 to Root.Items.Count - 1 do
begin
Result := FindItemNamed(Root.Items[I], AName, True);
if Result <> nil then
Break;
end;
end;
function TJvTranslator.IsObject(const Obj: TClass; const ClassName: string): Boolean;
begin
if Obj = nil then
Result := False
else
Result := SameText(Obj.ClassName, ClassName) or (IsObject(Obj.ClassParent, ClassName));
end;
function TJvTranslator.ComponentToXML(const AComponent: TComponent; Recurse: Boolean): string;
var
AName: string;
AElem: TJvSimpleXMLElem;
procedure CollectionToXML(Collection: TCollection; Elem: TJvSimpleXMLElem; Recurse:Boolean); forward;
procedure TreeNodesToXML(Nodes: TTreeNodes; Elem: TJvSimpleXMLElem);
var
N: TTreeNode;
AElem: TJvSimpleXMLElem;
begin
// format: <Items>
// <Item Index="" Value="" />
// TODO
if InSkipList(Nodes) then Exit;
N := Nodes.GetFirstNode;
while Assigned(N) do
begin
if not InSkipList(N) then
begin
AElem := Elem.Items.Add(cItem);
AElem.Properties.Add(cIndex, N.Index);
AElem.Properties.Add(cValue, N.Text);
end;
{
AElem.Properties.Add('ImageIndex',N.ImageIndex);
AElem.Properties.Add('SelectedIndex',N.SelectedIndex);
}
N := N.GetNext;
end;
end;
procedure ListItemsToXML(Items: TListItems; Elem: TJvSimpleXMLElem);
var
I, J: Integer;
AElem: TJvSimpleXMLElem;
begin
// format: <Items>
// <Item Index="" Column="" Value="" />
// TODO
if InSkipList(Items) then Exit;
for I := 0 to Items.Count - 1 do
begin
if not InSkipList(Items[I]) then
begin
AElem := Elem.Items.Add(cItem);
AElem.Properties.Add(cIndex, I);
AElem.Properties.Add(cColumn, 0);
AElem.Properties.Add(cValue, Items[I].Caption);
for J := 0 to Items[I].SubItems.Count - 1 do
begin
AElem := Elem.Items.Add(cItem);
AElem.Properties.Add(cIndex, I);
AElem.Properties.Add(cColumn, J + 1);
AElem.Properties.Add(cValue, Items[I].SubItems[J]);
end;
end;
end;
end;
procedure StringsToXML(Strings: TStrings; Elem: TJvSimpleXMLElem);
var
I: Integer;
AElem: TJvSimpleXMLElem;
begin
// format: <Items>
// <Item Index="" Value="" />
if InSkipList(Strings) then Exit;
for I := 0 to Strings.Count - 1 do
begin
AElem := Elem.Items.Add(cItem);
AElem.Properties.Add(cIndex, I);
AElem.Properties.Add(cValue, Strings[I]);
end;
end;
procedure TranslatorStringsToXML(AStrings: TJvTranslatorStrings; Elem: TJvSimpleXMLElem);
var
I: Integer;
AElem: TJvSimpleXMLElem;
begin
// I'm not sure how to create a translation template for this component, so this is just a guess...
// format:
// <Variables>
// <Item Name="" Value="" />
// </Variables>
if InSkipList(AStrings) then Exit;
Elem.Name := cVariables;
for I := 0 to AStrings.Count - 1 do
begin
AElem := Elem.Items.Add(cItem);
AElem.Properties.Add(cName, AStrings[I]);
AElem.Properties.Add(cValue, AStrings.Value[I]);
end;
end;
(*
procedure ObjectToXML(AnObject: TObject; Elem: TJvSimpleXMLElem);
var
J, Count: Integer;
PropList: PPropList;
PropName: string;
PropInfo: PPropInfo;
AnObj: TObject;
begin
if (AnObject <> nil) and not InSkipList(AnObject) then
begin
Count := InternalGetPropList(AnObject, PropList);
for J := 0 to Count - 1 do
begin
PropInfo := PropList[J];
PropName := PropInfo^.Name;
try
if (PropInfo^.SetProc = nil) or InSkipList(AnObject, PropName) then
Continue;
case PropInfo^.PropType^.Kind of
tkInteger:
Elem.Properties.Add(PropName, GetOrdProp(AnObject, PropName));
tkEnumeration:
Elem.Properties.Add(PropName, GetEnumProp(AnObject, PropName));
tkSet:
Elem.Properties.Add(PropName, GetSetProp(AnObject, PropName));
tkString, tkLString:
Elem.Properties.Add(PropName, GetStrProp(AnObject, PropName));
tkClass:
begin
AnObj := GetObjectProp(AnObject, PropName);
if IsObject(AnObj.ClassType, cTTreeNodes) then
TreeNodesToXML(TTreeNodes(AnObj), Elem.Items.Add(PropName))
else
if IsObject(AnObj.ClassType, cTListItems) then
ListItemsToXML(TListItems(AnObj), Elem.Items.Add(PropName))
else
if IsObject(AnObj.ClassType, cTStrings) then
StringsToXML(TStrings(AnObj), Elem.Items.Add(PropName))
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -