updatepofiles: added ability to search for .rsj/.rst/.lrj files in specified directory, do not ignore missing .rsj/.rst/.lrj files anymore

This commit is contained in:
Maxim Ganetsky 2021-08-11 01:14:18 +03:00
parent bc7d6df12a
commit 8d7e9e30ff
2 changed files with 125 additions and 50 deletions

View File

@ -1,46 +1,44 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<CONFIG> <CONFIG>
<ProjectOptions> <ProjectOptions>
<Version Value="9"/> <Version Value="12"/>
<PathDelim Value="\"/> <PathDelim Value="\"/>
<General> <General>
<Flags> <Flags>
<SaveOnlyProjectUnits Value="True"/> <SaveOnlyProjectUnits Value="True"/>
<MainUnitHasCreateFormStatements Value="False"/> <MainUnitHasCreateFormStatements Value="False"/>
<MainUnitHasTitleStatement Value="False"/> <MainUnitHasTitleStatement Value="False"/>
<MainUnitHasScaledStatement Value="False"/>
</Flags> </Flags>
<SessionStorage Value="InIDEConfig"/> <SessionStorage Value="InIDEConfig"/>
<MainUnit Value="0"/>
</General> </General>
<VersionInfo> <BuildModes>
<StringTable ProductVersion=""/> <Item Name="default" Default="True"/>
</VersionInfo>
<BuildModes Count="1">
<Item1 Name="default" Default="True"/>
</BuildModes> </BuildModes>
<PublishOptions> <PublishOptions>
<Version Value="2"/> <Version Value="2"/>
<IgnoreBinaries Value="False"/>
<IncludeFileFilter Value="*.(pas|pp|inc|lfm|lpr|lrs|lpi|lpk|sh|xml)"/>
<ExcludeFileFilter Value="*.(bak|ppu|ppw|o|so);*~;backup"/>
</PublishOptions> </PublishOptions>
<RunParams> <RunParams>
<local> <FormatVersion Value="2"/>
<FormatVersion Value="1"/> <Modes>
<LaunchingApplication PathPlusParams="\usr\X11R6\bin\xterm -T 'Lazarus Run Output' -e $(LazarusDir)\tools\runwait.sh $(TargetCmdLine)"/> <Mode Name="default">
</local> <local>
<LaunchingApplication PathPlusParams="\usr\X11R6\bin\xterm -T &apos;Lazarus Run Output&apos; -e $(LazarusDir)\tools\runwait.sh $(TargetCmdLine)"/>
</local>
</Mode>
</Modes>
</RunParams> </RunParams>
<RequiredPackages Count="1"> <RequiredPackages>
<Item1> <Item>
<PackageName Value="LCL"/> <PackageName Value="LCL"/>
</Item1> </Item>
</RequiredPackages> </RequiredPackages>
<Units Count="1"> <Units>
<Unit0> <Unit>
<Filename Value="updatepofiles.pas"/> <Filename Value="updatepofiles.pas"/>
<IsPartOfProject Value="True"/> <IsPartOfProject Value="True"/>
<UnitName Value="UpdatePoFiles"/> <UnitName Value="UpdatePoFiles"/>
</Unit0> </Unit>
</Units> </Units>
</ProjectOptions> </ProjectOptions>
<CompilerOptions> <CompilerOptions>
@ -49,5 +47,10 @@
<SearchPaths> <SearchPaths>
<IncludeFiles Value="$(ProjOutDir)"/> <IncludeFiles Value="$(ProjOutDir)"/>
</SearchPaths> </SearchPaths>
<Linking>
<Debugging>
<DebugInfoType Value="dsDwarf3"/>
</Debugging>
</Linking>
</CompilerOptions> </CompilerOptions>
</CONFIG> </CONFIG>

View File

