如何在我的 Java 应用程序中使用 Google Translate API? [英] How to use Google Translate API in my Java application?

查看:72
本文介绍了如何在我的 Java 应用程序中使用 Google Translate API?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我将字符串(英语或阿拉伯语)作为输入传递给

3) 使用此 java 代码测试您的 API:

import java.io.BufferedReader;导入 java.io.IOException;导入 java.io.InputStreamReader;导入 java.net.HttpURLConnection;导入 java.net.URL;导入 java.net.URLEncoder;公共类翻译器{public static void main(String[] args) 抛出 IOException {String text = "Hello world!";//翻译文本:你好世界!System.out.println("翻译文本:" + translate("en", "de", text));}private static String translate(String langFrom, String langTo, String text) 抛出 IOException {//在此处插入您的 URLString urlStr = "https://your.google.script.url" +"?q=" + URLEncoder.encode(text, "UTF-8") +"&target=" + langTo +"&source=" + langFrom;URL url = 新 URL(urlStr);StringBuilder response = new StringBuilder();HttpURLConnection con = (HttpURLConnection) url.openConnection();con.setRequestProperty("User-Agent", "Mozilla/5.0");BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));字符串输入行;while ((inputLine = in.readLine()) != null) {response.append(inputLine);}附寄();返回 response.toString();}}

因为它是免费的,所以有QUATA LIMITS:https://docs.google.com/macros/dashboard

If I pass a string (either in English or Arabic) as an input to the Google Translate API, it should translate it into the corresponding other language and give the translated string to me.

I read the same case in a forum but it was very hard to implement for me.
I need the translator without any buttons and if I give the input string it should automatically translate the value and give the output.

Can you help out?

解决方案

You can use google script which has FREE translate API. All you need is a common google account and do these THREE EASY STEPS.
1) Create new script with such code on google script:

var mock = {
  parameter:{
    q:'hello',
    source:'en',
    target:'fr'
  }
};


function doGet(e) {
  e = e || mock;

  var sourceText = ''
  if (e.parameter.q){
    sourceText = e.parameter.q;
  }

  var sourceLang = '';
  if (e.parameter.source){
    sourceLang = e.parameter.source;
  }

  var targetLang = 'en';
  if (e.parameter.target){
    targetLang = e.parameter.target;
  }

  var translatedText = LanguageApp.translate(sourceText, sourceLang, targetLang, {contentType: 'html'});

  return ContentService.createTextOutput(translatedText).setMimeType(ContentService.MimeType.JSON);
}

2) Click Publish -> Deploy as webapp -> Who has access to the app: Anyone even anonymous -> Deploy. And then copy your web app url, you will need it for calling translate API.

3) Use this java code for testing your API:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;

public class Translator {

    public static void main(String[] args) throws IOException {
        String text = "Hello world!";
        //Translated text: Hallo Welt!
        System.out.println("Translated text: " + translate("en", "de", text));
    }

    private static String translate(String langFrom, String langTo, String text) throws IOException {
        // INSERT YOU URL HERE
        String urlStr = "https://your.google.script.url" +
                "?q=" + URLEncoder.encode(text, "UTF-8") +
                "&target=" + langTo +
                "&source=" + langFrom;
        URL url = new URL(urlStr);
        StringBuilder response = new StringBuilder();
        HttpURLConnection con = (HttpURLConnection) url.openConnection();
        con.setRequestProperty("User-Agent", "Mozilla/5.0");
        BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
        String inputLine;
        while ((inputLine = in.readLine()) != null) {
            response.append(inputLine);
        }
        in.close();
        return response.toString();
    }

}

As it is free, there are QUATA LIMITS: https://docs.google.com/macros/dashboard

这篇关于如何在我的 Java 应用程序中使用 Google Translate API?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