Translate text using vba

后端 未结 5 1161
既然无缘
既然无缘 2020-11-29 07:08

Probably could be a rare petition, but here is the issue.

I am adapting an excel of a third-party to my organization. The excel is developed in English and the peop

5条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-29 07:45

    One of the modern solution using Google Translation API To Enable Google Translation API, first you should create the project and credentials. If you receive 403 (Daily Limit), you need to add payment method into your Google Cloud Account, then you will get results instantly.

    Private Function GoogleTranslateJ(ByVal text, ByVal resLang, ByVal srcLang) As String
    Dim jsonProvider As Object
    
    Dim jsonResult As Object
    Dim jsonResultText As String
    
    Dim googleApiUrl As String
    Dim googleApiKey As String
    
    Dim resultText As String
    
    Set jsonProvider = CreateObject("MSXML2.ServerXMLHTTP")
    
    text = Replace(text, " ", "%20")
    googleApiKey = "ijHF28h283fjijefiwjeofij90f2h923" 'YOUR GOOGLE API KEY
    
    googleApiUrl = "https://translation.googleapis.com/language/translate/v2?key=" & googleApiKey & "&source=" & srcLang & "&target=" & resLang & "&q=" & text
    
    jsonProvider.Open "POST", googleApiUrl, False
    jsonProvider.setRequestHeader "Content-type", "application/text"
    jsonProvider.send ("")
    jsonResultText = jsonProvider.responseText
    
    Set jsonResult = JsonConverter.ParseJson(jsonResultText)
    Set jsonResult = jsonResult("data")
    Set jsonResult = jsonResult("translations")
    Set jsonResult = jsonResult(1)
    
    resultText = jsonResult("translatedText")
    
    GoogleTranslateJ = resultText
    End Function
    

提交回复
热议问题