コード例 #1
0
 static void Sync(string culture, string input, string output)
 {
     SyncTranslator job = new SyncTranslator(cc, culture);
     Console.WriteLine("Adding files");
     Console.WriteLine("Input: " + input);
     Console.WriteLine("Output: " + output);
     job.OutputSaveBehavior = SaveBehavior.AlwaysOverwrite;
     ClientResult<TranslationItemInfo> cr = job.Translate(input, output);
     cc.ExecuteQuery();
     Console.WriteLine(DateTime.Now);
     Console.WriteLine("OutputSaveBehavior: {0}", job.OutputSaveBehavior.ToString());
     Console.WriteLine("Input: " + cr.Value.InputFile);
     Console.WriteLine("Output: " + cr.Value.OutputFile);
     Console.WriteLine("ErrorMessage: " + cr.Value.ErrorMessage);
     Console.WriteLine("TranslationId: " + cr.Value.TranslationId);
     Console.WriteLine("JobStatus....");
     Console.WriteLine("Succeeded: " + cr.Value.Succeeded);
     Console.WriteLine("Failed: " + cr.Value.Failed);
     Console.WriteLine("Canceled: " + cr.Value.Canceled);
     Console.WriteLine("InProgress: " + cr.Value.InProgress);
     Console.WriteLine("NotStarted: " + cr.Value.NotStarted);
     Console.WriteLine(DateTime.Now);
     Console.WriteLine("Done");
 }
コード例 #2
0
 static SyncTranslator createSyncTranslationJob(string jobCulture)
 {
     Console.WriteLine("Creating Sync job");
     SyncTranslator job = new SyncTranslator(sc, CultureInfo.GetCultureInfo(jobCulture));
     return job;
 }
        protected void Page_Load(object sender, EventArgs e)
        {
            // Read the data that has been posted
            connectedSiteUrl = Request.Form["connectedSiteUrl"];
            accessToken = Request.Form["accessToken"];
            refreshToken = Request.Form["refreshToken"];

            // Note that the following form fields were set dynamically by JavaScript
            // to represent the data from the document to be translated and the target language
            string documentContent = string.Empty;
            string targetLanguage = string.Empty;
            documentContent = Request.Form["documentContent"];
            targetLanguage = Request.Form["documentLanguage"];
            try
            {
                using (ClientContext context = TokenHelper.GetClientContextWithAccessToken(connectedSiteUrl, accessToken))
                {
                    // Use standard CSOM and OOXML approaches for creating a file
                    Web thisWeb = context.Web;
                    List docLib = thisWeb.Lists.GetByTitle("Documents");
                    Folder rootFolder = docLib.RootFolder;
                    context.Load(thisWeb);
                    context.Load(docLib);
                    context.Load(rootFolder);
                    context.ExecuteQuery();
                    FileStream fs = null;
                    try
                    {

                        // We'll build a Word Document by using OOXML
                        // Note that we'll first create it in a folder in this Web app.
                        using (WordprocessingDocument wordDocument =
                            WordprocessingDocument.Create(Server.MapPath("~/TempOOXML/SourceDocument.docx"),
                            WordprocessingDocumentType.Document))
                        {

                            // Add a main document part.
                            MainDocumentPart mainPart = wordDocument.AddMainDocumentPart();

                            // Create the document structure.
                            mainPart.Document = new Document();
                            Body body = mainPart.Document.AppendChild(new Body());

                            // Create a paragraph based on the text that was posted
                            // in Request.Form["documentContent"]
                            Paragraph para = body.AppendChild(new Paragraph());
                            Run run = para.AppendChild(new Run());
                            run.AppendChild(new Text(documentContent));

                        }

                        // At this stage, the local file has been created in the folder of this Web project
                        // so we'll now read it and create a new file in SharePoint, based on this local file.
                        byte[] documentBytes;
                        fs = System.IO.File.OpenRead(Server.MapPath("~/TempOOXML/SourceDocument.docx"));
                        documentBytes = new byte[fs.Length];
                        fs.Read(documentBytes, 0, Convert.ToInt32(fs.Length));

                        // At this stage, the file contents of the OOXML document has been read into the byte array
                        // so we can use that as the content of a new file in SharePoint.
                        Microsoft.SharePoint.Client.FileCreationInformation ooxmlFile
                            = new Microsoft.SharePoint.Client.FileCreationInformation();
                        ooxmlFile.Overwrite = true;
                        ooxmlFile.Url = thisWeb.Url
                            + rootFolder.ServerRelativeUrl
                            + "/SharePointSourceDocument.docx";
                        ooxmlFile.Content = documentBytes;
                        Microsoft.SharePoint.Client.File newFile = rootFolder.Files.Add(ooxmlFile);
                        context.Load(newFile);
                        context.ExecuteQuery();
                        success = true;
                    }
                    catch (Exception ex)
                    {
                        // Tell the user what went wrong. These variables will be used
                        // to report the error to the user in the TextTranslator.aspx page.
                        success = false;
                        exception = ex;

                    }
                    finally
                    {
                        // Clean up our filestream object
                        fs.Close();
                    }

                    // Do the actual translation work. Note that we use a synchronous translation
                    // approach here, but you could also use the TranslationJob object to
                    // perform an asynchronous translation.
                    if (success)
                    {
                        try
                        {
                            SyncTranslator job = new SyncTranslator(context, targetLanguage);
                            job.OutputSaveBehavior = SaveBehavior.AlwaysOverwrite;
                            job.Translate(
                                thisWeb.Url + rootFolder.ServerRelativeUrl + "/SharePointSourceDocument.docx",
                                thisWeb.Url + rootFolder.ServerRelativeUrl + "/" + targetLanguage + "_Document.docx");
                            context.ExecuteQuery();
                            targetLibrary = thisWeb.Url + rootFolder.ServerRelativeUrl;
                        }
                        catch (Exception ex)
                        {
                            // Tell the user what went wrong. These variables will be used
                            // to report the error to the user in the TextTranslator.aspx page.
                            success = false;
                            exception = ex;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                // Tell the user what went wrong. These variables will be used
                // to report the error to the user in the TextTranslator.aspx page.
                success = false;
                exception = ex;
            }
        }
コード例 #4
0
 private void sync_Click(object sender, RoutedEventArgs e)
 {
     CreateClientContext();
     SyncTranslator job = new SyncTranslator(cc, this.culture.Text);
     string input = this.inputFile.Text;
     string output = this.outputFile.Text;
     job.OutputSaveBehavior = SaveBehavior.AlwaysOverwrite;
     ClientResult<TranslationItemInfo> cr = job.Translate(input, output);
     ThreadPool.QueueUserWorkItem(new WaitCallback(CreateThreadForSync), cr);
 }