0% found this document useful (0 votes)
2 views1 page

API

The document contains a Java program that translates text from one language to another using a Google Apps Script URL. It takes user input for the text to be translated and specifies the source and target languages. The program constructs a URL to make an HTTP request to the translation service and prints the translated text.

Uploaded by

Quang Đặng
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views1 page

API

The document contains a Java program that translates text from one language to another using a Google Apps Script URL. It takes user input for the text to be translated and specifies the source and target languages. The program constructs a URL to make an HTTP request to the translation service and prints the translated text.

Uploaded by

Quang Đặng
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

package com.example.

api;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;

public class Main {


public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
String text = sc.nextLine();
//Translated text: Hallo Welt!
System.out.println("Translated text: " + translate("vi", "en", text));
}

private static String translate(String langFrom, String langTo, String text)


throws IOException {
// INSERT YOU URL HERE
String urlStr =
"https://script.google.com/macros/s/AKfycbwvGZFU2Vr75wlueNO9AqqsPnJSjFGSoYr--
arQHi7jCFjHUfcQ62_xEKJpeRhpEDU7/exec" +
"?q=" + URLEncoder.encode(text, StandardCharsets.UTF_8) +
"&target=" + langTo +
"&source=" + langFrom;
System.out.println(urlStr);
URL url = new URL(urlStr);
InputStream is = url.openStream();
StringBuilder respone = new StringBuilder();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String line;
while ((line = br.readLine()) != null) {
respone.append(line);
}
is.close();
return respone.toString();
}
}

You might also like