Skip to content

The Klingon Translator App You Definitely Need

I love the new Star Trek Discovery and there are a fair amount of Klingons in it, from time to time. It occurred to me that someone has probably built a web service to translate English to Klingon and if so, I needed to build a Xojo app to use it.

A quick Google search turned up an API by FunTranslations:

It has simple usage where you send along the text in English and you get back a JSON result containing the text translated to Klingon. Here’s the result of my 15 minutes of effort to use this in a Xojo desktop app:

This is a pretty simple project with less than 20 lines of code.

You can recreate the layout using a couple TextFields and a Button. I used URLConnection to connect to the web service. You can also add that to the layout by dragging an Object from the Library and changing its Super property to “URLConnection”. I named this object “KlingonAPI”.

In the Button’s Action event the code sets up the call to the web service using the text entered in the first TextArea:

Dim url As String = ""

Dim param As String = "text=" + EncodeURLComponent(EnglishArea.Text)

KlingonAPI.SetRequestContent(param, "application/x-www-form-urlencoded")
KlingonAPI.Send("POST", url)

In the KlingonAPI URLConnection object, add the ContentReceived event. This code grabs the translated Klingon text from the JSON and displays it in the 2nd TextArea:

  Dim json As New JSONItem(content)
  Dim contents As JSONItem
  contents = json.Value("contents")
  Dim klingonTranslation As String = contents.Value("translated")
  KlingonArea.Text = klingonTranslation
Catch e As JSONException
  MsgBox("Error processing API call. Content=" + content)
End Try

You can also add the Error event and have it display an error in case there’s a problem with the API call:

MsgBox("API Error: " + e.Message)

And that’s all there is to it.

You can download the project from here: KlingonTranslator Project, just remember some things can’t be translated!

I’ll leave it up to you to implement a Klingon to English translator, although you may want to make that a mobile app in case you are ever teleported to the bridge of a Klingon vessel.