static Dictionary <string, string[]> GetLanguagePairs(Configuration conf)
        {
            TranslationApi api = new TranslationApi(conf);
            Dictionary <string, string[]> response = api.GetLanguagePairs();

            return(response);
        }
        internal static (string txt, string err) TranslateText(string text, string pair)
        {
            Console.WriteLine("TranslateText");
            if (string.IsNullOrWhiteSpace(pair))
            {
                return("", "No language pair provided");
            }
            Configuration cfg = new Configuration {
                AppKey = Common.GroupDocsKey,
                AppSid = Common.GroupDocsSID
            };
            TranslationApi api      = new TranslationApi(cfg);
            TextInfo       textInfo = new TextInfo {
                Pair = pair,
                Text = text
            };
            string userRequest                = $"'[{JsonConvert.SerializeObject(textInfo)}]'";
            TranslateTextRequest request      = new TranslateTextRequest(userRequest);
            TextResponse         textResponse = api.RunTranslationTextTask(request);

            if (textResponse.Status != "ok")
            {
                return("", textResponse.ToString());
            }
            return(textResponse.Translation, "");
        }
        static NET.Model.FileInfo GetDocRequest(Configuration conf)
        {
            TranslationApi api = new TranslationApi(conf);

            NET.Model.FileInfo info = api.GetDocumentRequestStructure();
            return(info);
        }
        static TranslationResponse HealthCheck(Configuration conf)
        {
            TranslationApi      api      = new TranslationApi(conf);
            TranslationResponse response = api.RunHealthCheck();

            return(response);
        }
        static TextInfo GetTextRequest(Configuration conf)
        {
            TranslationApi api  = new TranslationApi(conf);
            TextInfo       info = api.GetTextRequestStructure();

            return(info);
        }
        public void Test_GetHtmlTranslateByURL_en_fr_1()
        {
            string sourceUrl = @"https://www.le.ac.uk/oerresources/bdra/html/page_02.htm";

            Stream stream = TranslationApi.GetTranslateDocumentByUrl(sourceUrl, "en", "fr");

            Assert.IsNotNull(stream);
            Assert.IsTrue(stream.GetType() == typeof(FileStream));
            Assert.IsTrue(File.Exists(((FileStream)stream).Name));
        }
        static TextResponse TranslateText(Configuration conf)
        {
            // add text for translation and language pair
            string pair = "en-fr";
            string text = "Welcome to Paris";

            TranslationApi       api      = new TranslationApi(conf);
            TranslateTextRequest request  = api.CreateTextRequest(pair, text);
            TextResponse         response = api.RunTranslationTextTask(request);

            return(response);
        }
        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");
        }
        public void Test_PutHtmlTranslateByURL_en_de_1()
        {
            string sourceUrl = @"https://www.le.ac.uk/oerresources/bdra/html/page_02.htm";
            string folder    = "TempHtml";

            var response = TranslationApi.PutTranslateDocumentByUrl(sourceUrl, "en", "de", folder);

            Assert.AreEqual("storage", (string)response.Content);
            Assert.IsTrue(response.ContentName != null);

            var path   = string.Format("{0}/{1}", folder, response.ContentName);
            var stResp = StorageApi.GetIsExist(path, null, null);

            Assert.IsTrue(stResp.FileExist.IsExist);
        }
