let
fnTranslate = (Original as text, SourceLanguageCode, LanguageCode as text) as list =>
let
PostContent = "q=" & Original,
WebResult = Web.Contents(
"https://translate.googleapis.com/translate_a/single?client=gtx&sl=" & SourceLanguageCode & "&tl=" & LanguageCode & "&dt=t",
[
Headers= [#"Content-type"="application/x-www-form-urlencoded"],
Content=Text.ToBinary(PostContent)
]
),
JsonValue = Json.Document(WebResult),
Translation = List.Transform(
JsonValue{0},
each Text.Replace(_{0}, NewLine, "")
)
in
Translation,
Source = Excel.CurrentWorkbook(){[Name="Original"]}[Content],
Languages = Excel.CurrentWorkbook(){[Name="Languages"]}[Content],
SourceLanguageText = Table.ColumnNames(Source){0},
SourceLanguage = Text.Split(Table.ColumnNames(Source){0}, "-"),
SourceLanguageName = SourceLanguage{0},
SourceLanguageCode = SourceLanguage{1},
Original = Table.Column(Source, SourceLanguageText ),
NewLine = "#(cr)#(lf)",
CombinedText = Text.Combine(Original, NewLine),
Translation = List.Generate(
() => [i = 0],
each [i] < List.Count(Languages[Code]),
each [i = [i] + 1],
each fnTranslate(CombinedText, SourceLanguageCode, Languages[Code]{[i]})
),
Result = Table.FromColumns(
List.InsertRange(Translation, 0, {Original}),
List.InsertRange(Languages[Language], 0, {SourceLanguageName})
)
in
Result