Translation Standalone Hello World

Translation Standalone Hello World

September 27, 2020

example java google-cloud translation

This is a command line program that uses Google Translate to translate some text.

View the code for this project here.

Download the code as a .zip from DownGit here.

This example prints out the Spanish translation for the string "Happy coding!".


pom.xml is a Maven POM file that defines the project.

<project xmlns=""




</project> contains a main() method that sends a translation request and outputs the result.

package io.happycoding.translation;


public class TranslationHelloWorld {

  public static void main(String[] args) {

    String originalText = "Happy coding!";

    // Create an instance of Translate to send the translation request
    Translate translate = TranslateOptions.getDefaultInstance().getService();

    // Translate into Spanish (es)
    Translation translation =
        translate.translate(originalText, TranslateOption.targetLanguage("es"));

    // Get the translated text
    String translatedText = translation.getTranslatedText();

    // Print the translated text
    System.out.println("Translated text: " + translatedText);

Run this project by executing this command:

mvn package exec:java

Learn more in these tutorials:

Translation Examples


Happy Coding is a community of folks just like you learning about coding.
Do you have a comment or question? Post it here!

Comments are powered by the Happy Coding forum. This page has a corresponding forum post, and replies to that post show up as comments here. Click the button above to go to the forum to post a comment!