示例#10
0
        public void Test_GetHtmlTranslate_en_fr_1()
        {
            string name        = "testpage1.html";
            string folder      = "HtmlTestTranslate";
            string storagePath = $"{folder}/{name}";

            string srcPath = Path.Combine(dataFolder, name);

            StorageApi.PutCreate(storagePath, null, null, File.ReadAllBytes(srcPath));
            FileExistResponse resp = StorageApi.GetIsExist(storagePath, null, null);

            Assert.IsTrue(resp.FileExist.IsExist);

            Stream stream = TranslationApi.GetTranslateDocument(
                name, "en", "fr", folder, null);

            Assert.IsNotNull(stream);
            Assert.IsTrue(stream.GetType() == typeof(FileStream));
            Assert.IsTrue(File.Exists(((FileStream)stream).Name));
        }
        public void Run()
        {
            string name    = "testpage1.html";
            string folder  = null;
            string storage = null;

            string srcPath = Path.Combine(CommonSettings.DataFolder, name);

            if (File.Exists(srcPath))
            {
                var storagePath = !string.IsNullOrEmpty(folder)
                    ? string.Format("{0}/{1}", folder, name) : name;

                StorageApi storageApi = new StorageApi(CommonSettings.AppKey, CommonSettings.AppSID, CommonSettings.BasePath);
                storageApi.PutCreate(storagePath, null, storage, File.ReadAllBytes(srcPath));
                var response = storageApi.GetIsExist(storagePath, null, storage);
                if (response.FileExist.IsExist)
                {
                    TranslationApi transApi = new TranslationApi(CommonSettings.AppKey, CommonSettings.AppSID, CommonSettings.BasePath);
                    Stream         stream   = transApi.GetTranslateDocument(name, SrcLang, ResLang, folder, storage);

                    if (stream != null && stream.GetType() == typeof(FileStream))
                    {
                        string outName = ((FileStream)stream).Name;
                        string outPath = Path.Combine(CommonSettings.OutDirectory, Path.GetFileName(outName));
                        using (FileStream fstr = new FileStream(outPath, FileMode.Create, FileAccess.Write))
                        {
                            stream.CopyTo(fstr);
                            fstr.Flush();
                            Console.WriteLine(string.Format("File '{0}' downloaded to: {1}", Path.GetFileName(outName), outPath));
                        }
                    }
                    stream.Close();
                    stream.Dispose();
                }
            }
            else
            {
                throw new FileNotFoundException("File not found in the Data folder", name);
            }
        }
示例#12
0
        public static void ClassInit(TestContext context)
        {
            if (!File.Exists("../../apiKey.txt"))
            {
                throw new Exception("To properly run the tests, please add an apiKey.txt file containing your api key in the SystranClientTranslationApiLibTests folder or edit the test file with your key");
            }
            client = new ApiClient("https://platform.systran.net:8904");
            Configuration.apiClient = client;
            Dictionary <String, String> keys = new Dictionary <String, String>();
            string key;

            using (StreamReader streamReader = new StreamReader("../../apiKey.txt", Encoding.UTF8))
            {
                key = streamReader.ReadToEnd();
            }
            keys.Add("key", key); Configuration.apiKey = keys; Configuration.apiKey = keys;
            Configuration.apiKey = keys;
            if (keys.Count == 0)
            {
                throw new Exception("No api key found, please check your apiKey.txt file");
            }
            translationApi = new TranslationApi(Configuration.apiClient);
        }
