Machine Translation Services: Synchronous and Asynchronous Translation in SharePoint 2013

Introduction

This article explains the Machine Translation Service that is an important feature of SharePoint 2013. This is one of the new features introduced in SharePoint 2013. The translation is being done in the cloud services provided by Microsoft. The translation can be done in one of the following two ways:

  1. Synchronous: The file or content can be translated immediately once the request is received
  2. Asynchronous: It is based on a translation timer job and execution timings can be configured in Central Administration

To configure the service using Windows PowerShell:

$job = Get-SPTimerJob “SharePoint Translation Services”
$job.Runnow()

We will see how the translation can be done by the server and Client-Side Object Model (CSOM) as follows.

Asynchronous Translation using Server-side object model

In this method, the default time interval is 15 minutes. The Translation job class is used to translate the files in the Document Library asynchronously. The job details are stored in the Translation Database.

SPServiceContext sc = SPServiceContext.GetContext(new SPSite(site));
TranslationJob job = new TranslationJob(sc, CultureInfo.GetCultureInfo(culture));
using (SPSite site = new SPSite(input))
{
    using (SPWeb web = site.OpenWeb())
    {
        using (SPSite siteTo = new SPSite(output))
        {
            using (SPWeb webTo = siteTo.OpenWeb())          {
                SPDocumentLibrary list = (SPDocumentLibrary)web.GetList(input);
                SPDocumentLibrary listTo = (SPDocumentLibrary)webOut.GetList(output);
                job.AddLibrary(list, listTo);
                job.Start();
            }
        }
    }
}

Synchronous Translation using Server-side object model

In synchronous translation, the SyncTranslator class translates stream objects and files. Here the jobs are not stored in the database and are being added to the queue synchronously.

SPServiceContext sc = SPServiceContext.GetContext(new SPSite(site));
SyncTranslator job = new SyncTranslator(sc, CultureInfo.GetCultureInfo(jobCulture));
FileStream inputStream = new FileStream(input, FileMode.Open);
FileStream outputStream = new FileStream(output, FileMode.Create);
TranslationItemInfo itemInfo = job.Translate(inputStream, outputStream, fileFormat);
inputStream.Close();
outputStream.Flush();
outputStream.Close();

Asynchronous Translation using client-side object model: JavaScript CSOM

The following is the sample code snippet that does the job of translating a single file asynchronously.

var asyncJob;
var clientContext = new SP.ClientContext("serverRelativeUrl");
var contextSite = clientContext.get_site();
asyncJob = SP.Translation.TranslationJob.newObject(clientContext, "cultureID");
asyncJob.set_outputSaveBehavior(SP.Translation.SaveBehavior.alwaysOverwrite);
asyncJob.addFile("inputFilePath", "outputFilePath");
asyncJob.set_name("translationJobName");
asyncJob.start();
clientContext.load(asyncJob);
clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceededASync),Function.createDelegate(this, this.onQueryFailed));

Synchronous Translation using client-side object model: JavaScript CSOM

The following code translates the file synchronously.

var result;
var clientContext = new SP.ClientContext("serverRelativeUrl");
var contextSite = clientContext.get_site();
var job = SP.Translation.SyncTranslator.newObject(clientContext, "cultureID");
job.set_outputSaveBehavior(SP.Translation.SaveBehavior.alwaysOverwrite);
result = job.translate("inputFilePath", "outputFilePath");
clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceededSync),
Function.createDelegate(this, this.onQueryFailed));

Summary

Thus we have explored the various ways of translating the content in SharePoint 2013 with Machine Translation Services.

Up Next
    Ebook Download
    View all
    Learn
    View all