question

InventerioSoft-7996 avatar image
0 Votes"
InventerioSoft-7996 asked ·

Microsoft Translation API for Winform

I'm trying to use the Microsoft Translation API in VB.Net, I'm using the Microsoft sample codes (I converted the code from C# to VB.Net), it was working fine until a month ago, but now, it's giving an error.
Does anyone know the solution for this?
Thanks in advance

The code I used:

 Private Async Sub ArabicTranslation()
     Dim route As String = "/translate?api-version=3.0&to=ar"
     Dim textToTranslate As String = txtProductName.Text
     Dim body As Object() = New Object() {New With {Key .Text = textToTranslate}}
     Dim requestBody = JsonConvert.SerializeObject(body)
     Using client = New HttpClient()
         Using request = New HttpRequestMessage()
             request.Method = HttpMethod.Post
             request.RequestUri = New Uri(endpoint_var & route)
             request.Content = New StringContent(requestBody, Encoding.UTF8, "application/json")
             request.Headers.Add("Ocp-Apim-Subscription-Key", key_var)
             request.Headers.Add("Ocp-Apim-Subscription-Region", region_var)
             Dim response As HttpResponseMessage = Await client.SendAsync(request).ConfigureAwait(False)
             Dim result As String = Await response.Content.ReadAsStringAsync()
             Dim deserializedOutput As TranslationResult() = JsonConvert.DeserializeObject(Of TranslationResult())(result)
             For Each o As TranslationResult In deserializedOutput
                 For Each t As Translation In o.Translations
                     txtArabicName.Text = t.Text
                 Next
             Next
         End Using
     End Using
 End Sub

![72597-2.png][2]
[2]: /answers/storage/attachments/72597-2.png

dotnet-csharpdotnet-visual-basicazure-translator
1.png (247.2 KiB)
2.png (239.6 KiB)
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

vb2ae avatar image
2 Votes"
vb2ae answered ·

I would look at the Json you are receiving from the Translation Service. The error sounds like you are only getting one TranslationResult instead of an array of TranslationResult. Put a breakpoint on line 1159 and look at what result is.
You might have to copy result into the clipboard and use paste special --> paste json as classes to update the class you are deserializing into

·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.