0.1.17.0: po files stored in resources

Use Project/Options/Resources in Laz 1.7+
            Use LazRes to make a file 'translate.lrs' in older Laz (minesadorada)   

git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@5434 8e941d3f-bd1b-0410-a28a-d453659cc2b4
This commit is contained in:
gbamber
2016-12-07 11:27:22 +00:00
parent 3d58c89dc1
commit b20239aae5
4 changed files with 186 additions and 119 deletions

View File

@ -15,6 +15,10 @@
<TextDesc Value="Your application description."/>
</XPManifest>
<Icon Value="0"/>
<Resources Count="2">
<Resource_0 FileName="locale\jsoneditor.en.po" Type="RCDATA" ResourceName="JSONEDITOR.EN"/>
<Resource_1 FileName="locale\jsoneditor.es.po" Type="RCDATA" ResourceName="JSONEDITOR.ES"/>
</Resources>
</General>
<i18n>
<EnableI18N Value="True"/>

View File

@ -4,13 +4,13 @@
<PathDelim Value="\"/>
<Version Value="10"/>
<BuildModes Active="Win64"/>
<Units Count="23">
<Units Count="24">
<Unit0>
<Filename Value="jsonpackage.lpr"/>
<IsPartOfProject Value="True"/>
<EditorIndex Value="2"/>
<CursorPos Y="30"/>
<UsageCount Value="76"/>
<UsageCount Value="78"/>
<Loaded Value="True"/>
</Unit0>
<Unit1>
@ -20,12 +20,12 @@
<HasResources Value="True"/>
<ResourceBaseClass Value="Form"/>
<IsVisibleTab Value="True"/>
<TopLine Value="15"/>
<CursorPos X="57" Y="44"/>
<UsageCount Value="76"/>
<TopLine Value="34"/>
<CursorPos X="56" Y="60"/>
<UsageCount Value="78"/>
<Bookmarks Count="2">
<Item0 Y="502" ID="1"/>
<Item1 Y="784"/>
<Item0 Y="507" ID="1"/>
<Item1 Y="791"/>
</Bookmarks>
<Loaded Value="True"/>
<LoadedDesigner Value="True"/>
@ -33,7 +33,7 @@
<Unit2>
<Filename Value="ugenericcollection.pas"/>
<IsPartOfProject Value="True"/>
<UsageCount Value="76"/>
<UsageCount Value="78"/>
</Unit2>
<Unit3>
<Filename Value="D:\Lazarusprojects\golfml\egacalculator\source\umainform.pas"/>
@ -154,9 +154,8 @@
<Unit18>
<Filename Value="D:\lazarustrunk\common_components\cryptini\ucryptini.pas"/>
<EditorIndex Value="-1"/>
<TopLine Value="1023"/>
<CursorPos X="30" Y="1045"/>
<UsageCount Value="9"/>
<UsageCount Value="10"/>
</Unit18>
<Unit19>
<Filename Value="C:\NewPascalDarwin\lazarus\ide\revision.inc"/>
@ -176,9 +175,9 @@
<Filename Value="C:\NewPascalDarwin\lazarus\lcl\translations.pas"/>
<UnitName Value="Translations"/>
<EditorIndex Value="1"/>
<TopLine Value="67"/>
<CursorPos X="41" Y="90"/>
<UsageCount Value="11"/>
<TopLine Value="76"/>
<CursorPos X="36" Y="82"/>
<UsageCount Value="12"/>
<Loaded Value="True"/>
</Unit21>
<Unit22>
@ -186,6 +185,12 @@
<EditorIndex Value="-1"/>
<UsageCount Value="10"/>
</Unit22>
<Unit23>
<Filename Value="C:\NewPascalDarwin\fpc\rtl\win\wininc\defines.inc"/>
<EditorIndex Value="-1"/>
<CursorPos X="64" Y="1204"/>
<UsageCount Value="10"/>
</Unit23>
</Units>
<OtherDefines Count="1">
<Define0 Value="IGNOREPICTURE"/>
@ -193,123 +198,123 @@
<JumpHistory Count="30" HistoryIndex="29">
<Position1>
<Filename Value="umain.pas"/>
<Caret Line="498" Column="75" TopLine="458"/>
<Caret Line="897" TopLine="850"/>
</Position1>
<Position2>
<Filename Value="umain.pas"/>
<Caret Line="77" Column="72" TopLine="43"/>
<Caret Line="904" Column="47" TopLine="862"/>
</Position2>
<Position3>
<Filename Value="umain.pas"/>
<Caret Line="568" Column="55" TopLine="545"/>
<Caret Line="898" Column="3" TopLine="863"/>
</Position3>
<Position4>
<Filename Value="umain.pas"/>
<Caret Line="57" TopLine="33"/>
<Caret Line="902" Column="44" TopLine="863"/>
</Position4>
<Position5>
<Filename Value="umain.pas"/>
<Caret Line="569" Column="43" TopLine="545"/>
<Caret Line="903" Column="42" TopLine="863"/>
</Position5>
<Position6>
<Filename Value="umain.pas"/>
<Caret Line="415" Column="4" TopLine="412"/>
<Caret Line="899" Column="45" TopLine="863"/>
</Position6>
<Position7>
<Filename Value="umain.pas"/>
<Caret Line="417" Column="54" TopLine="244"/>
<Caret Line="900" Column="44" TopLine="864"/>
</Position7>
<Position8>
<Filename Value="umain.pas"/>
<Caret Line="470" Column="3" TopLine="209"/>
<Caret Line="188" Column="4" TopLine="175"/>
</Position8>
<Position9>
<Filename Value="umain.pas"/>
<Caret Line="18" Column="40"/>
<Caret Line="900" Column="50" TopLine="864"/>
</Position9>
<Position10>
<Filename Value="umain.pas"/>
<Caret Line="464" TopLine="437"/>
<Caret Line="188" Column="4" TopLine="159"/>
</Position10>
<Position11>
<Filename Value="umain.pas"/>
<Caret Line="168" Column="18" TopLine="134"/>
<Caret Line="900" Column="45" TopLine="864"/>
</Position11>
<Position12>
<Filename Value="umain.pas"/>
<Caret Line="897" TopLine="850"/>
<Caret Line="188" TopLine="154"/>
</Position12>
<Position13>
<Filename Value="umain.pas"/>
<Caret Line="904" Column="47" TopLine="862"/>
<Caret Line="903" Column="34" TopLine="863"/>
</Position13>
<Position14>
<Filename Value="umain.pas"/>
<Caret Line="898" Column="3" TopLine="863"/>
<Caret Line="54" Column="49" TopLine="20"/>
</Position14>
<Position15>
<Filename Value="umain.pas"/>
<Caret Line="902" Column="44" TopLine="863"/>
<Caret Line="3"/>
</Position15>
<Position16>
<Filename Value="umain.pas"/>
<Caret Line="903" Column="42" TopLine="863"/>
<Caret Line="184" Column="23" TopLine="162"/>
</Position16>
<Position17>
<Filename Value="umain.pas"/>
<Caret Line="899" Column="45" TopLine="863"/>
<Caret Line="55" Column="78" TopLine="37"/>
</Position17>
<Position18>
<Filename Value="umain.pas"/>
<Caret Line="900" Column="44" TopLine="864"/>
<Caret Line="905" Column="26" TopLine="864"/>
</Position18>
<Position19>
<Filename Value="umain.pas"/>
<Caret Line="188" Column="4" TopLine="175"/>
<Caret Line="922" Column="5" TopLine="878"/>
</Position19>
<Position20>
<Filename Value="umain.pas"/>
<Caret Line="900" Column="50" TopLine="864"/>
<Caret Line="193" TopLine="183"/>
</Position20>
<Position21>
<Filename Value="umain.pas"/>
<Caret Line="188" Column="4" TopLine="159"/>
<Caret Line="63" Column="99" TopLine="31"/>
</Position21>
<Position22>
<Filename Value="umain.pas"/>
<Caret Line="900" Column="45" TopLine="864"/>
<Caret Line="947" Column="66" TopLine="917"/>
</Position22>
<Position23>
<Filename Value="umain.pas"/>
<Caret Line="188" TopLine="154"/>
<Caret Line="961" Column="56" TopLine="929"/>
</Position23>
<Position24>
<Filename Value="umain.pas"/>
<Caret Line="903" Column="34" TopLine="863"/>
<Caret Line="947" Column="65" TopLine="930"/>
</Position24>
<Position25>
<Filename Value="umain.pas"/>
<Caret Line="54" Column="49" TopLine="20"/>
<Caret Line="59" Column="78" TopLine="35"/>
</Position25>
<Position26>
<Filename Value="umain.pas"/>
<Caret Line="3"/>
<Caret Line="947" Column="75" TopLine="923"/>
</Position26>
<Position27>
<Filename Value="umain.pas"/>
<Caret Line="184" Column="23" TopLine="162"/>
<Caret Column="2"/>
</Position27>
<Position28>
<Filename Value="umain.pas"/>
<Caret Line="55" Column="78" TopLine="37"/>
<Caret Line="909" Column="20" TopLine="893"/>
</Position28>
<Position29>
<Filename Value="umain.pas"/>
<Caret Line="905" Column="26" TopLine="864"/>
<Caret Line="935" Column="35" TopLine="923"/>
</Position29>
<Position30>
<Filename Value="umain.pas"/>
<Caret Line="922" Column="5" TopLine="878"/>
<Caret Line="47" Column="57" TopLine="7"/>
</Position30>
</JumpHistory>
</ProjectSession>

