In this exercise you use a simple
console application to see the fundamental code pattern to
synchronously convert a Word document in a document library into French
using Machine Translation Services. For this Try It Out you need access
to a SharePoint 2013 on-premises server development environment with
Visual Studio 2012 installed and Machine Translation Services running
on the SharePoint server.
1. Either
confirm with your SharePoint Administrator that Machine Translation
Services is running in your on-premises environment, or open the
SharePoint Administration Console, click Application Management, click
Manage Service Applications and confirm that the Machine Translation
Service and Proxy are started. If they are not, return to Application
Management, click Manage Services on Server, locate the Machine
Translation Service and click the Start link.
2. Run Visual Studio 2012 as Administrator. Select New Project.
3. In the New Project dialog, expand the Templates ⇒ Visual C# ⇒ Windows nodes. Select Console Application and provide the name C14TranslateSPOM. Click OK.
4. When the project loads, close the Program.cs file.
5. In the
Solution Explorer, right-click the project and select Properties. On
the Application tab confirm the Target framework is set to .NET
Framework 4. If it’s not, set it and confirm any prompts and reopen the
Properties pane. Click the Build tab, set the Configuration drop-down
list to All Configurations, and close the Properties pane.
6. In the Solution Explorer, right-click the project and select Add Reference.
7. In the Reference Manager, under Framework, add System.Web, and under Extensions add Microsoft.SharePoint. Click Browse and navigate to the following location: c:\Windows\Microsoft.NET\assembly\GAC_MSIL\Microsoft.Office.TranslationServices\v4.0_15.0.0.0__71e9bce111e9429c. Click the Microsoft.Office.TranslationServices.dll, click Add and click OK.
8. In the Solution Explorer, double-click the
Program.cs file to open it. Add the following
using statements:
using System.Globalization;
using System.Web;
using System.IO;
using Microsoft.SharePoint;
using Microsoft.Office.TranslationServices;
9. In the
Main method, add the following:
Console.WriteLine("C14TranslateSPOM Started");
string myWebSite = "http://YourServerNameHere";
string myCulture = "fr";
string myInput =
"http://YourServerNameHere/YourDocumentLibrary/YourDocumentName.docx";
string myOutput =
"http://YourServerNameHere/YourDocumentLibrary/YourDocumentName-fr.docx";
serviceContext = SPServiceContext.GetContext(new SPSite(myWebSite));
// Run synchronous conversion on a single file in doc lib.
Console.WriteLine("Synchronous Translation Process Starting");
SyncTranslator job = new SyncTranslator(serviceContext,
CultureInfo.GetCultureInfo(myCulture));
Console.WriteLine("File names for processing");
Console.WriteLine("File input: " + myInput);
Console.WriteLine("File to be output: " + myOutput);
TranslationItemInfo itemInfo = job.Translate(myInput, myOutput);
Console.WriteLine("Translation Language: {0}",
job.TargetLanguage.Name);
Console.WriteLine("SaveBehaviorForOutput: {0}",
job.OutputSaveBehavior.ToString());
displayTranslationItemInfo(itemInfo);
Console.ReadLine();
}
static void displayTranslationItemInfo(TranslationItemInfo itemInfo)
{
Console.WriteLine("\nTranslation completed -- Resulting information:");
Console.WriteLine("File Input: " + itemInfo.InputFile);
Console.WriteLine("File Output: " + itemInfo.OutputFile);
Console.WriteLine("Job Start Time: " + itemInfo.StartTime);
Console.WriteLine("Job Complete Time: " + itemInfo.CompleteTime);
Console.WriteLine("Error Message: " + itemInfo.ErrorMessage);
Console.WriteLine("Translation Id: " + itemInfo.TranslationId);
Console.WriteLine("\nFinal Job Status");
Console.WriteLine("Succeeded: " + itemInfo.Succeeded);
Console.WriteLine("Failed: " + itemInfo.Failed);
Console.WriteLine("Canceled: " + itemInfo.Canceled);
Console.WriteLine("In Progress: " + itemInfo.InProgress);
Console.WriteLine("Not Started: " + itemInfo.NotStarted);
Console.WriteLine("\nTranslation completed. Press <Enter> to quit.");
}
10. Open a browser, navigate to a site, and upload a .docx document to be translated.
11. After the
file is uploaded, click the ellipsis (...) by the filename and copy the
URL for the document. Use it to do all of the following tasks:
- Replace the YourServerNameHere literal in the myWebSite variable with the root URL for your SharePoint site.
- Replace the YourServerNameHere/YourDocumentLibrary/YourDocumentName literal values in the myInput variable, with the full path to your document.
- Replace the YourServerNameHere/YourDocumentLibrary/YourDocumentName literal values in the myOutput variable with the full path to your document. Be sure to leave the -fr
on the filename or some other arbitrary value so the output file does
not overwrite the input file when the translation is completed.
12. Press F5
to run the code. Depending on the hardware speed of your test server,
this might take several minutes to complete. The command window will
show the resulting processing information when the translation is
completed. Figure 1 provides an example of the output results in the command window.
13. When the operation completes, close the command window and look in the document library to review the new translated document.
For this Try It Out you performed a synchronous translation on a document in a document library, and to do this you used the SyncTranslator class. You created an object and passed in the full URL input/output path to a document. It is possible to use SyncTranslator
to input/output file stream objects and byte arrays, yet in all cases,
synchronous interactions with the Machine Translation Service is for a
single file translation only. With asynchronous interactions with the
service, where you submit timer jobs into the queue, you have more
latitude and for this you use the TranslationJob
class. Using this class you can not only designate a single file on a
document library to be translated asynchronously, you can designate SPFolder
objects for both input and output to translate the contents of an
entire folder, or you can translate an entire document library by
providing an SPDocumentLibrary object for both an input and output location.