summaryrefslogtreecommitdiff
path: root/wizards
diff options
context:
space:
mode:
authorJean-Pierre Ledure <jp@ledure.be>2021-09-02 16:08:44 +0200
committerJean-Pierre Ledure <jp@ledure.be>2021-09-03 14:53:27 +0200
commit552a1f30eca91eb647d10d77135b32a5729f11bd (patch)
tree17236f30e40fe755c2ee8ea949468dbfc89e9c4f /wizards
parent66402b476da8bcbd87f9dd7d7410ee48bfdeeadc (diff)
ScriptForge - add AddTextsFromDialog and GetTextsFromL10N methods
The objective is to facilitate the automatic translation of all fixed texts in dialogs and dialog controls. Concerned items are: - the title of the dialog - the caption associated with next control types: Button, CheckBox, FixedLine, FixedText, GroupBox and RadioButton - the content of list- and comboboxes - the tip- or helptext displayed when the mouse is hovering the control ScriptForge.SF_L10N.AddTextsFromDialog(dialog) store all fixed texts of the dialog in the list of translatable strings of a L10N object to prepare a POT file SFDialogs.Dialog.GetTextsFromL10N(l10n) get the translated versions of all fixed texts in the dialog from a L10N object and apply them on the dialog before display Both methods are executable from Basic and Python user scripts Change-Id: I3dc9fbbce287d7af4040df713f9d11e0ff928b1c Reviewed-on: https://gerrit.libreoffice.org/c/core/+/121514 Tested-by: Jean-Pierre Ledure <jp@ledure.be> Tested-by: Jenkins Reviewed-by: Jean-Pierre Ledure <jp@ledure.be>
Diffstat (limited to 'wizards')
-rw-r--r--wizards/source/scriptforge/SF_L10N.xba135
-rw-r--r--wizards/source/scriptforge/SF_Root.xba2
-rw-r--r--wizards/source/scriptforge/SF_Utils.xba5
-rw-r--r--wizards/source/scriptforge/python/scriptforge.py8
-rw-r--r--wizards/source/sfdialogs/SF_Dialog.xba93
-rw-r--r--wizards/source/sfdialogs/SF_DialogControl.xba4
-rw-r--r--wizards/source/sfdialogs/SF_Register.xba2
7 files changed, 232 insertions, 17 deletions
diff --git a/wizards/source/scriptforge/SF_L10N.xba b/wizards/source/scriptforge/SF_L10N.xba
index 020dca541831..6aab1f7b4f14 100644
--- a/wizards/source/scriptforge/SF_L10N.xba
+++ b/wizards/source/scriptforge/SF_L10N.xba
@@ -34,11 +34,12 @@ Option Explicit
&apos;&apos;&apos; Being independent text files, one per language to support, the programmer may give away
&apos;&apos;&apos; pristine PO template files (known as POT-files) for a translator to process.
&apos;&apos;&apos;
-&apos;&apos;&apos; This class implements mainly 3 mechanisms:
-&apos;&apos;&apos; - AddText: for the programmer to build a set of words or sentences
+&apos;&apos;&apos; This class implements mainly 4 mechanisms:
+&apos;&apos;&apos; 1. AddText: for the programmer to build a set of words or sentences
&apos;&apos;&apos; meant for being translated later
-&apos;&apos;&apos; - ExportToPOTFile: All the above texts are exported into a pristine POT-file
-&apos;&apos;&apos; - GetText: At runtime get the text in the user language
+&apos;&apos;&apos; 2. AddTextsFromDialog: to automatically execute AddText() on each fixed text of a dialog
+&apos;&apos;&apos; 3. ExportToPOTFile: All the above texts are exported into a pristine POT-file
+&apos;&apos;&apos; 4. GetText: At runtime get the text in the user language
&apos;&apos;&apos; Note that the first two are optional: POT and PO-files may be built with a simple text editor
&apos;&apos;&apos;
&apos;&apos;&apos; Several instances of the L10N class may coexist
@@ -52,7 +53,7 @@ Option Explicit
&apos;&apos;&apos; Locale: in the form la-CO (language-COUNTRY)
&apos;&apos;&apos; Service invocation examples:
&apos;&apos;&apos; Dim myPO As Variant
-&apos;&apos;&apos; myPO = CreateScriptService(&quot;L10N&quot;) &apos; AddText and ExportToPOTFile are allowed
+&apos;&apos;&apos; myPO = CreateScriptService(&quot;L10N&quot;) &apos; AddText, AddTextsFromDialogt and ExportToPOTFile are allowed
&apos;&apos;&apos; myPO = CreateScriptService(&quot;L10N&quot;, &quot;C:\myPOFiles\&quot;, &quot;fr-BE&quot;)
&apos;&apos;&apos; &apos;All functionalities are available
&apos;&apos;&apos;
@@ -229,6 +230,91 @@ CatchDuplicate:
End Function &apos; ScriptForge.SF_L10N.AddText
REM -----------------------------------------------------------------------------
+Public Function AddTextsFromDialog(Optional ByRef Dialog As Variant) As Boolean
+&apos;&apos;&apos; Add all fixed text strings of a dialog to the list of localizable text strings
+&apos;&apos;&apos; Added texts are:
+&apos;&apos;&apos; - the title of the dialog
+&apos;&apos;&apos; - the caption associated with next control types: Button, CheckBox, FixedLine, FixedText, GroupBox and RadioButton
+&apos;&apos;&apos; - the content of list- and comboboxes
+&apos;&apos;&apos; - the tip- or helptext displayed when the mouse is hovering the control
+&apos;&apos;&apos; The current method has method SFDialogs.SF_Dialog.GetTextsFromL10N as counterpart
+&apos;&apos;&apos; The targeted dialog must not be open when the current method is run
+&apos;&apos;&apos; Args:
+&apos;&apos;&apos; Dialog: a SFDialogs.Dialog service instance
+&apos;&apos;&apos; Returns:
+&apos;&apos;&apos; True when successful
+&apos;&apos;&apos; Examples:
+&apos;&apos;&apos; Dim myDialog As Object
+&apos;&apos;&apos; Set myDialog = CreateScriptService(&quot;SFDialogs.Dialog&quot;, &quot;GlobalScope&quot;, &quot;XrayTool&quot;, &quot;DlgXray&quot;)
+&apos;&apos;&apos; myPO.AddTextsFromDialog(myDialog)
+
+Dim bAdd As Boolean &apos; Return value
+Dim vControls As Variant &apos; Array of control names
+Dim sControl As String &apos; A single control name
+Dim oControl As Object &apos; SFDialogs.DialogControl
+Dim sText As String &apos; The text to insert in the dictionary
+Dim sDialogComment As String &apos; The prefix in the comment to insert in the dictionary for the dialog
+Dim sControlComment As String &apos; The prefix in the comment to insert in the dictionary for a control
+Dim vSource As Variant &apos; RowSource property of dialog control as an array
+Dim i As Long
+
+Const cstThisSub = &quot;L10N.AddTextsFromDialog&quot;
+Const cstSubArgs = &quot;Dialog&quot;
+
+ If SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
+ bAdd = False
+
+Check:
+ If SF_Utils._EnterFunction(cstThisSub, cstSubArgs) Then
+ If Not SF_Utils._Validate(Dialog, &quot;Dialog&quot;, V_OBJECT, , , &quot;DIALOG&quot;) Then GoTo Finally
+ End If
+
+Try:
+ With Dialog
+ &apos; Store the title of the dialog
+ sDialogComment = &quot;Dialog =&gt; &quot; &amp; ._Container &amp; &quot; : &quot; &amp; ._Library &amp; &quot; : &quot; &amp; ._Name &amp; &quot; : &quot;
+ stext = .Caption
+ If Len(sText) &gt; 0 Then
+ If Not _ReplaceText(&quot;&quot;, sText, sDialogComment &amp; &quot;Caption&quot;) Then GoTo Catch
+ End If
+ &apos; Scan all controls
+ vControls = .Controls()
+ For Each sControl In vControls
+ Set oControl = .Controls(sControl)
+ sControlComment = sDialogComment &amp; sControl &amp; &quot;.&quot;
+ With oControl
+ &apos; Extract fixed texts
+ sText = .Caption
+ If Len(sText) &gt; 0 Then
+ If Not _ReplaceText(&quot;&quot;, sText, sControlComment &amp; &quot;Caption&quot;) Then GoTo Catch
+ End If
+ vSource = .RowSource &apos; List and comboboxes only
+ If IsArray(vSource) Then
+ For i = 0 To UBound(vSource)
+ If Len(vSource(i)) &gt; 0 Then
+ If Not _ReplaceText(&quot;&quot;, vSource(i), sControlComment &amp; &quot;RowSource[&quot; &amp; i &amp; &quot;]&quot;) Then GoTo Catch
+ End If
+ Next i
+ End If
+ sText = .TipText
+ If Len(sText) &gt; 0 Then
+ If Not _ReplaceText(&quot;&quot;, sText, sControlComment &amp; &quot;TipText&quot;) Then GoTo Catch
+ End If
+ End With
+ Next sControl
+ End With
+
+ bAdd = True
+
+Finally:
+ AddTextsFromDialog = bAdd
+ SF_Utils._ExitFunction(cstThisSub)
+ Exit Function
+Catch:
+ GoTo Finally
+End Function &apos; ScriptForge.SF_L10N.AddTextsFromDialog
+
+REM -----------------------------------------------------------------------------
Public Function ExportToPOTFile(Optional ByVal FileName As Variant _
, Optional ByVal Header As Variant _
, Optional ByVal Encoding As Variant _
@@ -267,8 +353,8 @@ Const cstSubArgs = &quot;FileName, [Header=&quot;&quot;&quot;&quot;], [Encoding=
bExport = False
Check:
- If IsMissing(Header) Or IsMissing(Header) Then Header = &quot;&quot;
- If IsMissing(Encoding) Or IsMissing(Encoding) Then Encoding = &quot;UTF-8&quot;
+ If IsMissing(Header) Or IsEmpty(Header) Then Header = &quot;&quot;
+ If IsMissing(Encoding) Or IsEmpty(Encoding) Then Encoding = &quot;UTF-8&quot;
If SF_Utils._EnterFunction(cstThisSub, cstSubArgs) Then
If Not SF_Utils._ValidateFile(FileName, &quot;FileName&quot;) Then GoTo Finally
If Not SF_Utils._Validate(Header, &quot;Header&quot;, V_STRING) Then GoTo Finally
@@ -506,6 +592,7 @@ Public Function Methods() As Variant
&quot;AddText&quot; _
, &quot;ExportToPOTFile&quot; _
, &quot;GetText&quot; _
+ , &quot;AddTextsFromDialog&quot; _
, &quot;_&quot; _
)
@@ -686,6 +773,40 @@ Finally:
End Function &apos; ScriptForge.SF_L10N._PropertyGet
REM -----------------------------------------------------------------------------
+Private Function _ReplaceText(ByVal psContext As String _
+ , ByVal psMsgId As String _
+ , ByVal psComment As String _
+ ) As Boolean
+&apos;&apos;&apos; When the entry in the dictionary does not yet exist, equivalent to AddText
+&apos;&apos;&apos; When it exists already, extend the existing comment with the psComment argument
+&apos;&apos;&apos; Used from AddTextsFromDialog to manage identical strings without raising errors,
+&apos;&apos;&apos; e.g. when multiple dialogs have the same &quot;Close&quot; button
+
+Dim bAdd As Boolean &apos; Return value
+Dim sKey As String &apos; The key part of an entry in the dictionary
+Dim vItem As POEntry &apos; The item part of the new entry in the dictionary
+
+Try:
+ bAdd = False
+ If Len(psContext) &gt; 0 Then sKey = psContext Else sKey = psMsgId
+ If _Dictionary.Exists(sKey) Then
+ &apos; Load the entry, adapt comment and rewrite
+ vItem = _Dictionary.Item(sKey)
+ If Len(vItem.Comment) = 0 Then vItem.Comment = psComment Else vItem.Comment = vItem.Comment &amp; &quot;\n&quot; &amp; psComment
+ bAdd = _Dictionary.ReplaceItem(sKey, vItem)
+ Else
+ &apos; Add a new entry as usual
+ bAdd = AddText(psContext, psMsgId, psComment)
+ End If
+
+Finally:
+ _ReplaceText = bAdd
+ Exit Function
+Catch:
+ GoTo Finally
+End Function &apos; ScriptForge.SF_L10N._ReplaceText
+
+REM -----------------------------------------------------------------------------
Private Function _Repr() As String
&apos;&apos;&apos; Convert the L10N instance to a readable string, typically for debugging purposes (DebugPrint ...)
&apos;&apos;&apos; Args:
diff --git a/wizards/source/scriptforge/SF_Root.xba b/wizards/source/scriptforge/SF_Root.xba
index 131f2eb90d56..7d9132caf205 100644
--- a/wizards/source/scriptforge/SF_Root.xba
+++ b/wizards/source/scriptforge/SF_Root.xba
@@ -72,7 +72,6 @@ Private DatabaseContext As Object &apos; com.sun.star.sdb.DatabaseContext
Private ConfigurationProvider _
As Object &apos; com.sun.star.configuration.ConfigurationProvider
Private MailService As Object &apos; com.sun.star.system.SimpleCommandMail or com.sun.star.system.SimpleSystemMail
-Private TreeDataModel As Object &apos; com.sun.star.awt.tree.MutableTreeDataModel
Private GraphicExportFilter As Object &apos; com.sun.star.drawing.GraphicExportFilter
&apos; Specific persistent services objects or properties
@@ -135,7 +134,6 @@ Private Sub Class_Initialize()
Set DatabaseContext = Nothing
Set ConfigurationProvider = Nothing
Set MailService = Nothing
- Set TreeDataModel = Nothing
Set GraphicExportFilter = Nothing
OSName = &quot;&quot;
SFDialogs = Empty
diff --git a/wizards/source/scriptforge/SF_Utils.xba b/wizards/source/scriptforge/SF_Utils.xba
index 99ee224cc79f..1505e4cfd308 100644
--- a/wizards/source/scriptforge/SF_Utils.xba
+++ b/wizards/source/scriptforge/SF_Utils.xba
@@ -439,11 +439,6 @@ Dim vNodePath As Variant
Set .TextSearch = CreateUnoService(&quot;com.sun.star.util.TextSearch&quot;)
End If
Set _GetUNOService = .TextSearch
- Case &quot;TreeDataModel&quot;
- If IsEmpty(.TreeDataModel) Or IsNull(.TreeDataModel) Then
- Set .TreeDataModel = CreateUnoService(&quot;com.sun.star.awt.tree.MutableTreeDataModel&quot;)
- End If
- Set _GetUNOService = .TreeDataModel
Case &quot;URLTransformer&quot;
If IsEmpty(.URLTransformer) Or IsNull(.URLTransformer) Then
Set .URLTransformer = CreateUnoService(&quot;com.sun.star.util.URLTransformer&quot;)
diff --git a/wizards/source/scriptforge/python/scriptforge.py b/wizards/source/scriptforge/python/scriptforge.py
index 498da0637dd7..12039b1d923d 100644
--- a/wizards/source/scriptforge/python/scriptforge.py
+++ b/wizards/source/scriptforge/python/scriptforge.py
@@ -1179,6 +1179,10 @@ class SFScriptForge:
def AddText(self, context = '', msgid = '', comment = ''):
return self.ExecMethod(self.vbMethod, 'AddText', context, msgid, comment)
+ def AddTextsFromDialog(self, dialog):
+ dialogobj = dialog.objectreference if isinstance(dialog, SFDialogs.SF_Dialog) else dialog
+ return self.ExecMethod(self.vbMethod + self.flgObject, 'AddTextsFromDialog', dialogobj)
+
def ExportToPOTFile(self, filename, header = '', encoding= 'UTF-8'):
return self.ExecMethod(self.vbMethod, 'ExportToPOTFile', filename, header, encoding)
@@ -1659,6 +1663,10 @@ class SFDialogs:
def Execute(self, modal = True):
return self.ExecMethod(self.vbMethod, 'Execute', modal)
+ def GetTextsFromL10N(self, l10n):
+ l10nobj = l10n.objectreference if isinstance(l10n, SFScriptForge.SF_L10N) else l10n
+ return self.ExecMethod(self.vbMethod + self.flgObject, 'GetTextsFromL10N', l10nobj)
+
def Terminate(self):
return self.ExecMethod(self.vbMethod, 'Terminate')
diff --git a/wizards/source/sfdialogs/SF_Dialog.xba b/wizards/source/sfdialogs/SF_Dialog.xba
index a574cf87ef25..b69e4b3ff5cf 100644
--- a/wizards/source/sfdialogs/SF_Dialog.xba
+++ b/wizards/source/sfdialogs/SF_Dialog.xba
@@ -499,6 +499,98 @@ Catch:
End Function &apos; SFDialogs.SF_Dialog.GetProperty
REM -----------------------------------------------------------------------------
+Public Function GetTextsFromL10N(Optional ByRef L10N As Variant) As Boolean
+&apos;&apos;&apos; Replace all fixed text strings of a dialog by their localized version
+&apos;&apos;&apos; Replaced texts are:
+&apos;&apos;&apos; - the title of the dialog
+&apos;&apos;&apos; - the caption associated with next control types: Button, CheckBox, FixedLine, FixedText, GroupBox and RadioButton
+&apos;&apos;&apos; - the content of list- and comboboxes
+&apos;&apos;&apos; - the tip- or helptext displayed when the mouse is hovering the control
+&apos;&apos;&apos; The current method has a twin method ScriptForge.SF_L10N.AddTextsFromDialog
+&apos;&apos;&apos; The current method is probably run before the Execute() method
+&apos;&apos;&apos; Args:
+&apos;&apos;&apos; L10N : a &quot;L10N&quot; service instance created with CreateScriptService(&quot;L10N&quot;)
+&apos;&apos;&apos; Returns:
+&apos;&apos;&apos; True when successful
+&apos;&apos;&apos; Examples:
+&apos;&apos;&apos; Dim myPO As Object, oDlg As Object
+&apos;&apos;&apos; Set oDlg = CreateScriptService(&quot;Dialog&quot;, &quot;GlobalScope&quot;, &quot;XrayTool&quot;, &quot;DlgXray&quot;)
+&apos;&apos;&apos; Set myPO = CreateScriptService(&quot;L10N&quot;, &quot;C:\myPOFiles\&quot;, &quot;fr-BE&quot;)
+&apos;&apos;&apos; oDlg.GetTextsFromL10N(myPO)
+
+Dim bGet As Boolean &apos; Return value
+Dim vControls As Variant &apos; Array of control names
+Dim sControl As String &apos; A single control name
+Dim oControl As Object &apos; SFDialogs.DialogControl
+Dim sText As String &apos; The text found in the dialog
+Dim sTranslation As String &apos; The translated text got from the dictionary
+Dim vSource As Variant &apos; RowSource property of dialog control as an array
+Dim bChanged As Boolean &apos; True when at least 1 item of a RowSource is modified
+Dim i As Long
+
+Const cstThisSub = &quot;SFDialogs.Dialog.GetTextsFromL10N&quot;
+Const cstSubArgs = &quot;L10N&quot;
+
+ If SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
+ bGet = False
+
+Check:
+ If SF_Utils._EnterFunction(cstThisSub, cstSubArgs) Then
+ If Not SF_Utils._Validate(L10N, &quot;L10N&quot;, V_OBJECT, , , &quot;L10N&quot;) Then GoTo Finally
+ End If
+
+Try:
+ &apos; Get the dialog title
+ sText = Caption
+ If Len(sText) &gt; 0 Then
+ sTranslation = L10N._(sText)
+ If sText &lt;&gt; sTranslation Then Caption = sTranslation
+ End If
+ &apos; Scan all controls
+ vControls = Controls()
+ For Each sControl In vControls
+ Set oControl = Controls(sControl)
+ With oControl
+ &apos; Extract fixed texts
+ sText = .Caption
+ If Len(sText) &gt; 0 Then
+ sTranslation = L10N._(sText)
+ If sText &lt;&gt; sTranslation Then .Caption = sTranslation
+ End If
+ vSource = .RowSource &apos; List and comboboxes only
+ If IsArray(vSource) Then
+ bChanged = False
+ For i = 0 To UBound(vSource)
+ If Len(vSource(i)) &gt; 0 Then
+ sTranslation = L10N._(vSource(i))
+ If sTranslation &lt;&gt; vSource(i) Then
+ bChanged = True
+ vSource(i) = sTranslation
+ End If
+ End If
+ Next i
+ &apos; Rewrite if at least 1 item has been modified by the translation process
+ If bChanged Then .RowSource = vSource
+ End If
+ sText = .TipText
+ If Len(sText) &gt; 0 Then
+ sTranslation = L10N._(sText)
+ If sText &lt;&gt; sTranslation Then .TipText = sTranslation
+ End If
+ End With
+ Next sControl
+
+ bGet = True
+
+Finally:
+ GetTextsFromL10N = bGet
+ SF_Utils._ExitFunction(cstThisSub)
+ Exit Function
+Catch:
+ GoTo Finally
+End Function &apos; SFDialogs.SF_Dialog.GetTextsFromL10N
+
+REM -----------------------------------------------------------------------------
Public Function Methods() As Variant
&apos;&apos;&apos; Return the list of public methods of the Model service as an array
@@ -507,6 +599,7 @@ Public Function Methods() As Variant
, &quot;Controls&quot; _
, &quot;EndExecute&quot; _
, &quot;Execute&quot; _
+ , &quot;GetTextsFromL10N&quot; _
, &quot;Terminate&quot; _
)
diff --git a/wizards/source/sfdialogs/SF_DialogControl.xba b/wizards/source/sfdialogs/SF_DialogControl.xba
index 42270e32bcd3..f4a0891d92c4 100644
--- a/wizards/source/sfdialogs/SF_DialogControl.xba
+++ b/wizards/source/sfdialogs/SF_DialogControl.xba
@@ -1440,7 +1440,7 @@ Try:
Case &quot;TreeControlModel&quot;
&apos; Initialize the data model
_ControlType = CTLTREECONTROL
- Set _ControlModel.DataModel = ScriptForge.SF_Utils._GetUNOService(&quot;TreeDataModel&quot;)
+ Set _ControlModel.DataModel = CreateUnoService(&quot;com.sun.star.awt.tree.MutableTreeDataModel&quot;)
Set _TreeDataModel = _ControlModel.DataModel
Case &quot;UnoControlGridModel&quot;
_ControlType = CTLTABLECONTROL
@@ -2081,4 +2081,4 @@ Private Function _Repr() As String
End Function &apos; SFDialogs.SF_DialogControl._Repr
REM ============================================ END OF SFDIALOGS.SF_DIALOGCONTROL
-</script:module>
+</script:module> \ No newline at end of file
diff --git a/wizards/source/sfdialogs/SF_Register.xba b/wizards/source/sfdialogs/SF_Register.xba
index b99c9e337526..11ee1543b412 100644
--- a/wizards/source/sfdialogs/SF_Register.xba
+++ b/wizards/source/sfdialogs/SF_Register.xba
@@ -220,7 +220,7 @@ End Function &apos; SFDialogs.SF_Register._FindDialogInCache
REM -----------------------------------------------------------------------------
Public Function _NewDialog(Optional ByVal pvArgs As Variant) As Object
&apos;&apos;&apos; Create a new instance of the SF_Dialog class
-&apos; Args:
+&apos;&apos;&apos; Args:
&apos;&apos;&apos; Container: either &quot;GlobalScope&quot; or a WindowName. Default = the active window
&apos;&apos;&apos; see the definition of WindowName in the description of the UI service
&apos;&apos;&apos; Library: the name of the library hosting the dialog. Default = &quot;Standard&quot;