View File

@ -1,6 +1,8 @@
unit umain;
{$DEFINE PO_RESOURCES}
{ OnlinePackageManager Update JSON Editor
{$DEFINE PO_BUILTINRES}// Use built-in resources for .po files
{ OnlinePackageManager Update JSON Editor
Copyright (C)2016 usernames lainz, minesadorada, GetMem @ http://forum.lazarus.freepascal.org/index.php
@ -41,7 +43,9 @@ unit umain;
0.1.15.0: BugFix: File/Save didn't add the '.json' suffix in Linux (minesadorada)
Addition: After Loading, run validation tests(minesadorada)
0.1.16.0: Renamed ForceUpdate to ForceNotify (GetMem/minesadorada)
0.1.17.0: po files stored in resource file as fallback (minesadorada)
0.1.17.0: po files stored in resources
Use Project/Options/Resources in Laz 1.7+
Use LazRes to make a file 'translate.lrs' in older Laz (minesadorada)
0.1.18.0: ??
}
{$mode objfpc}{$H+}
@ -53,14 +57,14 @@ uses
Classes, Forms, Controls, StdCtrls, Menus, ActnList, StdActns, Grids,
Graphics, Buttons, fileutil, LazFileUtils, fileinfo, ugenericcollection, fpjsonrtti,
Dialogs, LCLTranslator, PopupNotifier, SysUtils, inifiles,
lclintf, lclVersion{$IFDEF PO_RESOURCES},LResources, LazUTF8Classes{$ENDIF};
CONST C_DEBUGMESSAGES=FALSE;
lclintf, lclVersion,{$IFDEF PO_BUILTINRES}LResources,LazUTF8Classes{$ENDIF};
const
C_DEBUGMESSAGES = False;
type
{ TUpdatePackageFiles }
TUpdatePackageFiles = class(TCollectionItem)
@ -101,8 +105,10 @@ type
function LoadFromFile(AFileName: string): boolean;
function SaveToFile(AFileName: string): boolean;
published
property UpdatePackageData: TUpdatePackageData read FUpdatePackageData write FUpdatePackageData;
property UpdatePackageFiles: TPackageFilesList read FUpdatePackageFiles write FUpdatePackageFiles;
property UpdatePackageData: TUpdatePackageData
read FUpdatePackageData write FUpdatePackageData;
property UpdatePackageFiles: TPackageFilesList
read FUpdatePackageFiles write FUpdatePackageFiles;
end;
{ TfrmMain }
@ -166,24 +172,27 @@ type
sUpdateDirectory, sZipDirectory: string;
slErrorList: TStrings;
CFG: TIniFile;
INIFilePath:String;
INIFilePath: string;
function ValidationFailed: boolean;
procedure CtrlShowPopup(Sender: TObject);
procedure CtrlHidePopup(Sender: TObject);
procedure CtrlSetUpPopupHandlers;
procedure CtrlMakeDirty(Sender: TObject);
function FoundADuplicateLPK: boolean;
Function CreateUniqueINI(var aCount: integer):Boolean;
function CreateUniqueINI(var aCount: integer): boolean;
public
{ public declarations }
end;
var
frmMain: TfrmMain;
{$IFDEF PO_RESOURCES}
sPoPath_en, sPoPath_es: string;
{$IFDEF PO_BUILTINRES}
aLRes: TLResource;
aSS: TStringListUTF8;
{$ENDIF}
S: TResourceStream;
F: TFileStream;
{$ENDIF}
implementation
@ -227,8 +236,8 @@ resourcestring
rsLanguageChan = 'Language changed to "%s".';
rsSorryThisLan = 'Sorry, this language is unavailable at this time.';
rsYouMayNeedTo = '(You may need to restart the app to see the change)';
rsThereAreOneO = '- There are one or more .lpk entries with the same name.%s'
+ '- Every .lpk entry must have a unique name.';
rsThereAreOneO = '- There are one or more .lpk entries with the same name.%s' +
'- Every .lpk entry must have a unique name.';
rsUpdateJsonSF = 'Update file "%s" failed to load correctly.';
{ TUpdatePackageData }
@ -387,7 +396,7 @@ end;
procedure TfrmMain.FormCloseQuery(Sender: TObject; var CanClose: boolean);
begin
CanClose := True;
if ((bDirty = True) AND (bDisableWarnings=FALSE)) then
if ((bDirty = True) and (bDisableWarnings = False)) then
begin
if MessageDlg(rsFileMayBeUns, mtConfirmation, [mbYes, mbNo], 0, mbNo) = mrNo then
CanClose := False;
@ -399,18 +408,19 @@ begin
CFG.UpdateFile;
end;
end;
function TfrmMain.CreateUniqueINI(var aCount: integer):Boolean;
// Recursively loop until correct INI found, or new one created
function TfrmMain.CreateUniqueINI(var aCount: integer): boolean;
// Recursively loop until correct INI found, or new one created
begin
Result:=FALSE;
Result := False;
INIFilePath := GetAppConfigFile(False) + IntToStr(aCount);
CFG := TIniFile.Create(INIFilePath);
CFG.CacheUpdates:=TRUE;
CFG.CacheUpdates := True;
if CFG.ReadString('Options', 'AppPath', ProgramDirectory) <> ProgramDirectory then
begin
FreeAndNil(CFG); // Ditch the old one
Inc(aCount);
Result:=TRUE;
Result := True;
CreateUniqueINI(aCount); // Make a new one
end;
end;
@ -418,7 +428,7 @@ end;
procedure TfrmMain.FormCreate(Sender: TObject);
var
sLang: string;
iIniCount:Integer;
iIniCount: integer;
begin
{
Self.AutoAdjustLayout(lapAutoAdjustForDPI, Self.DesignTimeDPI,
@ -449,12 +459,12 @@ begin
// Enable options persistence
// If program location is different, create a new CFG file
// Because each component's location might be different
iIniCount:=0;
If CreateUniqueINI(iIniCount) then
iIniCount := 0;
if CreateUniqueINI(iIniCount) then
CFG.WriteString('Options', 'AppPath', ProgramDirectory);
CFG.UpdateFile;
If C_DEBUGMESSAGES=TRUE then
ShowMessageFmt('Inifile=%s, Count=%d',[INIFilePath,iIniCount]);
if C_DEBUGMESSAGES = True then
ShowMessageFmt('Inifile=%s, Count=%d', [INIFilePath, iIniCount]);
// Pop-up hints (show on first run, then not again unless the user chooses)
bIsVirgin := CFG.ReadBool('Options', 'Virgin', True);
bShowPopupHints := bIsVirgin;
@ -479,8 +489,8 @@ end;
procedure TfrmMain.FormDestroy(Sender: TObject);
begin
CFG.Free;
slErrorList.Free;
CFG.Free;
slErrorList.Free;
end;
procedure TfrmMain.FormShow(Sender: TObject);
@ -509,21 +519,23 @@ begin
stringPackageFiles.RowCount := JSONPackage.UpdatePackageFiles.Count + 1;
for i := 0 to JSONPackage.UpdatePackageFiles.Count - 1 do
begin
stringPackageFiles.Cells[0, i + 1] := JSONPackage.UpdatePackageFiles.Items[i].Name;
stringPackageFiles.Cells[1, i + 1] := JSONPackage.UpdatePackageFiles.Items[i].Version;
stringPackageFiles.Cells[0, i + 1] :=
JSONPackage.UpdatePackageFiles.Items[i].Name;
stringPackageFiles.Cells[1, i + 1] :=
JSONPackage.UpdatePackageFiles.Items[i].Version;
end;
if ValidationFailed then
begin
if (slErrorList.Count > 1) then
ShowMessage(Format(rsUpdateJsonSF, [ExtractFileName(sJSONFilePath)]) +
LineEnding + LineEnding + rsOneOfTheReqn + LineEnding +
slErrorList.Text + LineEnding + rsFixThenTryAg)
else
ShowMessage(Format(rsUpdateJsonSF, [ExtractFileName(sJSONFilePath)]) +
LineEnding + LineEnding + rsOneOfTheReq1 + LineEnding +
slErrorList.Text + LineEnding + rsFixThenTryAg);
Exit;
end;
if ValidationFailed then
begin
if (slErrorList.Count > 1) then
ShowMessage(Format(rsUpdateJsonSF, [ExtractFileName(sJSONFilePath)]) + LineEnding
+ LineEnding + rsOneOfTheReqn + LineEnding + slErrorList.Text +
LineEnding + rsFixThenTryAg)
else
ShowMessage(Format(rsUpdateJsonSF,[ExtractFileName(sJSONFilePath)]) + LineEnding
+ LineEnding + rsOneOfTheReq1 + LineEnding + slErrorList.Text +
LineEnding + rsFixThenTryAg);
Exit;
end;
end
else
ShowMessageFmt('There was a problem loading "%s" - is it corrupted or in the wrong format?',
@ -573,8 +585,10 @@ begin
s += RightStr(VInfo.VersionStrings[1], Length(VInfo.VersionStrings[1]) -
EqualsPos) + LineEnding;
end;
{
s+=Format(rsCompiledWith2,
[{$I %FPCVERSION%},lcl_major,lcl_minor,LineEnding,{$I %FPCTARGETCPU%},{$I %FPCTARGETOS%},LineEnding,LineEnding]);
}
if VInfo.VersionStrings.Count > 1 then
begin
EqualsPos := Pos('=', VInfo.VersionStrings[0]); // File Deswcription
@ -614,7 +628,7 @@ begin
else
begin
mnu_lang_en.Checked := False;
// SetDefaultLang(''); // Back to default?
// SetDefaultLang(''); // Back to default?
ShowMessage(rsSorryThisLan + LineEnding + rsYouMayNeedTo);
end;
end;
@ -673,8 +687,8 @@ begin
Result := True;
end;
// A full URL?
if ((Length(editDownloadZipURL.Text) > 0) and (RightStr(editDownloadZipURL.Text, 1) = '/'))
then
if ((Length(editDownloadZipURL.Text) > 0) and
(RightStr(editDownloadZipURL.Text, 1) = '/')) then
begin
slErrorList.Add(rsDownloadZipURLI2);
editDownloadZipURL.Color := clYellow;
@ -752,7 +766,8 @@ begin
if bForceSaveAs or (sJSONFilePath = '') then
begin
FileSaveAs1.Dialog.InitialDir := sUpdateDirectory;
FileSaveAs1.Dialog.FileName := 'update_' + ExtractFilenameOnly(editName.Text) + '.json';
FileSaveAs1.Dialog.FileName :=
'update_' + ExtractFilenameOnly(editName.Text) + '.json';
if FileSaveAs1.Dialog.Execute then
sJSONFilePath := FileSaveAs1.Dialog.FileName
else
@ -897,39 +912,82 @@ begin
end;
end;
{$IFDEF PO_RESOURCES}
// Use embedded .po resources if not distributed with executable
Initialization
{$I translate.lrs}
If NOT FileExistsUTF8(ProgramDirectory + 'locale\' + ExtractFilenameOnly(Application.EXEName) + '.es.po') then
BEGIN
aLRes:=LazarusResources.Find('jsoneditor.es');
if assigned(aLRes) then
initialization
sPoPath_en := ProgramDirectory + 'locale\' + ExtractFilenameOnly(
Application.EXEName) + '.en.po';
sPoPath_es := ProgramDirectory + 'locale\' + ExtractFilenameOnly(
Application.EXEName) + '.es.po';
if (lcl_major > 0) and (lcl_minor > 6) then
begin
ForceDirectory(ProgramDirectory + 'locale');
aSS:=TStringListUTF8.Create;
TRY
Ass.Add(aLRes.Value);
aSS.SaveToFile(ProgramDirectory + 'locale\' + ExtractFilenameOnly(Application.EXEName) + '.es.po');
Finally
aSS.Free;
{$IFDEF PO_BUILTINRES}
// This uses a resource file added via Project/Options
if not FileExistsUTF8(sPoPath_en) then
begin
// create a resource stream which points to the po file
S := TResourceStream.Create(HInstance, 'JSONEDITOR.EN', MakeIntResource(10));
try
ForceDirectory(ProgramDirectory + 'locale');
F := TFileStream.Create(sPoPath_en, fmCreate);
try
F.CopyFrom(S, S.Size); // copy data from the resource stream to file stream
finally
F.Free; // destroy the file stream
end;
finally
S.Free; // destroy the resource stream
end;
end;
if not FileExistsUTF8(sPoPath_es) then
begin
S := TResourceStream.Create(HInstance, 'JSONEDITOR.ES', MakeIntResource(10));
try
ForceDirectory(ProgramDirectory + 'locale');
F := TFileStream.Create(sPoPath_es, fmCreate);
try
F.CopyFrom(S, S.Size);
finally
F.Free
end;
finally
S.Free;
end;
end;
end
else
begin // Older version of laz
// This uses an lrs file generated from lazres
{$I translate.lrs}
if not FileExistsUTF8(sPoPath_es) then
begin
aLRes := LazarusResources.Find('jsoneditor.es');
if assigned(aLRes) then
begin
ForceDirectory(ProgramDirectory + 'locale');
aSS := TStringListUTF8.Create;
try
Ass.Add(aLRes.Value);
aSS.SaveToFile(sPoPath_es);
finally
aSS.Free;
end;
end;
end;
if not FileExistsUTF8(sPoPath_en) then
begin
aLRes := LazarusResources.Find('jsoneditor.en');
if assigned(aLRes) then
begin
ForceDirectory(ProgramDirectory + 'locale');
aSS := TStringListUTF8.Create;
try
Ass.Add(aLRes.Value);
aSS.SaveToFile(sPoPath_en);
finally
aSS.Free;
end;
end;
end;
end;
END;
If NOT FileExistsUTF8(ProgramDirectory + 'locale\' + ExtractFilenameOnly(Application.EXEName) + '.en.po') then
BEGIN
aLRes:=LazarusResources.Find('jsoneditor.en');
if assigned(aLRes) then
begin
ForceDirectory(ProgramDirectory + 'locale');
aSS:=TStringListUTF8.Create;
TRY
Ass.Add(aLRes.Value);
aSS.SaveToFile(ProgramDirectory + 'locale\' + ExtractFilenameOnly(Application.EXEName) + '.en.po');
FINALLY
aSS.Free;
END;
end;
END;
{$ENDIF}
end.