Code Monkey home page Code Monkey logo

translation-kit's Introduction

Translation Kit

Translation kit is an asynchronous abstraction on top of common translation services. It offers built-in support for Yandex.Translate (v1.5) and Google Translate (v2). Its primary aim is to offer a modern Java interface while allowing easy switching between services during program execution.

You can find the Javadocs here.

API Keys

Before using the kit, you should acquire a keys for the services you plan to use. Set the key values as environment variables with the following names:

  • Google Translate
    GOOGLE_API_KEY
  • Yandex.Translate
    YANDEX_API_KEY

On certain operating systems, you may need to start your IDE through a terminal in order for the values to be recognized.

Using Translators

Translators perform operations asynchronously and are easily used with lambdas. You can perform simple operations, ignoring possible errors. Although this is isn't suggested, here is an example:

Translator translator = Translators.get(Key.YANDEX);

translator.translate(new Text("test"), Language.NL, possibleResults -> {
    if (possibleResults != null)
        System.out.println(possibleResults[0]);
});

Error handling relies on an additional consumer that handles a TranslationError. You can retrieve the service's response code from such errors and act accordingly.

Translator translator = Translators.get(Key.YANDEX);

translator.translate(new Text("test"), Language.NL,
    possibleResults -> {
        if (possibleResults != null)
            System.out.println(possibleResults[0]);
    },
    error -> {
        if (error.getCode() == 401)
            System.out.println("The API key is invalid.");
    }
);

The Translator class defines operations for:

  • translating text and retrieving possible translations
  • detecting possible languages a text belongs to
  • determining if translation is supported from one language to another

translation-kit's People

Contributors

mlposey avatar

Watchers

James Cloos 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.