Message Boards Message Boards


[GIF]One Click Translation in Palette with Azure

Posted 23 days ago
0 Replies
2 Total Likes

Please Download the package file at the end of the dicussion

Wolfram Langue developers and Mathematica users are all over the world and speak different languages. Automatic translation of their notebooks contents, even partial, would be very handy for these creators to share their works to audiens from varies background.

I will illustrate how you can create a palette with simple GUI to tranlate the content in a text cell automatically based on external Azure Cognitive Service. Though the following example is based on MS Translation, any Cognitive Service with Restful API can be configured in the same way in Wolfram Language.

Besides Mathematica, You will need

  • a free Azure account
  • API key for Cognitive Service

Once you are done with these configuration, you can follow the instruction below to create an one-click smart bi-directional translator in Mathematica.

For example,

  • one-click from English to ZH-CHS


  • One-click from ZH-CHS to English


The features covered here:

  • ServiceConnect -- core functionality
  • Notebook Manipulation -- content extraction
  • CreatePalette function -- user interface


A wrapper function is defined for ServiceConnect to make an API call and do the translation between ZH-CHS and English/en

MSTranslatorService[txt_String,direction_] := Module[{langlist,translationDs},
      translationDs = ServiceExecute["MicrosoftTranslator", "GetTranslations", {
             "Text" -> txt, 
             "From" -> langlist["From"], 
             "To" -> langlist["To"], 
             "MaxTranslations" -> "5"

A test function is created at same time. It just makes a simple call to the MSTranslateService to translate "Hello" to ZH-CHS, which of course works assuming the connection is configured properly.

Notebook Manipulation

You need to break down the action before you can program something to automatically extract the content of the cell from a notebook

  • Use the notebook I want to paste texual content, call InputNotebook[]
  • You paste the textual content into the InputNotebook[]. Then you click the translation button immediately after paste. For FrontEnd, You are still selecting the last cell. Thus the following code works:

    cell = NotebookRead[SelectedCells[InputNotebook[]][[1]]]/.{ Cell[txt_, type_, __]/;MemberQ[{"Text,Title,Subsection"},type] :> {txt,type}}},

The rule appended is to tell Mathematica that I only want to translate cells with Text, Title or Subsection StyleData.

  • You have extracted the textual content of the cell and then print the translated text inside a window, which is sitting below the current texual cell

    SelectionMove[InputNotebook[], Next, Cell];

The key here is that you must pass a low level Cell[BoxData[MakeBoxes[ ... ]]] object back to the InputNotebook object.


Everything you have created above are wrapped in

CreatePalette[{ ExpressionCell[.DynamicModule[{ }, ... }] ] }]

Use DynamicModule here because all actions are controlled by Button and RadioButton. They require Dynamic function to wrap around what gets updated upon click.

     RadioButtonBar[Dynamic[dir],{"FromZH","ToZH"} ],
              SelectionMove[InputNotebook[], All, CellContents];
              (* Code in Notebook Manipulation section *)  

Feel free to use Panel, Colum and Grid to align the controls in a visually-pleasing way.

Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
or Discard

Group Abstract Group Abstract