none
Translate the content of a page to other language using Microsoft APIs RRS feed

  • Question

  • Hi

    We have a requirement to translate the content of a page or rather full page to other language for instance from English to Spanish,

    I tried to use the variations available OOTB to translate but it doesn't work and only the OOTB options are translated but not the content on the page.

    Is there anyway to accomplish the same using Microsoft APIs?

    Thanks

    Paru


    Paru

    Tuesday, November 28, 2017 7:51 AM

Answers

  • Hi,

    I could reproduce this in my online environment, below code works before and works in local SharePoint 2013 while stop working in SharePoint online now.

     <script type="text/javascript" src="https://code.jquery.com/jquery-1.11.3.js" ></script>
        <script type="text/javascript">
            $(function () {
                $.ajax({
                    url: _spPageContextInfo.webAbsoluteUrl +
                             "/_api/TranslationJob('zh-cn')/TranslateFile(inputFile='/sites/Developer/SitePages/wikipage1.aspx', outputFile='/sites/Developer/SitePages/wikipage2.aspx')",
                    type: 'POST',
                    //data: arrayBuffer,
                    processData: false,
                    headers: {
                        "Accept": "application/json; odata=verbose",
                        //"content-type": "application/json; odata=verbose",
                        "X-RequestDigest": $('#__REQUESTDIGEST').val(),
                    },
                    success: successHandler,
                    error: errorHandler
                })
            })
            function successHandler(data, textStatus, jqXHR) {
                alert("successHandler " + "textstatus:" + textStatus + "data: " + data);
            }
    
            function errorHandler(xhr, ajaxOptions, thrownError) {
                alert('Request failed: ' + xhr.status + '\n' + thrownError + '\n' + xhr.responseText);
            }
        </script>

    I would suggest you submit a service request from admin center for this case.

     

    Best Regards,

    Lee


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Thursday, December 7, 2017 5:16 AM

All replies

  • Hi,

    You could use Machine Translation Service to translate a page.

    Sample code:

     using (var clientContext = new ClientContext("http://sp:12001/"))
                {
                    
                    IEnumerable<string> supportedLanguages = TranslationJob.EnumerateSupportedLanguages(clientContext);
                    clientContext.ExecuteQuery();
                    foreach (string item in supportedLanguages)
                    {
                        Console.Write(item + ", ");
                    }
                    
                    string culture = "zh-cn";
                    string name = "translationJobName";
                    string inputFile = "http://sp:12001/SitePages/wikic1.aspx";
                    string outputFile = "http://sp:12001/SitePages/wikic2.aspx";           
                    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.ToString();
    
                }

    Here are some links for your reference.

    https://docs.microsoft.com/en-us/sharepoint/dev/general-development/machine-translation-services-in-sharepoint

    https://code.msdn.microsoft.com/SharePoint-2013-Perform-a-8e53b06a

    Best Regards,

    Lee


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Wednesday, November 29, 2017 2:24 AM
  • Hi Lee

    I just tried this but it didn't work, the articles says it works only for on-premises? I want to try it on SharePoint Online, is there any other way to do it?

    Thanks

    Paru


    Paru

    Wednesday, November 29, 2017 11:25 AM
  • My code is as below
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using Microsoft.SharePoint.Client;
    using Microsoft.SharePoint;
    using Microsoft.Office.Client.TranslationServices;
    using System.Security;
    
    namespace testTranslate
    {
        class Program
        {
            static void Main(string[] args)
    
            {
                ClientContext clientContext = new ClientContext("https://xyz.sharepoint.com/sites/abc/");
                string username = "abc@xyz.onmicrosoft.com", pssword = "";
                var securePassword = new SecureString();
                foreach (char c in pssword)
                {
                    securePassword.AppendChar(c);
                }
                var onlineCredentials = new SharePointOnlineCredentials(username, securePassword);
                clientContext.Credentials = onlineCredentials;
                string jobID;
                string culture = "en";
                string name = "translationJob1";
                string inputFile = "https://xyz.sharepoint.com/sites/abc/SitePages/testTranslate.aspx";
                string outputFile = "https://xyz.sharepoint.com/sites/abc/SitePages/testTranslateEnglish.aspx";
                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.
                jobID = job.JobId.ToString();
    
                //IEnumerable<string> supportedLanguages = TranslationJob.EnumerateSupportedLanguages(clientContext);
                //clientContext.ExecuteQuery();
                //foreach (string item in supportedLanguages)
                //{
                //    Console.Write(item + ", ");
                //}
                Console.WriteLine("Script completed,press any key to exit");
                Console.ReadKey();
    
            }
        }
    }
    


    Paru

    Wednesday, November 29, 2017 11:52 AM
  • Hi,

    I could reproduce this in my online environment, below code works before and works in local SharePoint 2013 while stop working in SharePoint online now.

     <script type="text/javascript" src="https://code.jquery.com/jquery-1.11.3.js" ></script>
        <script type="text/javascript">
            $(function () {
                $.ajax({
                    url: _spPageContextInfo.webAbsoluteUrl +
                             "/_api/TranslationJob('zh-cn')/TranslateFile(inputFile='/sites/Developer/SitePages/wikipage1.aspx', outputFile='/sites/Developer/SitePages/wikipage2.aspx')",
                    type: 'POST',
                    //data: arrayBuffer,
                    processData: false,
                    headers: {
                        "Accept": "application/json; odata=verbose",
                        //"content-type": "application/json; odata=verbose",
                        "X-RequestDigest": $('#__REQUESTDIGEST').val(),
                    },
                    success: successHandler,
                    error: errorHandler
                })
            })
            function successHandler(data, textStatus, jqXHR) {
                alert("successHandler " + "textstatus:" + textStatus + "data: " + data);
            }
    
            function errorHandler(xhr, ajaxOptions, thrownError) {
                alert('Request failed: ' + xhr.status + '\n' + thrownError + '\n' + xhr.responseText);
            }
        </script>

    I would suggest you submit a service request from admin center for this case.

     

    Best Regards,

    Lee


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Thursday, December 7, 2017 5:16 AM
  • Thanks Lee

    I will do this now


    Paru

    Wednesday, December 13, 2017 12:46 PM