Code Examples

Translation API Code Examples

The examples below should be an indicative guide on how to use our Translation API

HTML

Machine Translation



<form name="frmAPI" method="post" action="http://[api | sandbox].strakertranslations.com/api/v1/job/machine/index.cfm">
<table>
<tr valign="top">
<td>Account ID</td>
<td><input type="text" name="account_id" size="60" value=""></td>
</tr>
<tr valign="top">
<td>Source Language Code</td>
<td><input type="text" name="sl" size="20" value="en"></td>
</tr>
<tr valign="top">
<td>Target Language Code</td>
<td><input type="text" name="tl" size="20" value="es"></td>
</tr>
<tr valign="top">
<td>Source Text</td>
<td><textarea name="payload" rows="5" cols="60">some test content</textarea></td>
</tr>
<tr valign="top">
<td>Return Format</td>
<td>
<select name="format">
<option value="xml" selected="true" >XML</option>
<option value="json" >JSON</option>
</select>
</td>
</tr>
<tr>
<td> </td>
<td><input type="submit" value="Send for Translation" name="btnSubmit"></td>
</tr>
</table>
</form>


Create Job



<form name="frmAPI" method="post" action="http://[api | sandbox].strakertranslations.com/api/v1/job/create/index.cfm">
<table>
<tr valign="top">
<td>Account ID</td>
<td><input type="text" name="account_id" size="60" value=""></td>
</tr>
<tr valign="top">
<td>Title</td>
<td><input type="text" name="title" size="20" value="English"></td>
</tr>
<tr valign="top">
<td>Source Language Code</td>
<td><input type="text" name="sl" size="20" value="English"></td>
</tr>
<tr valign="top">
<td>Target Language Code</td>
<td><input type="text" name="tl" size="20" value="French,Spanish"></td>
</tr>
<tr valign="top">
<td>Source Text</td>
<td><textarea name="payload" rows="5" cols="60">some test content</textarea></td>
</tr>
<tr valign="top">
<td>Token</td>
<td><input type="text" name="token" size="20" value=""></td>
</tr>
<tr valign="top">
<td>Callback URI</td>
<td><input type="text" name="callback_uri" size="20" value=""></td>
</tr>
<tr valign="top">
<td>Leadtime</td>
<td><input type="text" name="leadtime" size="20" value=""></td>
</tr>
<tr valign="top">
<td>Plan</td>
<td>
<select name="plan">
<option value="base" selected="true" >base</option>
<option value="professional" >professional</option>
<option value="premium" >premium</option>
</select>
</td>
</tr>
<tr valign="top">
<td>Return Format</td>
<td>
<select name="format">
<option value="xml" selected="true" >XML</option>
<option value="json" >JSON</option>
</select>
</td>
</tr>
<tr>
<td> </td>
<td><input type="submit" value="Send for Translation" name="btnSubmit"></td>
</tr>
</table>
</form>


Get Job



<form name="frmAPI" method="get" action="http://[api | sandbox].strakertranslations.com/api/v1/job/get/index.cfm">
<table>
<tr valign="top">
<td>Account ID</td>
<td><input type="text" name="account_id" size="60" value=""></td>
</tr>
<tr valign="top">
<td>Job Key</td>
<td><input type="text" name="job_key" size="60" value=""></td>
</tr>
<tr valign="top">
<td>Return Format</td>
<td>
<select name="format">
<option value="xml" selected="true" >XML</option>
<option value="json" >JSON</option>
</select>
</td>
</tr>
<tr>
<td> </td>
<td><input type="submit" value="Get Translation" name="btnSubmit"></td>
</tr>
</table>
</form>

JAVA

Machine Translation



