Using translate in Visual Basic 2013

1,559 views
Skip to first unread message

Ole Jul Larsen

unread,
Mar 12, 2016, 10:29:45 AM3/12/16
to Google Translate API Developer Forum
Based on findings from 2012 on the net, I have tried out this code:

Imports System.Collections.Generic
Imports System.ComponentModel
Imports Google.Apis.Translate.v2
Imports Google.Apis.Translate.v2.Data
Imports TranslationsResource = Google.Apis.Translate.v2.Data.TranslationsResource
Public Class Form1

    Private Sub Translate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Translate.Click
        Dim test As New TranslateService()
        test.Key = "My 39 characters API Key"

        Dim response As TranslationsListResponse = test.Translations.List(Txtfrom.Text, "da").Fetch()

        Dim translations As New List(Of String)()
        For Each t As TranslationsResource In response.Translations
            'Console.WriteLine(t.TranslatedText)

            txtto.Text = t.TranslatedText
        Next
    End Sub
End Class

My projects Service References includes:
Google.Apis
Type   .NET
Version 1.1.4497.35846

Google.Apis.Translate.v2
Type .NET
Version 1.1.4497.35829

It does not work - at debug 'Dim test As New TranslateService()' throws:

A first chance exception of type 'System.TypeInitializationException' occurred in Google.Apis.dll


The result language in the example is French but as my source text is rather technical I would like to define both source (English) and result language (Swedish,Polish and Russian). 

Further I don't fully understand the For Each loop getting the resulting translation.

Ole Jul Larsen

unread,
Mar 14, 2016, 2:33:29 PM3/14/16
to Google Translate API Developer Forum
I now found another method using a http request like this:

        Dim Uri As String = "https://www.googleapis.com/language/translate/v2?key=My API Key&source=" & FromLang.Text & "&target=" & ToLang.Text & "&q=" & TextFrom.Text
        Dim Result As String = webClient.DownloadString(Uri)

I works fine but does not show the Russian result properly (and for that matter some national characters in Swedish, Polish and Danish).

I presume some language definition has to be included somehow?

I checked my VB by loading a file containing cyrillic - that is OK in the text box on my form so my problem must be a matter between the HTTP result and VB.NET

I am aware of the format returned but that is easy to parse.

Nicholas (Google Cloud Support)

unread,
Mar 18, 2016, 5:02:06 PM3/18/16
to Google Translate API Developer Forum
Thank you for posting your questions here. Could you elaborate on what you mean by "does not show the Russian result properly"? Also, I'm not sure what you mean by "language definition".

To the best of my knowledge, this client library is essentially issue a request to the Translate REST API. The Query parameter reference explains what parameters can be specified in the request to 'https://www.googleapis.com/language/translate/v2'. In addition, the Language reference shows what language codes can be used to specify the source and target languages.

It may also be helpful if you could provide the request headers your application sends along with the response received. If you're not certain the problem is occurring with your application, you can test API calls using the API explorer.

Hope this information is helpful. 

Ole Jul Larsen

unread,
Mar 19, 2016, 5:56:22 PM3/19/16
to Google Translate API Developer Forum
Hi Nicolas,

Thanks' your response.

I have concluded that my problem is within Visual Basic:

My English text for testing is:
This a test from english to russian

Response using https://translate.google.com/ or the API Explorer:
Этот тест с английского на русский

(I don't read russian but presume it is correct)

Response in Visual Studio VB.NET using the API:
Этот Ñ‚ÐµÑ Ñ‚ Ñ Ð°Ð½Ð³Ð»Ð¸Ð¹Ñ ÐºÐ¾Ð³Ð¾ на Ñ€ÑƒÑ Ñ ÐºÐ¸Ð¹

This is what I would call the raw UTF-8 format which I succeeded to convert correctly by using my own designed converter so the final result is exactly as from the API explorer.
So my problem seems to be solved but I don't see why Visual Studio apparently is not with same standard as other MS applikations.






Nicholas (Google Cloud Support)

unread,
Mar 21, 2016, 5:03:50 PM3/21/16
to Google Translate API Developer Forum
I'm glad to hear you've successfully identified the source of the issue.

I'm not very familiar with Visual Basic 2013. It may be helpful to get the request headers being sent by your Visual Basic application along with the response headers in the event that this is causing an unnecessary conversion to ASCII. Again, I'm not sure what VB debugging allows, but if you inspect the request and response only to determine they are both working as expected, I would look for possible implicit charset conversions by the VB UI or logging tool (however it is you're finding the result to be Этот Ñ‚ÐµÑ Ñ‚ Ñ Ð°Ð½Ð³Ð»Ð¸Ð¹Ñ ÐºÐ¾Ð³Ð¾ на Ñ€ÑƒÑ Ñ ÐºÐ¸Ð¹)

For instance, many VB UI form controls have a charset attribute that defaults to ASCII. This can be changed. Also, some console output tools don't display unicode by default and also do an implicit conversion. It's therefore entirely possible that the data is in fact properly handled in unicode through and through while simply being displayed in ASCII.

Hope this helps.

Ole Jul Larsen

unread,
Mar 21, 2016, 5:50:02 PM3/21/16
to Google Translate API Developer Forum
Hi Nicolas,

Thanks' again for your interest.

My little ASCII to UTF-8 converter works fine - I studied the specifications for UTF-8 and it took me a while to fully grasp the concept..

I have attached my code in case some others might use it.

    Public Function UTF8Convert(strInput As String) As String
        Dim AscValue As Integer = 0
        Dim strConverted As String = vbEmpty
        Dim Adder As Integer = 0
        For c As Integer = 0 To strInput.Length - 1
            AscValue = Asc(strInput.Substring(c, 1))
            If AscValue < 128 Then
                'Standard ASCII
                strConverted &= strInput.Substring(c, 1)
            Else
                'Convert to UTF-8
                Adder = (Convert.ToInt16(Format(Asc(strInput.Substring(c, 1)), "X2").Substring(0, 1), 16) - 12) * 1024 + Convert.ToInt16(Format(Asc(strInput.Substring(c, 1)), "X2").Substring(1, 1), 16) * 64
                c += 1
                AscValue = Asc(strInput.Substring(c, 1))
                AscValue = AscValue And &H7F
                AscValue += Adder
                strConverted &= ChrW(AscValue)
            End If
        Next
        Return strConverted
    End Function







































Nicholas (Google Cloud Support)

unread,
Mar 22, 2016, 10:47:22 AM3/22/16
to Google Translate API Developer Forum
Thanks for following up with your issue and sharing your solution with the community. It is much appreciated!

Regards,
Nicholas
Reply all
Reply to author
Forward
0 new messages