com.google.cloud.translate.TranslateOptions Java Examples

The following examples show how to use com.google.cloud.translate.TranslateOptions. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DetectLanguageAndTranslate.java    From google-cloud-java with Apache License 2.0 6 votes vote down vote up
public static void main(String... args) {
  // Create a service object
  //
  // If no explicit credentials or API key are set, requests are authenticated using Application
  // Default Credentials if available; otherwise, using an API key from the GOOGLE_API_KEY
  // environment variable
  Translate translate = TranslateOptions.getDefaultInstance().getService();

  // Text of an "unknown" language to detect and then translate into English
  final String mysteriousText = "Hola Mundo";

  // Detect the language of the mysterious text
  Detection detection = translate.detect(mysteriousText);
  String detectedLanguage = detection.getLanguage();

  // Translate the mysterious text to English
  Translation translation =
      translate.translate(
          mysteriousText,
          TranslateOption.sourceLanguage(detectedLanguage),
          TranslateOption.targetLanguage("en"));

  System.out.println(translation.getTranslatedText());
}
 
Example #2
Source File: GoogleTranslateAuth.java    From jbpm-work-items with Apache License 2.0 5 votes vote down vote up
public Translate getTranslationService(String apiKey) throws Exception {
    try {
        return TranslateOptions.newBuilder().setApiKey(apiKey).build().getService();
    } catch (Exception e) {
        throw new IllegalArgumentException(e);
    }
}
 
Example #3
Source File: BackgroundContextListener.java    From getting-started-java with Apache License 2.0 5 votes vote down vote up
@Override
public void contextInitialized(ServletContextEvent event) {
  String firestoreProjectId = System.getenv("FIRESTORE_CLOUD_PROJECT");
  Firestore firestore = (Firestore) event.getServletContext().getAttribute("firestore");
  if (firestore == null) {
    firestore =
        FirestoreOptions.getDefaultInstance().toBuilder()
            .setProjectId(firestoreProjectId)
            .build()
            .getService();
    event.getServletContext().setAttribute("firestore", firestore);
  }

  Translate translate = (Translate) event.getServletContext().getAttribute("translate");
  if (translate == null) {
    translate = TranslateOptions.getDefaultInstance().getService();
    event.getServletContext().setAttribute("translate", translate);
  }

  Publisher publisher = (Publisher) event.getServletContext().getAttribute("publisher");
  if (publisher == null) {
    try {
      String topicId = System.getenv("PUBSUB_TOPIC");
      publisher =
          Publisher.newBuilder(
                  ProjectTopicName.newBuilder()
                      .setProject(firestoreProjectId)
                      .setTopic(topicId)
                      .build())
              .build();
      event.getServletContext().setAttribute("publisher", publisher);
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}
 
Example #4
Source File: QuickstartSample.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
public static void main(String... args) throws Exception {
  // Instantiates a client
  Translate translate = TranslateOptions.getDefaultInstance().getService();

  // The text to translate
  String text = "Hello, world!";

  // Translates some text into Russian
  Translation translation =
      translate.translate(
          text, TranslateOption.sourceLanguage("en"), TranslateOption.targetLanguage("ru"));

  System.out.printf("Text: %s%n", text);
  System.out.printf("Translation: %s%n", translation.getTranslatedText());
}
 
Example #5
Source File: Translator.java    From dualsub with GNU General Public License v3.0 5 votes vote down vote up
private void updateInstance() {
	String key = preferences.get("googleTranslateKey", "");

	if (key != null && !key.equals("")) {
		translate = TranslateOptions.newBuilder().setApiKey(key).build()
				.getService();
	} else {
		// This should not happen (GUI forces the user to insert a key,
		// otherwise translation is not allowed)
		throw new RuntimeException(
				"Key for Google Translate service not defined");
	}
}
 
Example #6
Source File: Translate.java    From java-docs-samples with Apache License 2.0 2 votes vote down vote up
/**
 * Create Google Translate API Service.
 *
 * @return Google Translate Service
 */
public static com.google.cloud.translate.Translate createTranslateService() {
  return TranslateOptions.newBuilder().build().getService();
}