Exemplo n.º 1
0
        /// <summary>
        /// Create request for document translation
        /// </summary>
        /// <param name="name"></param>
        /// <param name="folder"></param>
        /// <param name="pair"></param>
        /// <param name="format"></param>
        /// <param name="storage"></param>
        /// <param name="savefile"></param>
        /// <param name="savepath"></param>
        /// <param name="masters"></param>
        /// <param name="elements"></param>
        /// <returns></returns>
        public TranslateDocumentRequest CreateDocumentRequest(string name,
                                                              string folder,
                                                              string pair,
                                                              string format,
                                                              string outformat,
                                                              string storage,
                                                              string savefile,
                                                              string savepath,
                                                              bool masters,
                                                              List <int> elements,
                                                              string origin = ".NET",
                                                              bool details  = false)
        {
            Model.FileInfo fileInfo = new Model.FileInfo();
            fileInfo.Folder    = folder;
            fileInfo.Format    = format;
            fileInfo.OutFormat = outformat;
            fileInfo.Name      = name;
            fileInfo.Pair      = pair;
            fileInfo.SaveFile  = savefile;
            fileInfo.SavePath  = savepath;
            fileInfo.Storage   = storage;
            fileInfo.Masters   = masters;
            fileInfo.Elements  = elements;
            fileInfo.Origin    = origin;
            fileInfo.Details   = details;
            string userRequest = String.Format("'[{0}]'", JsonConvert.SerializeObject(fileInfo));
            TranslateDocumentRequest request = new TranslateDocumentRequest(userRequest);

            return(request);
        }
        public TranslateDocumentRequest CreateDocumentRequest(
            string name,
            string folder,
            string pair,
            string format,
            string outformat,
            string storage,
            string savefile,
            string savepath,
            bool masters,
            List <int> elements
            )
        {
            var fileInfo = new FileInfo
            {
                Folder    = folder,
                Format    = format,
                OutFormat = outformat,
                Name      = name,
                Pair      = pair,
                SaveFile  = savefile,
                SavePath  = savepath,
                Storage   = storage,
                Masters   = masters,
                Elements  = elements
            };
            var userRequest = $"'[{JsonConvert.SerializeObject(fileInfo)}]'";
            var request     = new TranslateDocumentRequest(userRequest);

            return(request);
        }
        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");
        }
Exemplo n.º 4
0
        /// <summary>
        /// Translate document
        /// </summary>
        /// <param name="request">Request.  <see cref="TranslateDocumentRequest"/> </param>
        /// <returns><see cref="TranslationResponse"/></returns>
        public TranslationResponse RunTranslationTask(TranslateDocumentRequest request)
        {
            if (request.UserRequest == null)
            {
                throw new ApiException(400, "Empty request");
            }

            var resourcePath = this.configuration.GetApiRootUrl() + "/document";

            resourcePath = Regex
                           .Replace(resourcePath, "\\*", string.Empty)
                           .Replace("&amp;", "&")
                           .Replace("/?", "?");

            try
            {
                var response = this.apiInvoker.InvokeApi(
                    resourcePath,
                    "POST",
                    request.UserRequest,
                    null,
                    null);
                if (response != null)
                {
                    return((TranslationResponse)SerializationHelper.Deserialize(response, typeof(TranslationResponse)));
                }

                return(null);
            }
            catch (ApiException ex)
            {
                if (ex.ErrorCode == 404)
                {
                    return(null);
                }

                throw;
            }
        }