示例#13
0
        public void Test_PutHtmlTranslate_en_ru_1()
        {
            string name        = "testpage1.html";
            string folder      = "HtmlTestTranslate";
            string storagePath = $"{folder}/{name}";

            string srcPath = Path.Combine(dataFolder, name);

            StorageApi.PutCreate(storagePath, null, null, File.ReadAllBytes(srcPath));
            FileExistResponse resp = StorageApi.GetIsExist(storagePath, null, null);

            Assert.IsTrue(resp.FileExist.IsExist);

            var response = TranslationApi.PutTranslateDocument(name, "en", "ru", folder);

            Assert.AreEqual("storage", (string)response.Content);
            Assert.IsTrue(response.ContentName != null);

            storagePath = string.Format("{0}/{1}", folder, response.ContentName);
            var stResp = StorageApi.GetIsExist(storagePath, null, null);

            Assert.IsTrue(stResp.FileExist.IsExist);
        }
        public void Run()
        {
            string srcUrl = @"http://www.htmlhelp.com/reference/css/stylesheets-now.html";
            //string srcUrl = @"https://www.le.ac.uk/oerresources/bdra/html/page_01.htm";
            string folder  = null; // default folder is root
            string storage = null; // default storage is AmazonS3

            TranslationApi     transApi = new TranslationApi(CommonSettings.AppKey, CommonSettings.AppSID, CommonSettings.BasePath);
            NativeRestResponse resp     = transApi.PutTranslateDocumentByUrl(srcUrl, SrcLang, ResLang, folder, storage);

            if (resp.ContentType == NativeRestResponse.RespContentType.FileName &&
                resp.ContentName != null)
            {
                StorageApi storageApi  = new StorageApi(CommonSettings.AppKey, CommonSettings.AppSID, CommonSettings.BasePath);
                var        storagePath = !string.IsNullOrEmpty(folder)
                    ? string.Format("{0}/{1}", folder, resp.ContentName) : resp.ContentName;
                var stResp = storageApi.GetIsExist(storagePath, null, storage);
                if (stResp.FileExist.IsExist)
                {
                    Console.WriteLine(string.Format("\nFile '{0}' created in the cloud storage: folder = '{1}', storage = '{2}' ",
                                                    resp.ContentName, folder ?? "", storage ?? "<default>"));

                    ResponseMessage resp2 = storageApi.GetDownload(storagePath, null, storage);
                    using (MemoryStream resStream = new MemoryStream(resp2.ResponseStream))
                    {
                        var outPath = Path.Combine(CommonSettings.OutDirectory, resp.ContentName);
                        using (FileStream fstr = new FileStream(outPath, FileMode.Create, FileAccess.Write))
                        {
                            resStream.Position = 0;
                            resStream.CopyTo(fstr);
                            fstr.Flush();
                            Console.WriteLine(string.Format("\nResult file downloaded to: {0}", outPath));
                        }
                    }
                }
            }
        }
        public void Run()
        {
            string srcUrl = "https://stallman.org/sayings.html";
            //string srcUrl = @"https://www.le.ac.uk/oerresources/bdra/html/page_01.htm";

            TranslationApi transApi = new TranslationApi(CommonSettings.AppKey, CommonSettings.AppSID, CommonSettings.BasePath);
            Stream         stream   = transApi.GetTranslateDocumentByUrl(srcUrl, SrcLang, ResLang);

            string strName = null;

            if (stream != null && stream.GetType() == typeof(FileStream))
            {
                string name    = ((FileStream)stream).Name;
                string outPath = Path.Combine(CommonSettings.OutDirectory, Path.GetFileName(name));
                using (FileStream fstr = new FileStream(outPath, FileMode.Create, FileAccess.Write))
                {
                    stream.CopyTo(fstr);
                    fstr.Flush();
                    Console.WriteLine(string.Format("File '{0}' downloaded to: {1}", Path.GetFileName(name), outPath));
                }
            }
            stream.Close();
            stream.Dispose();
        }
        public void Test_GetHtmlTranslate_en_fr_1()
        {
            string name        = "testpage1.html";
            string folder      = "HtmlTestTranslate";
            string storagePath = $"{folder}/{name}";

            string srcPath = Path.Combine(dataFolder, name);

            using (Stream fstr = new FileStream(srcPath, FileMode.Open, FileAccess.Read))
            {
                PutCreateRequest reqCr = new PutCreateRequest(storagePath, fstr);
                this.StorageApi.PutCreate(reqCr);
                GetIsExistRequest reqExist = new GetIsExistRequest(storagePath);
                FileExistResponse resp     = this.StorageApi.GetIsExist(reqExist);
                Assert.IsTrue(resp.FileExist.IsExist.HasValue && resp.FileExist.IsExist.Value);
            }

            Stream stream = TranslationApi.GetTranslateDocument(
                name, "en", "fr", folder, null);

            Assert.IsNotNull(stream);
            Assert.IsTrue(stream.GetType() == typeof(FileStream));
            Assert.IsTrue(File.Exists(((FileStream)stream).Name));
        }
示例#17
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
            });
        }
 public void Translate()
 {
     Output = TranslationApi.Translate(Input, SelectedFromLanguage, SelectedToLanguage);
     NotifyOfPropertyChange(() => Output);
 }