How to Translate Text from shell script

Using the WhatsMate Translation REST API



This article shows you how to translate text from a shell script.

To translate text (e.g. from English to Indonesian) from a shell script, do this:

  1. Copy the following source code to your script.
    #!/bin/bash
    # TODO: If you have your own Client ID and secret, put down their values here:
    CLIENT_ID="FREE_TRIAL_ACCOUNT"
    CLIENT_SECRET="PUBLIC_SECRET"
    # TODO: Specify your translation requirements here:
    fromLang="en"
    toLang="id"
    text="Let's have some fun!"
    read -r -d '' jsonPayload << _EOM_
    {
    "fromLang": "$fromLang",
    "toLang": "$toLang",
    "text": "$text"
    }
    _EOM_
    curl -X POST \
    -H "X-WM-CLIENT-ID: $CLIENT_ID" \
    -H "X-WM-CLIENT-SECRET: $CLIENT_SECRET" \
    -H "Content-Type: application/json" \
    -d "$jsonPayload" \
    http://api.whatsmate.net/v1/translation/translate
    echo -e "\n=== END OF DEMO ==="
  2. Specify the language code of the original text on line 8.
  3. Specify the language code of your target language on line 9.
  4. Specify the text that needs to be translated on line 10.
  5. Make your script executable: chmod 755 translate-text.sh
  6. Run the script to see the result: ./translate-text.sh

Looking for your language code? See the complete listing.

The trial account only allows you to call the translation API up to 10 times for learning purpose. Subscribe to a Premium plan to use the translation API seriously.

Want to translate text in another programming language? Check out the Translation API page.