import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.Writer;
import java.net.HttpURLConnection;
import java.net.ProtocolException;
import java.net.URL;
import java.net.URLConnection;
//Create Post String
String data = URLEncoder.encode("account_id", "UTF-8") + "=" + URLEncoder.encode("", "UTF-8");
data += "&" + URLEncoder.encode("payload", "UTF-8") + "=" + URLEncoder.encode("Hi, how are you", "UTF-8");
data += "&" + URLEncoder.encode("sl", "UTF-8") + "=" + URLEncoder.encode("en", "UTF-8");
data += "&" + URLEncoder.encode("tl", "UTF-8") + "=" + URLEncoder.encode("es", "UTF-8");
// Send Data To Page
URL url = new URL("http://[api | sandbox].strakertranslations.com/api/v1/job/machine/index.cfm");
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write(data);
wr.flush();
// Get The Response
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while ((line = rd.readLine()) != null) {
System.out.println(line);
//you Can Break The String Down Here
}


Create Job



import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.Writer;
import java.net.HttpURLConnection;
import java.net.ProtocolException;
import java.net.URL;
import java.net.URLConnection;
//Create Post String
String data = URLEncoder.encode("account_id", "UTF-8") + "=" + URLEncoder.encode("", "UTF-8");
data += "&" + URLEncoder.encode("payload", "UTF-8") + "=" + URLEncoder.encode("Hi, how are you", "UTF-8");
data += "&" + URLEncoder.encode("sl", "UTF-8") + "=" + URLEncoder.encode("English", "UTF-8");
data += "&" + URLEncoder.encode("tl", "UTF-8") + "=" + URLEncoder.encode("Spanish", "UTF-8");
data += "&" + URLEncoder.encode("priority", "UTF-8") + "=" + URLEncoder.encode("5", "UTF-8");
data += "&" + URLEncoder.encode("callback_uri", "UTF-8") + "=" + URLEncoder.encode("", "UTF-8");
//data += "&" + URLEncoder.encode("token", "UTF-8") + "=" + URLEncoder.encode("you can pass in a token to identify this translation in your system, for example it may be the ID of the content object in your CMS or application", "UTF-8");
// Send Data To Page
URL url = new URL("http://[api | sandbox].strakertranslations.com/api/v1/job/create/index.cfm");
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write(data);
wr.flush();
// Get The Response
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while ((line = rd.readLine()) != null) {
System.out.println(line);
//you Can Break The String Down Here
}


Get Job



import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.Writer;
import java.net.HttpURLConnection;
import java.net.ProtocolException;
import java.net.URL;
import java.net.URLConnection;
//@param endpoint - "http://[api | sandbox].strakertranslations.com/api/v1/job/get/index.cfm"
//@param requestParameters - "account_id=***your account id goes here***&job_key=***your job key goes here***"
public static String sendGetRequest(String endpoint, String requestParameters)
{
String result = null;
if (endpoint.startsWith("http://"))
{
// Send a GET request to the servlet
try
{
// Construct data
StringBuffer data = new StringBuffer();
// Send data
String urlStr = endpoint;
if (requestParameters != null && requestParameters.length () > 0)
{
urlStr += "?" + requestParameters;
}
URL url = new URL(urlStr);
URLConnection conn = url.openConnection ();
// Get the response
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
StringBuffer sb = new StringBuffer();
String line;
while ((line = rd.readLine()) != null)
{
sb.append(line);
}
rd.close();
result = sb.toString();
} catch (Exception e)
{
e.printStackTrace();
}
}
return result;
}


Approve Job



import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.Writer;
import java.net.HttpURLConnection;
import java.net.ProtocolException;
import java.net.URL;
import java.net.URLConnection;
//Create Post String
String data = URLEncoder.encode("account_id", "UTF-8") + "=" + URLEncoder.encode("", "UTF-8");
data += "&" + URLEncoder.encode("job_key", "UTF-8") + "=" + URLEncoder.encode("", "UTF-8");
// Send Data To Page
URL url = new URL("http://[api | sandbox].strakertranslations.com/api/v1/job/approve/index.cfm");
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write(data);
wr.flush();
// Get The Response
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while ((line = rd.readLine()) != null) {
System.out.println(line);
//you Can Break The String Down Here
}

