Click here to Skip to main content
12,751,306 members (36,318 online)
Click here to Skip to main content
Add your own
alternative version

Stats

4.1K views
Posted 13 Mar 2014

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

, 14 Mar 2014 CPOL
Rate this:
Please Sign up or sign in to vote.
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:

//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:

License

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

Share

About the Author

abin jaik
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

You may also be interested in...

Comments and Discussions

 
-- There are no messages in this forum --
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170215.1 | Last Updated 14 Mar 2014
Article Copyright 2014 by abin jaik
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid