unit XliffPlainExporter;
interface
uses
Document,
SysUtils, Variants,
Classes;
type
TXliffPlainExporter =
class( TInterfacedObject, IDocumentExportFileStrategy )
private
procedure WriteLine( AStream : TStream; ALine :
string );
protected
procedure WriteHead( AContext : TDocument; AStream : TStream );
procedure WriteDetails( AContext : TDocument; AStream : TStream );
procedure WriteFoot( AContext : TDocument; AStream : TStream );
public
procedure ExportDoc( AContext : TDocument;
const AFileName :
string );
end;
implementation
{ TXmlExporter }
procedure TXliffPlainExporter.ExportDoc( AContext : TDocument;
const AFileName :
string );
var
LStream : TStream;
LFileName :
string;
begin
AContext.First;
if not AContext.Eof
then
begin
LFileName := AFileName;
if ExtractFileExt( LFileName ) = '
'
then
LFileName := ChangeFileExt( LFileName, '
.xml' );
LStream := TFileStream.Create( LFileName, fmCreate );
try
WriteHead( AContext, LStream );
WriteDetails( AContext, LStream );
WriteFoot( AContext, LStream );
finally
LStream.Free;
end;
end;
end;
procedure TXliffPlainExporter.WriteHead( AContext : TDocument; AStream : TStream );
begin
WriteLine( AStream, '
<?xml version=''
1.0''
encoding=''
utf-8''
?>' );
WriteLine( AStream, '
<xliff version="1.1">' );
WriteLine( AStream, '
<file original="source\simple.htm" source-language="EN" target-language="DE" datatype="html">' );
WriteLine( AStream, '
<header>' );
WriteLine( AStream, '
<skl>' );
WriteLine( AStream, '
<external-file uid="017dbcf0-c82c-11e2-ba2b-005056c00008" href="skl\simple.htm.skl"/>' );
WriteLine( AStream, '
</skl>' );
WriteLine( AStream, '
</header>' );
WriteLine( AStream, '
<body>' );
end;
procedure TXliffPlainExporter.WriteDetails( AContext : TDocument; AStream : TStream );
begin
while not AContext.Eof
do
begin
WriteLine( AStream, Format( '
<trans-unit id="%s">', [VarToStr( AContext.Value['
id'] )] ) );
WriteLine( AStream, Format( '
<source xml:lang="EN">%s</source>', [VarToStr( AContext.Value['
src'] )] ) );
WriteLine( AStream, Format( '
<target xml:lang="DE">%s</target>', [VarToStr( AContext.Value['
dst'] )] ) );
WriteLine( AStream, '
<note/></trans-unit>' );
AContext.Next;
end;
end;
procedure TXliffPlainExporter.WriteFoot( AContext : TDocument; AStream : TStream );
begin
WriteLine( AStream, '
</body>' );
WriteLine( AStream, '
</file>' );
WriteLine( AStream, '
</xliff>' );
end;
procedure TXliffPlainExporter.WriteLine( AStream : TStream; ALine :
string );
var
LLine : TStream;
begin
LLine := TStringStream.Create( ALine + sLineBreak, TEncoding.UTF8 );
try
LLine.Position := 0;
AStream.CopyFrom( LLine, LLine.Size );
finally
LLine.Free;
end;
end;
end.