PHP

Machine Translation

<?php
// pass in source text, source language, target language and your apikey and the
//system will return a machine (automatic) translation of your source text into the target language.
$uri = "http://[api | sandbox].strakertranslations.com/api/v1/job/machine/index.cfm";
$account_id = ""; // change this to your API key
$payload = "Hi, how are you"; // this is the source text
$sl = "en"; // this is the source language code for English
$tl = "es"; // this is the target language code for Spanish
header('Content-type: text/html; charset=UTF-8');
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_URL, $uri);
$data = array(
'account_id' => $apiKey,
'sl' => $sl,
'tl' => $tl,
'payload' => $st,
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$result = curl_exec($ch);
var_dump(json_decode($result));
?>


Create Job



<?php
// this call will return a json struct contain a key "id" and "translation_id".
//To get content back you will need to use a "GET" request passing in the "id".
//To get the status of the tranlation job you will need to pass in "translation_id"
$uri = "http://[api | sandbox].strakertranslations.com/api/v1/job/create/index.cfm";
$account_id = ""; // change this to your API key
$payload = "Hi, how are you"; // this is the source text
$sl = "en"; // this is the source language code for English
$tl = "es"; // this is the target language code for Spanish
// $token // you can pass in a token to identify this translation in your system, for example it may be the ID of the content object in your CMS or application
$title = "About Us"; // A friendly name that for the translation job. Specify this to easily identify jobs later
$format = "json" ; // can be json|xml|query
$priority = "5" ; // can be json|xml|query
header('Content-type: text/html; charset=UTF-8');
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_URL, $uri);
$data = array(
'account_id' => $account_id,
'sl' => $sl,
'tl' => $tl,
'priority' => $priority,
// 'token' => $token,
'payload' => $payload,
'title' => $title,
'format' => $format,
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$result = curl_exec($ch);
var_dump(json_decode($result));
?>


Get Job



<?php
// when you submitted your human translation the API returned a "content id". Pass the content id
// in to the translate function using a "GET" rather than "POST" method and if the content
// has been translated it will be returned
$uri = "http://[api | sandbox].strakertranslations.com/api/v1/job/get/?account_id=**account_id**&job_key=***your content id goes here*** "; //replace "***your content id goes here***" with your content id
header('Content-type: text/html; charset=UTF-8');
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, $uri);
$result = curl_exec($ch);
var_dump(json_decode($result));
?>


Approve Job



