static void TranslateDocument(Configuration conf)
        {
            // request parameters for translation
            string     name      = "test.docx";
            string     folder    = "";
            string     pair      = "en-fr";
            string     format    = "docx";
            string     outformat = "";
            string     storage   = "First Storage";
            string     saveFile  = "translated_d.docx";
            string     savePath  = "";
            bool       masters   = false;
            List <int> elements  = new List <int>();

            // local paths to upload and download files
            string uploadPath   = Directory.GetParent(Directory.GetCurrentDirectory()).Parent.Parent.Parent.FullName + "/" + name;
            string downloadPath = Directory.GetParent(Directory.GetCurrentDirectory()).Parent.Parent.Parent.FullName + "/" + saveFile;

            TranslationApi api     = new TranslationApi(conf);
            FileApi        fileApi = new FileApi(conf);


            Stream stream = File.Open(uploadPath, FileMode.Open);

            UploadFileRequest uploadRequest = new UploadFileRequest {
                File = stream, path = name, storageName = storage
            };
            FilesUploadResult uploadResult = fileApi.UploadFile(uploadRequest);

            Console.WriteLine("Files uploaded: " + uploadResult.Uploaded.Count);

            TranslateDocumentRequest request  = api.CreateDocumentRequest(name, folder, pair, format, outformat, storage, saveFile, savePath, masters, elements);
            TranslationResponse      response = api.RunTranslationTask(request);

            Console.WriteLine(response.Message);
            foreach (var key in response.Details.Keys)
            {
                Console.WriteLine(key + ": " + response.Details[key]);
            }

            DownloadFileRequest downloadRequest = new DownloadFileRequest {
                storageName = storage, path = saveFile
            };
            Stream result = fileApi.DownloadFile(downloadRequest);

            Console.WriteLine("Translated file downloaded");

            using (FileStream file = new FileStream(downloadPath, FileMode.Create, FileAccess.Write))
            {
                result.CopyTo(file);
            }
            Console.WriteLine("Translated file saved");
        }
Ejemplo n.º 2
0
        private Response Translate(Workbook sourceWorkbook, string sessionId, string filePath, string outputType, string translateFrom, string translateTo)
        {
            var conf = new Configuration
            {
                ClientId     = "ClientId",
                ClientSecret = "ClientSecret"
            };

            var name = sessionId + "_" + Path.GetFileNameWithoutExtension(filePath) + ".xlsx";

            const string folder = "";

            var pair = $"{translateFrom}-{translateTo}";

            const string format = "xlsx";

            const string outFormat = "xlsx";

            const string storage = "";

            var saveFile = "translated_" + name;

            const string savePath = "";

            const bool masters = false;

            var elements = new List <int>();

            var workingDirectoryPath = AppSettings.OutputDirectory + sessionId + "/" + Path.GetFileName(filePath);

            var fileApi = new FileApi(conf);

            using (var stream = File.Open(workingDirectoryPath, FileMode.Open))
            {
                var uploadRequest = new UploadFileRequest {
                    File = stream, Path = name, StorageName = storage
                };
                fileApi.UploadFile(uploadRequest);
            }

            var translationApi = new TranslationApi(conf);
            var request        = translationApi.CreateDocumentRequest(name, folder, pair, format, outFormat, storage, saveFile, savePath, masters, elements);

            translationApi.RunTranslationTask(request);

            var downloadRequest = new DownloadFileRequest {
                StorageName = storage, Path = saveFile
            };
            var result = fileApi.DownloadFile(downloadRequest);

            Directory.CreateDirectory(AppSettings.OutputDirectory + sessionId);
            var outFileName         = "translated_" + Path.GetFileName(filePath);
            var outputDirectoryPath = AppSettings.OutputDirectory + sessionId + "/" + outFileName;

            using (var file = new FileStream(outputDirectoryPath, FileMode.Create, FileAccess.Write))
            {
                result.CopyTo(file);
            }

            var convertWorkbook   = new Workbook(outputDirectoryPath);
            var convertWorksheets = convertWorkbook.Worksheets;
            var sourceWorksheets  = sourceWorkbook.Worksheets;

            foreach (var convertWorksheet in convertWorksheets)
            {
                var sourceWorksheet = sourceWorksheets[convertWorksheet.Index];
                var convertCells    = convertWorksheet.Cells;
                var sourceCells     = sourceWorksheet.Cells;

                foreach (Cell convertCell in convertCells)
                {
                    if (!convertCell.IsFormula && convertCell.Type == CellValueType.IsString)
                    {
                        sourceCells[convertCell.Row, convertCell.Column].PutValue(convertCell.Value);
                    }
                }
            }

            var filename = Path.GetFileNameWithoutExtension(outFileName) + "." + outputType.ToLower();
            var doc      = new DocumentInfo
            {
                FileName   = filename,
                FolderName = sessionId,
                Workbook   = sourceWorkbook
            };

            SaveDocument(doc, outputDirectoryPath, sessionId, GetSaveFormatType(filename));

            return(new Response
            {
                StatusCode = 200,
                Status = "OK",
                FileName = filename,
                FolderName = sessionId
            });
        }