SharePoint 2013 翻译功能
$tj = get-sptimerjob "Sharepoint Translation Services"
$tj.Runnow()
使用服务器对象模型的异步翻译
TranslationJob 类定义了一组要翻译的项目。它可以是单个文件或文件夹/文档库中的每个文件。以此方式提交的翻译作业将存储在翻译数据库中。每次运行翻译计时器作业时,它会从翻译数据库中提取一些作业并将其添加到要翻译的异步队列中。翻译计时器作业的默认间隔为 15 分钟。
以下代码演示如何异步翻译单个文件。
SPServiceContext sc = SPServiceContext.GetContext(new SPSite(site));
TranslationJob job = new TranslationJob(sc, CultureInfo.GetCultureInfo(culture));
job.AddFile(input, output);
job.Start();
SPServiceContext sc = SPServiceContext.GetContext(new SPSite(site));
TranslationJob job = new TranslationJob(sc, CultureInfo.GetCultureInfo(culture));
using (SPSite siteIn = new SPSite(inputList))
{
using (SPWeb webIn = siteIn.OpenWeb())
{
using (SPSite siteOut = new SPSite(outputList))
{
using (SPWeb webOut = siteOut.OpenWeb())
{
SPDocumentLibrary listIn = (SPDocumentLibrary)webIn.GetList(inputList);
SPDocumentLibrary listOut = (SPDocumentLibrary)webOut.GetList(outputList);
job.AddLibrary(listIn, listOut);
job.Start();
}
}
}
}
以下代码演示如何同步翻译单个文件。
SPServiceContext sc = SPServiceContext.GetContext(new SPSite(site));
SyncTranslator job = new SyncTranslator(sc, CultureInfo.GetCultureInfo(jobCulture));
TranslationItemInfo itemInfo = job.Translate(input, output);
异步翻译单个文件:
ClientContext clientContext = new ClientContext("http://serverName/sites/siteCollectionPath");
string culture = "cultureID";
string name = "translationJobName";
string inputFile = "http://serverName/path/inputFileName";
string outputFile = "http://serverName/path/outputFileName";
TranslationJob job = new TranslationJob(clientContext , culture);
job.AddFile(inputFile , outputFile);
job.Name = name;
job.Start();
clientContext.Load(job);
clientContext.ExecuteQuery();
//To retrieve the translation job ID.
string jobID = job.JobId;
异步翻译文件夹:
ClientContext clientContext = new ClientContext("http://serverName/sites/siteCollectionPath");
string culture = "cultureID";
string name = "translationJobName";
string inputFolder = clientContext.Web.GetFolderByServerRelativeUrl("inFolderPath");
string outputFolder = clientContext.Web.GetFolderByServerRelativeUrl("outFolderPath");
TranslationJob job = new TranslationJob(clientContext , culture);
job.AddFolder(inputFolder, outputFolder, true);
job.Name = name;
job.Start();
clientContext.Load(job);
clientContext.ExecuteQuery();
//To retrieve the translation job ID.
string jobID = job.JobId;
异步翻译库:
ClientContext clientContext = new ClientContext("http://serverName/sites/siteCollectionPath");
string culture = "cultureID";
string name = "translationJobName";
string inputLibrary = clientContext.Web.Lists.GetByTitle("inputLibraryName");
string outputLibrary = clientContext.Web.Lists.GetByTitle("outputLibraryName");
TranslationJob job = new TranslationJob(clientContext , culture);
job.AddFolder(inputLibrary , outputLibrary , true);
job.Name = name;
job.Start();
clientContext.Load(job);
clientContext.ExecuteQuery();
//To retrieve the translation job ID.
string jobID = job.JobId;
同步翻译单个文件:
ClientContext clientContext = new ClientContext("http://serverName/sites/siteCollectionPath");
string culture = "cultureID"
string inputFile = "http://serverName/path/inputFileName";
string outputFile = "http://serverName/path/outputFileName";
SyncTranslator job = new SyncTranslator(clientContext , culture);
job.OutputSaveBehavior = SaveBehavior.AlwaysOverwrite;
ClientResult<TranslationItemInfo> cr = job.Translate(inputFile, outputFile );
clientContext.ExecuteQuery();
//To retrieve additional information about the translation job.
string errorCode = clientContext.Value.ErrorCode;
string errorMessage = clientContext.Value.ErrorMessage;
string translateID = clientContext.Value.TranslationId;
string succeedResult = clientContext.Value.Succeeded;
string failResult = clientContext.Value.Failed;
string cancelStatus = clientContext.Value.Canceled;
string inProgressStatus = clientContext.Value.InProgress;
string notStartedStatus = clientContext.Value.NotStarted;
检索机器翻译服务支持的所有语言:
ClientContext clientContext = new ClientContext("http://serverName/sites/siteCollectionPath"); IEnumerable<string> supportedLanguages = TranslationJob.EnumerateSupportedLanguages(clientContext); clientContext.ExecuteQuery(); foreach (string item in supportedLanguages) { Console.Write(item + ", "); }
先列出来 API 接口。
异步翻译 REST API
以下是用于执行异步翻译的 REST API:
http://serverName/_api/TranslationJob('language')
异步翻译单个文件:
http://serverName/_api/TranslationJob('language')/TranslateFile(inputFile='/path/intput file', outputFile='/path/output file')
异步翻译文件夹:
http://serverName/_api/TranslationJob('language')/TranslateFolder(inputFolder='/path/in', outputFolder='/path/out')
异步翻译库:
http://serverName/_api/TranslationJob('language')/TranslateLibrary(inputLibrary='/LibraryName', outputLibrary='/LibraryName'')
同步翻译 REST API
机器翻译服务 REST 服务仅支持文件的同步翻译。以下是用于执行此操作的 API:
http://serverName/_api/SyncTranslator('language')/Translate(outputFile='/path/output file', inputFile='/path/input file')
附加的 机器翻译服务 REST API
机器翻译服务 REST 服务包括可用于检索有关机器翻译服务应用程序功能和翻译作业状态的信息的附加 API。
检索机器翻译服务支持的所有语言:
http://serverName/_api/TranslationJob.EnumerateSupportedLanguages
检查是否支持某种特定语言:
http://serverName/_api/TranslationJob.IsLanguageSupported('language')
检索机器翻译服务支持的所有文件扩展名:
http://serverName/_api/TranslationJob.EnumerateSupportedFileEXtensions
检查是否支持某个特定文件扩展名:
http://serverName/_api/TranslationJob.IsFileExtensionSupported('extension')
检查特定文件扩展名的文件大小限制:
http://serverName/_api/TranslationJob.GetMaximumFileSize('extension')
检索所有异步翻译作业的列表:
http://serverName/_api/TranslationJobStatus.GetAllJobs
检索所有活动异步翻译作业的列表:
http://serverName/_api/TranslationJobStatus.GetAllActiveJobs
检索特定异步翻译作业的文档信息:
http://serverName/_api/TranslationJobStatus('jobid')/GetAllItems
取消异步翻译作业:
http://serverName/_api/TranslationJob.CancelJob('jobid')