Example 2 - Adding checks
This example demonstrates the use of external components (Microsoft VBScript Regular Expressions) and the use of regular expression.
Although Passolo has build-in checks for C-format specifiers like %d %s we want to show how this feature could be implemented with a system macro.
If format specifiers are used in the texts, the order of the format specifiers must not be changed in the translation, otherwise data will be displayed wrong or the software might even crash. To add this additional check you have to implement the call back handler PSL_OnCheckString.
0001 Public Sub PSL_OnCheckString(Ctl As PslTransString)
0002 Dim i As Integer
0003 Dim srccoll As MatchCollection
0004 Dim trgcoll As MatchCollection
0005 Dim regexpr As RegExp
0006 Dim srcstring As String
0007 Dim trgstring As String
0008
0009 ' If we dont have to deal with string list
0010 If Ctl.ResType <> "StringTable" Then Exit Sub
0011
0012 ' No C-format specifiers
0013 If InStr(Ctl.SourceText, "%") = 0 And _
0014 InStr(Ctl.Text, "%") = 0 Then Exit Sub
0015
0016 Set regexpr = New RegExp
0017 regexpr.Pattern = _
0018 "([^%]|^)%-?\d*\.?\d*(ld|lo|lx|d|o|x|h|c|s|f|n)"
0019 regexpr.IgnoreCase = True
0020 regexpr.Global=True
0021 Set srccoll = regexpr.Execute(Ctl.SourceText)
0022 Set trgcoll = regexpr.Execute(Ctl.Text)
0023
0024 If srccoll.Count <> trgcoll.Count Then
0025 Ctl.OutputError _
0026 "Different number of format specifiers"
0027 Exit Sub
0028 End If
0029
0030 For i = 0 To srccoll.Count - 1
0031 srcstring = srccoll.Item(i)
0032 trgstring = trgcoll.Item(i)
0033
0034 If Left(srcstring,1) <> "%" Then
0035 srcstring = Mid(srcstring, 2)
0036 End If
0037
0038 If Left(trgstring,1) <> "%" Then
0039 trgstring = Mid(trgstring,2)
0040 End If
0041
0042 If srcstring <> trgstring Then
0043 Ctl.OutputError _
0044 CStr(i + 1) + ". Format specifier different"
0045 Exit Sub
0046 End If
0047 Next
0048 End Sub
We use a RegExp object to deal with regular expressions (Lines 3 to 5). This object is not part of the SAX Basic Runtime Library. So we need to add a reference to that component. The topic Project References describes how to do this. Choose Microsoft VBScript Regular Expressions 5.5 which is the component we need.
Line 17/18 sets the regular expression which describes the layout of the C-format specifiers. Using this expression we extract in line 21 and 22 collections of strings from both the source and target string that fits this pattern.
If the number of strings is different in the source or target collection an error message will be generated in line 25. The member function OuputError writes the message to the output window together with information about the translation string object itself. In Passolo you can use F4 to jump to the translation string as you can do it also with errors or warning generated by the Passolo itself.
In the For loop the C-format specifiers are checked one by one to see if the order is the same for both source and target string.
How to use it
Select PslCheckCPP as the system macro in the dialog Macro and active the system macro with the command Tools / System Macro / Start System Macro.
Open a translation list and select Translation / Check all Translations. If the string list contains texts that violate the check, you should get error messages like in the following example: