smozgur

Translate to Multiple Languages with Power Query - Part 3

I tested the part 3, and same thing :
Thanks if you have any good idea ?

1701251254095.png
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
Hi took a VBA code from someone (on the web) and I did a quick fix to manage carriage return (or new line). it could help you :)

Function GOOGLETRANSLATE(Text As String, source_language As String, target_language As String) As String
Dim URL As String
URL = "Google Translate" & source_language & "&tl=" & target_language & "&hl=en&ie=UTF-8&q=" & CLEANTEXT(Text)
'URL = "Google Translate" & source_language & "&tl=" & target_language & "&hl=en&ie=UTF-8&q=" & Text
Dim XMLHTTPS As Object
Set XMLHTTPS = CreateObject("MSXML2.ServerXMLHTTP")
XMLHTTPS.Open "GET", URL, False
XMLHTTPS.setrequestheader "User-Agent", "Mozilla/5.0 (compatible;MSIE 6.0; WindowsNT 10.0))"
XMLHTTPS.send ""

Dim HTML As Object
Set HTML = CreateObject("HTMLFile")
With HTML
.Open
.write XMLHTTPS.responseText
.Close
End With

Dim HTMLDc As HTMLDocument
Set HTMLDc = HTML

Dim Class As Object
Set Class = HTMLDc.getElementsByClassName("result-container")(0)
If Not Class Is Nothing Then
'GOOGLETRANSLATE = Class.innerText
GOOGLETRANSLATE = SPECIALREPLACE(Class.innerText)

End If

Set Class = Nothing
Set HTML = Nothing
Set XMLHTTPS = Nothing

End Function

Function CLEANTEXT(ByVal Text As String) As String
Text = Replace(Text, "&", " <advbcommercialand> ")
Text = Replace(Text, "%", " <advbpourcent> ")
Text = Replace(Text, Chr(10), Chr(13) & Chr(10))
Text = Replace(Text, Chr(13), Chr(13) & Chr(10))
Text = Replace(Text, Chr(10) & Chr(10), Chr(10))
Text = Replace(Text, Chr(13) & Chr(10) & Chr(13) & Chr(10), Chr(13) & Chr(10))
Text = Replace(Text, Chr(13) & Chr(10), " <advb1310> ")

CLEANTEXT = Text
End Function


Function SPECIALREPLACE(ByVal Text As String) As String
Text = Replace(Text, "<advbpourcent>", "%")
Text = Replace(Text, "<advbcommercialand>", "&")
Text = Replace(Text, "<advb1310>", Chr(13) & Chr(10))
Text = Replace(Text, "<advb1310 >", Chr(13) & Chr(10))
Text = Replace(Text, "< advb1310>", Chr(13) & Chr(10))
Text = Replace(Text, "< advb1310 >", Chr(13) & Chr(10))
Text = Replace(Text, " ", " ")

SPECIALREPLACE = Text
End Function


It looks a good result :

1701258431428.png
 
In the implementations I provided as sample, you need to replace the line breaks with spaces in the source without too many modification. Note that you'll get the translation as single lines.

Power Query:
    RemoveLineBreaks = Table.TransformColumns(Source, {"English", each Text.Replace(_, "#(lf)", " ")}),

Then use RemoveLineBreaks identifier afterwards.
 
Hi thx for your reply :) the idea is to keep the line break, but with the replace function it will be fine. Thanks
 

Forum statistics

Threads
1,223,574
Messages
6,173,144
Members
452,501
Latest member
musallam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top