Java

This guide provides a step by step tutorial on how to integrate the Translation Exchange SDK with your Java based web applications and allow you to translate your site into multiple languages.

Before Getting Started

Make sure you have a Translation Exchange account, and have created a project in your dashboard.

1. Install the SDK

To download and install the SDK you currently have the following options.

Plain-old JAR

Download the following JARs and put them on your test classpath:

Maven

Add dependencies to com.translationexchange:tml, com.translationexchange:tml-memcached in the preferred scope. (Note: 0.2.10 is the latest stable version for tml.)

<dependency>  
  <groupId>com.translationexchange</groupId>
  <artifactId>tml</artifactId>
  <version>0.2.10</version>
</dependency>  
<dependency>  
  <groupId>com.translationexchange</groupId>
  <artifactId>tml-memcached</artifactId>
  <version>0.2.3</version>
</dependency>  

2. Configure the SDK

For the best performance, translations should be cached in your application. Multiple cache adapters are supported.

Configure the SDK in your application using Tml.getConfig() getters/setters:

import com.translationexchange.core.Tml;  
import com.translationexchange.core.Utils;

Tml.getConfig().setApplication(Utils.buildMap(  
    "key", "e9ecde2f25c1fed5c555e4bb036210fcde472536f5d71cc8b85f3508de9c845c",    
    "host", "https://sandbox-api.translationexchange.com",
   // "cdn_host", "https://trex-snapshots-dev.s3-us-west-1.amazonaws.com"
));

Tml.getConfig().setAgent(Utils.buildMap(  
    "host", "https://tools.translationexchange.com/agent/sandbox/agent.min.js"
));

Tml.getConfig().setCache(Utils.buildMap(  
    "enabled",  true,  
    "class",    "com.translationexchange.cache.Memcached",
    "host",     "localhost:11211",
    "namespace", "e9ecde2f25c1",
    //"version_check_interval", 10
));

To learn more about configuring the SDK, go to the Java Configuration section of the docs.

3. Translate your content

Tml.init(YOUR_APPLICATION_KEY, YOUR_APPLICATION_TOKEN);  
Tml.initLanguage("en");  
Tml.initSource("index");

Tml.translate("Hello World");  
// Hello World

Tml.beginBlockWithOptions(Utils.buildMap("source", "sample")); // virtual sources

Tml.translate("Hello World", "Greeting");

Tml.endBlock();

Tml.translate("You have {count || message}", Utils.buildMap("count", 5));  
// You have 5 messages

Tml.translate("{user} sent {target} [bold: {count || gift}]", Utils.buildMap(  
      "user": currentUser,
      "target", users.get("anna"),
      "count", 1))
// Michael sent Anna 1 gift

Tml.translate("[indent: You] have [indent: {count||message}]", Utils.buildMap(  
       "count", 5,
       "indent", "<strong>{$0}</strong>"));
// <strong>You</strong> have <strong>5 messages</strong>