Click here to Skip to main content
15,886,857 members
Articles / Productivity Apps and Services / Sharepoint

Translating Text (NOT regular files & sites) using SharePoint 2013 Machine Translation Service Application

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
14 Mar 2014CPOL 7.1K  
How to translate text (NOT regular files and site) using SharePoint 2013 machine translation service application

Recently, I came across a very simple requirement from our customer to create a ‘Plain text translation utility Webpart’ on our SharePoint 2013 portal. Presently, we have components related to document translation, which is the sole purpose of SP 2013 Machine Translation Service (MTS) Application. By default, all object models functionality of MTS on SharePoint 2013 (CSOM, SOM, JSOM) have support to ‘Translate’ the documents. While searching on the internet, we will come across many articles or documentation explaining the document translation (one good article from MSDN is over here). But there is no straight forward API function to translate the TEXT data, which can provide functionality similar to Google or Bing Text Translation.

Of course, we can tweak the existing API functions to achieve this functionality. So the below mentioned code explains how we can achieve this:

C#
//Getting the Source text from input control.
string somestring = txtSource.Text;

//Retrieving the Service Context of the Site
SPServiceContext sc = SPServiceContext.GetContext(new SPSite("http://server/sites/MTS/"));

//Converting the input text to byte array
byte[] inputByte = Encoding.ASCII.GetBytes(somestring);

//Creating Synchronized translation job with target language as SPANISH
SyncTranslator job = new SyncTranslator(sc, CultureInfo.GetCultureInfo("es"));

byte[] outputByte = null; //byte array variable to get the output.

//Translate using the "Translate" function of Sync.Job with input text's byte array.
//Provided a dummy file extension type as "txt" - Here we are not specifying any output file path.
TranslationItemInfo itemInfo = job.Translate(inputByte, out outputByte, "txt");

//Converting the translated byte[] to text using UTF8 encoding.
string result = Encoding.UTF8.GetString(outputByte);

txtTarget.Text = result; // providing the result to the output text control.

So the trick in the above mentioned code is to use the overload for translating byte[] of the “Translate” function under Translation Sync.Job, then utilize the translated output byte[] to display the result after proper encoding. (UTF8 in this sample) to the text.

The utility webpart created using the above code will look like this:

Image 1

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
India India
I am Abin Jaik Antony, working as a Software Developer.My technology background area extends to MOSS 2007,Microsoft BI,.NET Framework etc.
Visit my blog for more details http://www.abinjaik.com

Comments and Discussions

 
-- There are no messages in this forum --