• If you would like to post, please check out the MrExcel Message Board FAQ and register here. If you forgot your password, you can reset your password.
  • Excel articles and downloadable files provided in the articles have not been reviewed by MrExcel Publishing. Please apply the provided methods / codes and open the files at your own risk. If you have any questions regarding an article, please use the Article Discussion section.
smozgur

Translate to Multiple Languages with Power Query - Part 3

Bonus: Translating from any language instead of English.

@Tejasp asked if we can make the source language dynamic instead of translating from English only. Therefore, we can translate from another language to English as well.

We can change the M code a bit to make it work as shown below.

Power Query:
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

Note that we are now setting the Original table header to include the language name with the language code, i.e. "Turkish-tr", and parsing this information in the code to dynamically set up the source language. Therefore, we can set the Original table header as Turkish-tr, fill the table with Turkish sentences, and include the English in the Languages table to translate from Turkish to English.
Turkish-trLanguageCode
Merhaba karanlık, benim eski dostumEnglishen
Seninle tekrar konuşmaya geldimItalianit
Çünkü hafifçe sürünen bir vizyonGermande
Ben uyurken tohumlarını bıraktıFrenchfr
Ve beynime yerleştirilen vizyon
Hala aynı
Sessizliğin sesinin içinde
Huzursuz rüyalarda yalnız yürüdüm
Arnavut kaldırımlı dar sokaklar
Sokak lambasının halesinin altında
Yakamı soğuğa ve nemliye çevirdim
Gözlerim neon bir ışığın parıltısıyla bıçaklandığında
Geceyi bölen
Ve sessizliğin sesine dokundu
Ve çıplak ışıkta gördüm
On bin insan, belki daha fazla
Konuşmadan konuşan insanlar
Dinlemeden duyan insanlar
Seslerin asla paylaşmadığı şarkılar yazan insanlar
Ve kimse cesaret edemedi
Sessizliğin sesini boz
Aptallar dedim ki, "Bilmiyorsun
Sessizlik tıpkı bir kanser gibi büyüyor
Sana öğretebileceğim sözlerimi duy
Kollarımı tut ki sana ulaşabilirim "
Ama sessiz yağmur damlaları gibi sözlerim düştü
Ve sessizliğin kuyularında yankılandı
Sessizliğin sesinin içinde

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