<?php
// pass in source text, source language, target language and your apikey and the
//system will return a machine (automatic) translation of your source text into the target language.
$uri = "ttp://[api | sandbox].strakertranslations.com/api/v1/job/approve/index.cfm";
$account_id = ""; // change this to your API key
$job_key = ""; // this is the job id
header('Content-type: text/html; charset=UTF-8');
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_URL, $uri);
$data = array(
'account_id' => $apiKey,
'job_key' => $st,
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$result = curl_exec($ch);
var_dump(json_decode($result));
?>

ColdFusion

Machine Translation



<cfset stArgs= structNew()>
<cfset stArgs["uri"]= "http://[api | sandbox].strakertranslations.com/api/v1/job/machine/index.cfm">
<cfset stArgs["account_id"]= "">
<cfset stArgs["sl"]= "en">
<cfset stArgs["tl"]= "es">
<cfset stArgs["payload"]= "Some text that needs to be translated">
<!--- Make the API call --->
<cfhttp url="#stArgs.uri#" method="POST">
<cfhttpparam name="api_key" type="formField" value="#stArgs.account_id#">
<cfhttpparam name="sl" type="formField" value="#stArgs.sl#">
<cfhttpparam name="tl" type="formField" value="#stArgs.tl#">
<cfhttpparam name="payload" type="formField" value="#stArgs.payload#">
</cfhttp>
<!--- Get hold of the response ---> <cfset sResponse = cfhttp.filecontent>
<!--- Convert the json based response into a structure --->
<cfset stResponse = deserializeJSON(sResponse)>
<cfoutput> <table> <tr> <td>Source Language: </td> <td>#stResponse.data.sl[1]#</td> </tr> <tr> <td>Target Language: </td> <td>#stResponse.data.tl[1]#</td> </tr> <tr> <td>Source Text:</td> <td>#stResponse.data.st[1]#</td> </tr> <tr> <td>Translated Text:</td> <td>#stResponse.data.tt[1]#</td> </tr> </table>
</cfoutput>


Create Job



<cfset stArgs= structNew()>
<cfset stArgs["uri"]= "http://[api | sandbox].strakertranslations.com/api/V1/job/create/index.cfm">
<cfset stArgs["account_id"]= "">
<cfset stArgs["sl"]= "en">
<cfset stArgs["tl"]= "es">
<cfset stArgs["payload"]= "Some text that needs to be translated">
<cfset stArgs["title"]= ""><--- A friendly name that for the translation job. Specify this to easily identify jobs later --->
<cfset stArgs["priority"]= "3"><--- value between 1 and 5, 1 being urgent and 5 standard --->
<!--- Make the API call --->
<cfhttp url="#stArgs.uri#" method="POST">
<cfhttpparam name="account_id" type="formField" value="#stArgs.account_id#">
<cfhttpparam name="sl" type="formField" value="#stArgs.sl#">
<cfhttpparam name="tl" type="formField" value="#stArgs.tl#">
<cfhttpparam name="payload" type="formField" value="#stArgs.payload#">
<cfhttpparam name="title" type="formField" value="#stArgs.title#">
<cfhttpparam name="priority" type="formField" value="#stArgs.priority#">
</cfhttp>
<!--- Get hold of the response ---> <cfset sResponse = cfhttp.filecontent>
<!--- Convert the json based response into a structure --->
<cfset stResponse = deserializeJSON(sResponse)>
<cfoutput>
<table>
<tr>
<td>Source Language: </td>
<td>#stResponse.data.sl[1]#</td>
</tr>
<tr>
<td>Target Language: </td>
<td>#stResponse.data.tl[1]#</td>
</tr>
<tr>
<td>Source Text:</td>
<td>#stResponse.data.content[1]#</td>
</tr>
<tr>
<td>Translation Job ID:</td>
<td>#stResponse.data.translation_id[1]#</td>
</tr>
<tr>
<td>Content ID:</td>
<td>#stResponse.data.id[1]#</td>
</tr>
<tr>
<td>Token:</td>
<td>#stResponse.data.token[1]#</td>
</tr>
<tr>
<td>Callback URI:</td>
<td>#stResponse.data.callback_uri[1]#</td>
</tr>
</table>
</cfoutput>


Get Job



<!--- when you submitted your human translation the API returned a "content id". Pass the content id
in to the translate function using a "GET" rather than "POST" method and if the content
has been translated it will be returned --->
<cfset stArgs= structNew()>
<cfset stArgs["uri"]= "http://[api | sandbox].strakertranslations.com/api/v1/job/get/index.cfm">
<cfset stArgs["account_id"]= "">
<cfset stArgs["job_key"]= "">
<!--- Make the API call --->
<cfhttp url="#stArgs.uri#" method="GET">
<cfhttpparam name="account_id" type="URL" value="#stArgs.account_id#">
<!--- put the content id returned from the add translation call in here --->
<cfhttpparam name="job_key" type="URL" value="your content id">
</cfhttp>
<!--- Get hold of the response ---> <cfset sResponse = cfhttp.filecontent>
<!--- Convert the json based response into a structure --->
<cfset stResponse = deserializeJSON(sResponse)>
<cfdump var="#sResponse#">


Approve Job