@ -1,4 +1,3 @@
{ $Id$ }
{ {
*************************************************************************** ***************************************************************************
* * * *
@ -25,20 +24,42 @@
updatepofiles - updates po files. updatepofiles - updates po files.
Synopsis: Synopsis:
updatepofiles filename1.pot [filename2.pot ... filenameN.pot] updatepofiles [--searchdir=<dir>] [filenameA.rsj [filenameB.rsj ... filenameN.rsj]] filename1.pot [filename2.pot ... filenameN.pot]
Description: Description:
updatepofiles updates the .pot file and merges new strings into Updatepofiles updates the .pot file from .rsj (or .rst/.lrj) files and merges new strings into
all translated po files (filename1.*.po) all translated po files (filename1.*.po).
Examples:
1. Only update .po files:
updatepofiles /path/to/project1.pot
2. Update .pot file from .rsj and then .po files:
updatepofiles /path/to/fileA.rsj /path/to/project1.pot
3. Update .pot file from several .rsj files:
updatepofiles /path/to/fileA.rsj /path/to/fileB.rsj /path/to/project1.pot
4. Same as 2, but search .rsj in specified directory and its subdirectories:
updatepofiles --searchdir=<dir> fileA.rsj /path/to/project1.pot
Arguments can be repeated as needed, e. g.:
updatepofiles /path/to/fileA.rsj /path/to/project1.pot /path/to/fileB.rsj /path/to/project2.pot
will update project1.pot from fileA.rsj and project2.pot from fileB.rsj respectively.
} }
program UpdatePoFiles; program UpdatePoFiles;
{$mode objfpc}{$H+} {$mode objfpc}{$H+}
uses uses
Classes, SysUtils, Translations, LazFileUtils, LazUTF8; Classes, SysUtils, Translations, LazFileUtils, LazUTF8, FileUtil;
const
ResSearchDirParamName='--searchdir=';
var var
Files: TStringList; Files: TStringList;
Prefix: string; Prefix: string;
@ -64,49 +85,100 @@ begin
ResFiles[i].Free; ResFiles[i].Free;
end; end;
function TryFindResFile(ResSearchDir, FileName: string): string;
var
FileNameOnly: String;
FL: TStringList;
i: integer;
begin
Result:='';
if ResSearchDir<>'' then
begin
FileNameOnly:=ExtractFileName(FileName); //make sure that path to file is removed
try
FL:=FindAllFiles(ResSearchDir, FileNameOnly, True);
if FL.Count>0 then
begin
Result:=FL[0];
i:=1;
while i<FL.Count do
begin
if FileAge(FL[i])>FileAge(Result) then
Result:=FL[i];
inc(i);
end;
end;
finally
FL.Free;
end;
end;
//writeln('Found resource string table file: ', Result);
end;
function ParamsValid: boolean; function ParamsValid: boolean;
var var
i: Integer; i: Integer;
ResSearchDir: String;
CurParam: String;
Filename: String; Filename: String;
Ext: String; Ext: String;
PoIndex: Integer; PoIndex: Integer;
IsResFile: Boolean;
begin begin
Result:=false; Result:=false;
PoIndex:=0; PoIndex:=0;
ResSearchDir:='';
if ParamCount<1 then if ParamCount<1 then
exit; exit;
for i:=1 to ParamCount do begin for i:=1 to ParamCount do
begin
CurParam:=ParamStrUTF8(i);
if UTF8StartsText(ResSearchDirParamName, CurParam) then
begin
ResSearchDir:=RightStr(CurParam, Length(CurParam)-Length(ResSearchDirParamName));
writeln('Current resource string table file search directory: ', ResSearchDir);
end
else
begin
Filename:=CurParam;
Filename:=ParamStrUTF8(i); Ext:=ExtractFileExt(Filename);
Ext:=ExtractFileExt(Filename); if (Ext<>'.pot') and (Ext<>'.rst') and (Ext<>'.lrj') and (Ext<>'.rsj') then
begin
writeln('ERROR: invalid extension: ', Filename);
exit;
end;
if not FileExistsUTF8(Filename) then begin IsResFile:=(Ext='.rst') or (Ext='.lrj') or (Ext='.rsj');
if (Ext='.rst') or (Ext='.lrj') or (Ext='.rsj') then if not FileExistsUTF8(Filename) then
continue; // ignore resource files begin
if IsResFile then
Filename:=TryFindResFile(ResSearchDir, Filename); // if resource file does not exist, search it in specified directory
writeln('ERROR: file not found: ',FileName); if (Filename='') or (not IsResFile) then
exit; begin
writeln('ERROR: file not found: ', FileName);
exit;
end;
end;
if Ext='.pot' then
begin
if Files=nil then
Files:=TStringList.Create;
Files.Add(Filename);
inc(PoIndex);
SetLength(ResFiles, Files.Count); // make sure Files and ResFiles are in sync
end
else
AddResFile(PoIndex, FileName);
end; end;
if (Ext<>'.pot') and (Ext<>'.rst') and (Ext<>'.lrj') and (Ext<>'.rsj') then begin
writeln('ERROR: invalid extension: ',Filename);
exit;
end;
if Ext='.pot' then begin
if Files=nil then
Files:=TStringList.Create;
Files.Add(Filename);
inc(PoIndex);
SetLength(ResFiles, Files.Count); // make sure Files and ResFiles are in sync
end else
AddResFile(PoIndex, FileName);
end; end;
Result:=true; Result:=PoIndex>0; // at least one .pot file should be found
end; end;
procedure UpdateAllPoFiles; procedure UpdateAllPoFiles;
@ -123,7 +195,7 @@ begin
if not ParamsValid then if not ParamsValid then
writeln('Usage: ',ExtractFileName(ParamStrUTF8(0)) writeln('Usage: ',ExtractFileName(ParamStrUTF8(0))
,' filename1.pot [filename2.pot ... filenameN.pot]') ,' [--searchdir=<dir>] [filenameA.rsj [filenameB.rsj ... filenameN.rsj]] filename1.pot [filename2.pot ... filenameN.pot]')
else else
UpdateAllPoFiles; UpdateAllPoFiles;