const
BaseFile:
string = '
Language.EN.pas';
var
i, j, LineIndexAllocation, LineIndexEndOfAssignment, PosStart, PosEnd: Integer;
LLines, LLinesExport: TStringList;
s, LUnitName, LFileToParse, LFileExport, LLine, LComponentOrVariable, LLineContentFilterted, msgcommentline, msgstrline, msgid, msgstr:
string;
BAllocationFound, BEndOfAssignmentFound: Boolean;
Arr: TArray<
string>;
procedure AddDataToPOFile(Str:
string);
begin
Str := Trim(Str);
if IsHeader
or (Pos('
''
', Str) > 0)
then
begin
msgcommentline := '
#. unit: ' + LUnitName + '
, ' + LComponentOrVariable;
msgstrline := '
# ' + BaseFile + '
:' + (LineIndexAllocation + 1).ToString;
msgid := Str;
msgstr := Str;
LLinesExport.Add(msgcommentline);
LLinesExport.Add(msgstrline);
LLinesExport.Add('
msgid "' + msgid + '
"');
LLinesExport.Add('
msgstr ""');
LLinesExport.Add('
');
end;
end;
begin
LLines := TStringList.Create;
try
LFileToParse := Path + BaseFile;
if FileExists(LFileToParse)
then
begin
LFileExport := '
Lang-export.po';
LLines.LoadFromFile(LFileToParse);
if LLines.Count > 0
then
begin
LLinesExport := TStringList.Create;
try
LLinesExport.Add('
msgid ""');
LLinesExport.Add('
msgstr ""');
LLinesExport.Add('
"POT-Creation-Date: \n"');
LLinesExport.Add('
"PO-Revision-Date: \n"');
LLinesExport.Add('
"Last-Translator: \n"');
LLinesExport.Add('
"MIME-Version: 1.0\n"');
LLinesExport.Add('
"Content-Type: text/plain; charset=iso-8859-1\n"');
LLinesExport.Add('
"Content-Transfer-Encoding: 8bit\n"');
LLinesExport.Add('
"Language: en\n"');
LLinesExport.Add('
"X-Generator: NAME\n"');
LLinesExport.Add('
');
LLinesExport.Add('
# BASE FILE for translating NAME into other languages');
LLinesExport.Add('
# Copyright (C) 1234 NAME');
LLinesExport.Add('
# This file is distributed under the same license as NAME');
LLinesExport.Add('
# NAME, 1234.');
LLinesExport.Add('
# header end');
LLinesExport.Add('
');
BAllocationFound := False;
BEndOfAssignmentFound := False;
LineIndexAllocation := -1;
LineIndexEndOfAssignment := -1;
for i := 0
to LLines.Count - 1
do
begin
LLine := LLines.Strings[i].Trim;
if LLine = '
'
then
Continue;
if LLine.StartsWith('
unit ')
then
begin
LUnitName := Copy(LLine, Pos('
', LLine), Length(LLine));
LUnitName := Trim(Copy(LUnitName, 1, Length(LUnitName) - 1));
Continue;
end;
// ('German', 'English')
if LLine.
Contains('
(''
')
and LLine.
Contains('
,')
and LLine.
Contains('
''
)')
then
begin
LComponentOrVariable := Trim(Copy(LLine, 1, Pos('
:', LLine) - 1));
LLine := Copy(LLine, Pos('
(', LLine) + 1, Length(LLine));
if LLine.EndsWith('
);')
then
LLine := Copy(LLine, 1, Length(LLine) - 2);
Arr := LLine.Split(['
,']);
LineIndexAllocation := i;
for s
in Arr
do
AddDataToPOFile(s);
Continue;
end;
if LLine.
Contains('
:=')
then
begin
LComponentOrVariable := Trim(Copy(LLine, 1, Pos('
:=', LLine) - 1));
BAllocationFound := True;
LineIndexAllocation := i;
end;
if LLine.
Contains('
;')
then
begin
BEndOfAssignmentFound := True;
LineIndexEndOfAssignment := i;
end;
if BAllocationFound
and BEndOfAssignmentFound
then
begin
s := '
';
for j := LineIndexAllocation
to LineIndexEndOfAssignment
do
s := s + '
' + LLines.Strings[j].Trim;
PosStart := Pos('
:=', s) + 2;
PosEnd := Pos('
;', s);
AddDataToPOFile(Copy(s, PosStart, PosEnd - PosStart));
BAllocationFound := False;
BEndOfAssignmentFound := False;
end;
end;
if LLinesExport.Count > 0
then
begin
LLinesExport.SaveToFile(LFileExport);
end;
finally
LLinesExport.Free;
end;
end;
end;
finally
LLines.Free;
end;