isladogs
MVP / VIP
- Local time
- Today, 10:51
- Joined
- Jan 14, 2017
- Messages
- 18,717
This sample database builds on the code that @arnelgp used in his sample Language Translator database in this thread Language Translator | Access World Forums (access-programmers.co.uk). Text translation uses the online Google Translate service and was based on that arnelgp found at How to Parse HTML from Description Field - ALM/QC User Discussions - ALM / Quality Center (microfocus.com)
Many thanks to @arnelgp for his input on this topic
As the topic interested me, I've now added several new features, notably the use of text to speech (TTS).
In addition, the following items are included:
a) code to check for an Internet connection when the form is loaded
b) reverse translate - useful for checking accuracy of Google translate and for translating into additional languages
c) copy text to clipboard
d) character count (including spaces etc) - tests indicate a limit of around 9800 characters can be translated. If you exceed that limit, there is no output. Interestingly, the character limit if you type / paste text directly into Google Translate is just 500!
Using Text to Speech requires the Microsoft Speech Object Library reference.
Note that there are two different references with that name. Make sure you use the reference shown below:
The selected text is exported to a VB script file (Text2Speech.vbs in the program folder) which is then 'executed' i.e. read aloud in the selected voice
The text to speech function is:
Here is an example of the VB Script file created:
For the best results, install additional voices from text to speech settings
In the above example, the text is spoken in French
If no voice is available for the selected language, the text is spoken in the default language:
During testing, I installed voices for all available languages from Windows Settings. There are a total of 75 voices in 48 languages.
For details, see Appendix A: Supported languages and voices (microsoft.com)
Disappointingly only 16 of those voices were available on my test workstations using text to speech:
16 available voices:
0 - Microsoft Hazel Desktop - English (Great Britain)
1 - Microsoft Hedda Desktop - German
2 - Microsoft David Desktop - English (United States)
3 - Microsoft Zira Desktop - English (United States)
4 - Microsoft Helena Desktop - Spanish (Spain)
5 - Microsoft Sabina Desktop - Spanish (Mexico)
6 - Microsoft Hortense Desktop - French
7 - Microsoft Elsa Desktop - Italian (Italy)
8 - Microsoft Haruka Desktop - Japanese
9 - Microsoft Heami Desktop - Korean
10 - Microsoft Paulina Desktop - Polish
11 - Microsoft Maria Desktop - Portuguese(Brazil)
12 - Microsoft Irina Desktop - Russian
13 - Microsoft Huihui Desktop - Chinese (Simplified)
14 - Microsoft Tracy Desktop - Chinese(Traditional, HongKong SAR)
15 - Microsoft Hanhan Desktop - Chinese (Taiwan)
Voices 8, 9, 12, 13, 14, 15 can't be used for TTS in Access as the character set cannot be read
As a result, languages such as Chinese, Japanese or Russian aren't exported to the VBS file
In such cases, a message like this is shown
However, in my tests, the following 8 languages all worked successfully:
8 available voices:
0 - Microsoft Hazel Desktop - English (Great Britain)
1 - Microsoft Hedda Desktop - German
2 - Microsoft Zira Desktop - English (United States)
3 - Microsoft Helena Desktop - Spanish (Spain)
4 - Microsoft Hortense Desktop - French
5 - Microsoft Elsa Desktop - Italian (Italy)
6 - Microsoft Paulina Desktop - Polish
7 - Microsoft Maria Desktop - Portuguese(Brazil)
I would be very interested if anyone can explain how to make use of all additional voices which can be installed
NOTE:
1. Four characters used in URLs (&, +, %, #) caused issues for the translate code
The '+' is ignored and any text after '&' or '#' is omitted. Using a % results in all spaces becoming %20 as well
Testing with a phrase like 'You + me & Fred' resulted in the output 'You me' (if 'translated' to English!). Similarly '50% profit #3' became '50%%20profit%20'
I've done a quick work round replacing those 4 characters with very unlikely character strings (at least unlikely in English).
I have also added code to handle line returns correctly in translated text output
See the modified function GglTranslate in modTranslate
2. Similarly, errors occurred when text used in the speech feature included quote marks or line returns.
I have added code to ignore these characters when the text is spoken. For example
3. The spoken text will be played in its entirety from the VBS script created.
I am not aware of any code which can be used to interrupt/stop it once started. Any info on ways of doing this would be welcomed
Many thanks to @arnelgp for his input on this topic
As the topic interested me, I've now added several new features, notably the use of text to speech (TTS).
In addition, the following items are included:
a) code to check for an Internet connection when the form is loaded
b) reverse translate - useful for checking accuracy of Google translate and for translating into additional languages
c) copy text to clipboard
d) character count (including spaces etc) - tests indicate a limit of around 9800 characters can be translated. If you exceed that limit, there is no output. Interestingly, the character limit if you type / paste text directly into Google Translate is just 500!
Using Text to Speech requires the Microsoft Speech Object Library reference.
Note that there are two different references with that name. Make sure you use the reference shown below:
The selected text is exported to a VB script file (Text2Speech.vbs in the program folder) which is then 'executed' i.e. read aloud in the selected voice
The text to speech function is:
Rich (BB code):
Public Function Text2Speech()
On Error GoTo Err_Handler
Dim J As Long
Dim voc As SpeechLib.SpVoice
Dim strFile As String
IP = Environ("ComputerName")
strFile = CurrentProject.Path & "\Text2Speech.vbs"
'check available voices
Set voc = New SpVoice
For J = 0 To voc.GetVoices.Count - 1
Set voc.Voice = voc.GetVoices.Item(J)
If InStr(voc.Voice.GetDescription, LANG) > 1 Then GoTo SetVoice
Next J
J = 0 'use default language if no match found
SetVoice:
'vbs command to send
A = "on error resume next" & vbCrLf & _
" Dim voc" & vbCrLf & _
" Set voc = CreateObject(""SAPI.SpVoice"")" & vbCrLf & _
" Set voc.Voice = voc.GetVoices.Item(" & J & ")" & vbCrLf & _
" voc.speak " & """" & MSG & """" & vbCrLf & _
" CreateObject(""Scripting.FileSystemObject"").DeleteFile strFile"
CreateObject("Scripting.FileSystemObject").OpenTextFile(strFile, 2, True).Write A
'Run the VBS through Wscript via WMI Object Win32_Process
B = GetObject("winmgmts:\\" & IP & "\root\cimv2:Win32_Process").Create("C:\windows\system32\wscript.exe" & strFile, Null, Null, intProcessID)
If Not GetDefaultVoice Like LANG & "*" Then 'not default language
If J = 0 And Err = 0 And Not GetDefaultVoice Like LANG & "*" Then 'text can be read but no default voice available
FormattedMsgBox "Voice not available for " & LANG & _
"@Text will be spoken in the default language: " & GetDefaultVoice & " @", vbExclamation, "Voice not available"
Else
'text can't be read so causing error 5 (handled below)
End If
End If
'run the script
Shell "wscript " & strFile, vbNormalFocus
Exit_Handler:
Exit Function
Err_Handler:
If Err = 70 Or Err = 2465 Then Resume Next
If Err = 5 Then 'character set not readable so TTS isn't available
FormattedMsgBox "CRITICAL ERROR" & _
"@Text to Speech is NOT available for " & LANG & ". @", vbCritical, "Text to Speech error"
Else
MsgBox "Error " & Err.Number & " in Text2Speech procedure: " & Err.Description
End If
Resume Exit_Handler
End Function
Here is an example of the VB Script file created:
For the best results, install additional voices from text to speech settings
In the above example, the text is spoken in French
If no voice is available for the selected language, the text is spoken in the default language:
During testing, I installed voices for all available languages from Windows Settings. There are a total of 75 voices in 48 languages.
For details, see Appendix A: Supported languages and voices (microsoft.com)
Disappointingly only 16 of those voices were available on my test workstations using text to speech:
16 available voices:
0 - Microsoft Hazel Desktop - English (Great Britain)
1 - Microsoft Hedda Desktop - German
2 - Microsoft David Desktop - English (United States)
3 - Microsoft Zira Desktop - English (United States)
4 - Microsoft Helena Desktop - Spanish (Spain)
5 - Microsoft Sabina Desktop - Spanish (Mexico)
6 - Microsoft Hortense Desktop - French
7 - Microsoft Elsa Desktop - Italian (Italy)
8 - Microsoft Haruka Desktop - Japanese
9 - Microsoft Heami Desktop - Korean
10 - Microsoft Paulina Desktop - Polish
11 - Microsoft Maria Desktop - Portuguese(Brazil)
12 - Microsoft Irina Desktop - Russian
13 - Microsoft Huihui Desktop - Chinese (Simplified)
14 - Microsoft Tracy Desktop - Chinese(Traditional, HongKong SAR)
15 - Microsoft Hanhan Desktop - Chinese (Taiwan)
Voices 8, 9, 12, 13, 14, 15 can't be used for TTS in Access as the character set cannot be read
As a result, languages such as Chinese, Japanese or Russian aren't exported to the VBS file
In such cases, a message like this is shown
However, in my tests, the following 8 languages all worked successfully:
8 available voices:
0 - Microsoft Hazel Desktop - English (Great Britain)
1 - Microsoft Hedda Desktop - German
2 - Microsoft Zira Desktop - English (United States)
3 - Microsoft Helena Desktop - Spanish (Spain)
4 - Microsoft Hortense Desktop - French
5 - Microsoft Elsa Desktop - Italian (Italy)
6 - Microsoft Paulina Desktop - Polish
7 - Microsoft Maria Desktop - Portuguese(Brazil)
I would be very interested if anyone can explain how to make use of all additional voices which can be installed
NOTE:
1. Four characters used in URLs (&, +, %, #) caused issues for the translate code
The '+' is ignored and any text after '&' or '#' is omitted. Using a % results in all spaces becoming %20 as well
Testing with a phrase like 'You + me & Fred' resulted in the output 'You me' (if 'translated' to English!). Similarly '50% profit #3' became '50%%20profit%20'
I've done a quick work round replacing those 4 characters with very unlikely character strings (at least unlikely in English).
I have also added code to handle line returns correctly in translated text output
See the modified function GglTranslate in modTranslate
2. Similarly, errors occurred when text used in the speech feature included quote marks or line returns.
I have added code to ignore these characters when the text is spoken. For example
Rich (BB code):
Private Sub cmdSpeakTo_Click()
MSG = Replace(Me.txtTo, """", "") 'remove quotes to prevent TTS error
MSG = Replace(MSG, Chr(13) & Chr(10), ".") 'remove line feeds to prevent TTS error
LANG = cboTo.Column(1)
Text2Speech
End Sub
3. The spoken text will be played in its entirety from the VBS script created.
I am not aware of any code which can be used to interrupt/stop it once started. Any info on ways of doing this would be welcomed
Attachments
Last edited: