Giter Club home page Giter Club logo

microsoft-translator-java's Introduction

Microsoft-Translator-Java

Microsoft Translator API Java Sample.

code

import java.net.URI;

import org.apache.commons.codec.net.URLCodec;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import com.google.gson.Gson;


public class Translate {

    public static void main(String[] args) {
        String subscriptionKey = args[0];
        String token = getToken(subscriptionKey);
        String text = "こんにちは世界";
        String fromLang = "ja";
        String toLang = "en-US";

        String translatedText = translate(token, fromLang, toLang, text);
        System.out.println(translatedText);
    }

    private static URLCodec CODEC = new URLCodec("UTF-8");

    public static String getToken(String subscriptionKey) {
        HttpClient httpclient = HttpClients.createDefault();

        try {
            URIBuilder builder = new URIBuilder("https://api.cognitive.microsoft.com/sts/v1.0/issueToken");
            URI uri = builder.build();
            HttpPost request = new HttpPost(uri);
            request.setHeader("Ocp-Apim-Subscription-Key", subscriptionKey);

            HttpResponse response = httpclient.execute(request);
            HttpEntity entity = response.getEntity();

            return EntityUtils.toString(entity);
        } catch (Exception e) {
            throw new IllegalStateException(e);
        }
    }

    public static String translate(String accessToken, String fromLang, String toLang, String text) {

        try {

            String env = CODEC.encode(text, "UTF-8");

            String translateUrl = "https://api.microsofttranslator.com/v2/Ajax.svc/Translate?text=%1s&from=%2s&to=%3s";
            String envUrl = String.format(translateUrl, env, fromLang, toLang);
            HttpGet request = new HttpGet(envUrl);

            String authorization = "Bearer " + accessToken;
            request.setHeader("Authorization", authorization);

            HttpClient httpclient = HttpClients.createDefault();
            HttpResponse response = httpclient.execute(request);
            HttpEntity entity = response.getEntity();

            String resultJson = EntityUtils.toString(entity);
            Gson gson = new Gson();
            
            return gson.fromJson(resultJson, String.class);
        } catch (Exception e) {
            throw new IllegalStateException(e);
        }
    }

}

resut

Hello to the world

microsoft-translator-java's People

Contributors

grachro avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.