From 511cc0cd621ebdc67fdfbd440d098b2d1c23dc58 Mon Sep 17 00:00:00 2001 From: Jean-Pierre Ledure Date: Thu, 10 Mar 2022 12:10:51 +0100 Subject: ScriptForge - (SF_Root) differ loading of the localized interface So far, the loading of the user interface in the user's own language was done at the first invocation of ScriptForge, usually via - a CreateScriptService() method - a direct call to any method, f.i. SF_Array.Append() Now the same loading of the user interface in memory is done at the first request of a localized text or message. Due to the fact that since last commit https://gerrit.libreoffice.org/c/core/+/131255 only error messages contain localized texts, the benefit is that correct scripts will not load any unnecessary texts anymore. The "Interface" variable has be renamed "LocalizedInterface" for clarity in the SF_Root class module No impact on documentation or Python scripts. Change-Id: Ie654625540c0c8f8442e9ba36d38ecaab00c0eb1 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/131313 Tested-by: Jean-Pierre Ledure Tested-by: Jenkins Reviewed-by: Jean-Pierre Ledure --- wizards/source/scriptforge/SF_Exception.xba | 13 ++++++------- wizards/source/scriptforge/SF_Root.xba | 28 +++++++++++++++++++++------- wizards/source/scriptforge/SF_Utils.xba | 6 ++---- 3 files changed, 29 insertions(+), 18 deletions(-) (limited to 'wizards') diff --git a/wizards/source/scriptforge/SF_Exception.xba b/wizards/source/scriptforge/SF_Exception.xba index 2af37d9534f2..aeac2fb91aab 100644 --- a/wizards/source/scriptforge/SF_Exception.xba +++ b/wizards/source/scriptforge/SF_Exception.xba @@ -612,7 +612,7 @@ Public Sub Raise(Optional ByVal Number As Variant _ ''' SF_Exception.Raise(,, "To divide by zero is not a good idea !") Dim sMessage As String ' Error message to log and to display -Dim L10N As Object ' Alias to Interface +Dim L10N As Object ' Alias to LocalizedInterface Const cstThisSub = "Exception.Raise" Const cstSubArgs = "[Number=Err], [Source=Erl], [Description]" @@ -641,7 +641,7 @@ Try: If Len(Description) > 0 Then .Description = Description ' Log and display - Set L10N = _SF_.Interface + Set L10N = _SF_._GetLocalizedInterface() sMessage = L10N.GetText("LONGERRORDESC", .Number, .Source, .Description) .DebugPrint(sMessage) If _SF_.DisplayEnabled Then MsgBox L10N.GetText("ERRORNUMBER", .Number) _ @@ -675,7 +675,7 @@ Public Sub RaiseAbort(Optional ByVal Source As Variant) Dim sLocation As String ' Common header in error messages: location of error Dim vLocation As Variant ' Split array (library, module, method) Dim sMessage As String ' Error message to log and to display -Dim L10N As Object ' Alias to Interface +Dim L10N As Object ' Alias to LocalizedInterface Const cstTabSize = 4 Const cstThisSub = "Exception.RaiseAbort" Const cstSubArgs = "[Source=Erl]" @@ -691,7 +691,7 @@ Try: With SF_Exception ' Prepare message header - Set L10N = _SF_.Interface + Set L10N = _SF_._GetLocalizedInterface() If Len(_SF_.MainFunction) > 0 Then ' MainFunction = [Library.]Module.Method vLocation = Split(_SF_.MainFunction, ".") If UBound(vLocation) < 2 Then vLocation = SF_Array.Prepend(vLocation, "ScriptForge") @@ -701,7 +701,6 @@ Try: End If ' Log and display - Set L10N = _SF_.Interface sMessage = L10N.GetText("LONGERRORDESC", .Number, .Source, .Description) .DebugPrint(sMessage) If _SF_.DisplayEnabled Then @@ -741,7 +740,7 @@ Dim sService As String ' Service name having detected the error Dim sMethod As String ' Method name having detected the error Dim vLocation As Variant ' Split array (library, module, method) Dim sMessage As String ' Message to log and display -Dim L10N As Object ' Alias of Interface +Dim L10N As Object ' Alias of LocalizedInterface Dim sAlt As String ' Alternative error messages Dim iButtons As Integer ' MB_OK or MB_YESNO Dim iMsgBox As Integer ' Return value of the message box @@ -760,7 +759,7 @@ Check: End If Try: - Set L10N = _SF_.Interface + Set L10N = _SF_._GetLocalizedInterface() ' Location header common to all error messages If Len(_SF_.MainFunction) > 0 Then ' MainFunction = [Library.]Module.Method vLocation = Split(_SF_.MainFunction, ".") diff --git a/wizards/source/scriptforge/SF_Root.xba b/wizards/source/scriptforge/SF_Root.xba index 0ed77373d773..31137c7ec041 100644 --- a/wizards/source/scriptforge/SF_Root.xba +++ b/wizards/source/scriptforge/SF_Root.xba @@ -83,7 +83,7 @@ Private Toolkit As Object ' com.sun.star.awt.Toolkit Private FileSystemNaming As String ' If "SYS", file and folder naming is based on operating system notation Private PythonHelper As String ' File name of Python helper functions (stored in $(inst)/share/Scripts/python) Private PythonHelper2 As String ' Alternate Python helper file name for test purposes -Private Interface As Object ' ScriptForge own L10N service +Private LocalizedInterface As Object ' ScriptForge own L10N service Private OSName As String ' WIN, LINUX, MACOS Private SFDialogs As Variant ' Persistent storage for the SFDialogs library Private SFForms As Variant ' Persistent storage for the SF_Form class in the SFDocuments library @@ -137,7 +137,7 @@ Private Sub Class_Initialize() FileSystemNaming = "ANY" PythonHelper = "ScriptForgeHelper.py" PythonHelper2 = "" - Set Interface = Nothing + Set LocalizedInterface = Nothing Set BrowseNodeFactory = Nothing Set DatabaseContext = Nothing Set ConfigurationProvider = Nothing @@ -240,6 +240,20 @@ Finally: Exit Function End Function ' ScriptForge.SF_Root._AddToPythonStorage +REM ------------------------------------------------------------------------------ +Public Function _GetLocalizedInterface() As Object +''' Returns the LN object instance related to the ScriptForge internal localization +''' If not yet done, load it from the shipped po files +''' Makes that the localized user interface is loaded only when needed + +Try: + If IsNull(LocalizedInterface) Then _LoadLocalizedInterface() + +Finally: + Set _GetLocalizedInterface = LocalizedInterface + Exit Function +End Function ' ScriptForge.SF_Root._GetLocalizedInterface + REM ----------------------------------------------------------------------------- Public Sub _InitPythonStorage() ''' Make PythonStorage an array @@ -269,7 +283,7 @@ End Sub ' ScriptForge.SF_Root._InitPythonStorage REM ----------------------------------------------------------------------------- Public Sub _LoadLocalizedInterface(Optional ByVal psMode As String) ''' Build the user interface in a persistent L10N object -''' Executed - only once - at first ScriptForge invocation by a user script +''' Executed - only once - at first request of a label inside the LocalizedInterface dictionary ''' Args: ''' psMode: ADDTEXT => the (english) labels are loaded from code below ''' POFILE => the localized labels are loaded from a PO file @@ -294,19 +308,19 @@ Try: sLocale = SF_Utils._GetUNOService("OfficeLocale").Language sPOFolder = .BuildPath(sInstallFolder, "po") sPOFile = .BuildPath(sPOFolder, sLocale & ".po") - If sLocale = "en" Then ' Interface loaded by code i.o. read from po file + If sLocale = "en" Then ' LocalizedInterface loaded by code i.o. read from po file psMode = "ADDTEXT" ElseIf Not .FileExists(sPOFile) Then ' File not found => load texts from code below psMode = "ADDTEXT" Else - Set Interface = CreateScriptService("L10N", sPOFolder, sLocale) + Set LocalizedInterface = CreateScriptService("L10N", sPOFolder, sLocale) End If End With End If If psMode = "ADDTEXT" Then ' Use this mode in development to prepare a new POT file - Set Interface = CreateScriptService("L10N") - With Interface + Set LocalizedInterface = CreateScriptService("L10N") + With LocalizedInterface ' SF_Exception.Raise .AddText( Context := "ERRORNUMBER" _ , MsgId := "Error %1" _ diff --git a/wizards/source/scriptforge/SF_Utils.xba b/wizards/source/scriptforge/SF_Utils.xba index 28aed74fa19f..e0381f8a5d5f 100644 --- a/wizards/source/scriptforge/SF_Utils.xba +++ b/wizards/source/scriptforge/SF_Utils.xba @@ -226,9 +226,9 @@ Dim sHeader As String ' The specific header to insert Try: With _SF_ - If Not IsNull(.Interface) Then .Interface.Dispose() + If Not IsNull(.LocalizedInterface) Then .LocalizedInterface.Dispose() ._LoadLocalizedInterface(psMode := "ADDTEXT") ' Force reload of labels from the code - .Interface.ExportToPOTFile(FileName, Header := sHeader) + .LocalizedInterface.ExportToPOTFile(FileName, Header := sHeader) End With Finally: @@ -506,8 +506,6 @@ Public Sub _InitializeRoot(Optional ByVal pbForce As Boolean) If IsEmpty(_SF_) Or IsNull(_SF_) Then Set _SF_ = New SF_Root Set _SF_.[Me] = _SF_ - ' Localization - _SF_._LoadLocalizedInterface() End If End Sub ' ScriptForge.SF_Utils._InitializeRoot -- cgit v1.2.3