1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
| Imports System.Speech.Synthesis
Imports System.Globalization
Public Class Form1
Dim language As String
Dim reader As New SpeechSynthesizer()
Dim listLanguage As String() = {"fr-FR", "en-US", "es-ES"}
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
If ComboBox1.Items.Count > 0 Then
ComboBox1.SelectedIndex = 0
End If
InstalleVoices()
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
If Not String.IsNullOrEmpty(TextBox2.Text) Then
reader.Volume = 100
reader.Rate = 0
reader.SpeakAsync(TextBox2.Text)
End If
End Sub
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
If Not String.IsNullOrEmpty(TextBox1.Text) Then
Dim threads As New Threading.Thread(Sub()
TextBox2.Invoke(Sub() TextBox2.Clear())
Dim traduction As String = TranslateText(TextBox1.Text, language)
TextBox2.Invoke(Sub() TextBox2.Text = traduction)
End Sub)
threads.Start()
End If
End Sub
Private Function TranslateText(input As String, langue As String) As String
Dim result As String = ""
Try
Dim webClients As New System.Net.WebClient()
webClients.Encoding = System.Text.Encoding.UTF8
webClients.Headers.Add("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.104 Safari/537.36")
result = webClients.DownloadString(String.Format("https://clients5.google.com/translate_a/t?client=dict-chrome-ex&sl=fr&tl=" & langue & "&q=" & input))
Catch ex As System.Net.WebException
MsgBox(ex, MsgBoxStyle.Information)
End Try
Return result.Substring(result.IndexOf("[""") + 2, result.LastIndexOf("""]") - 2)
End Function
Private Sub ComboBox1_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
If ComboBox1.Text = "francais" Then
language = "fr"
ElseIf ComboBox1.Text = "anglais" Then
language = "en"
ElseIf ComboBox1.Text = "espagnol" Then
language = "es"
End If
'...etc
End Sub
Private Sub InstalleVoices()
For i = 0 To listLanguage.Length - 1
For Each voice As InstalledVoice In reader.GetInstalledVoices(New CultureInfo(listLanguage(i)))
Dim info As VoiceInfo = voice.VoiceInfo
ComboBox2.Items.Add(info.Name)
If ComboBox2.Items.Count > 0 Then
ComboBox2.SelectedIndex = 0
End If
Next
Next
End Sub
Private Sub ComboBox2_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ComboBox2.SelectedIndexChanged
Dim voice As String = CType(ComboBox2.SelectedItem, String)
If voice IsNot Nothing Then
reader.SelectVoice(voice)
End If
End Sub
End Class |
Partager