Last active
August 5, 2021 19:01
-
-
Save groupdocs-cloud-gists/b90601ff74a27b320b8a3a8c2906837d to your computer and use it in GitHub Desktop.
Translate Text or Documents using REST API in C#
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1. Translate Documents using a REST API in C# | |
2. Translate Text using a REST API in C# |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Configuration conf = new Configuration(); | |
conf.ClientId = "659fe7da-715b-4744-a0f7-cf469a392b73"; | |
conf.ClientSecret = "b377c36cfa28fa69960ebac6b6e36421"; | |
string storageName = ""; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// api initialization | |
FileApi api = new FileApi(conf); | |
// create download file request | |
DownloadFileRequest request = new DownloadFileRequest(); | |
request.storageName = storageName; | |
request.path = "sample-fr.docx"; | |
// download file | |
Stream response = api.DownloadFile(request); | |
// save file to working directory | |
using (var fileStream = System.IO.File.Create("C:\\Files\\sample-fr.docx")) | |
{ | |
response.Seek(0, SeekOrigin.Begin); | |
response.CopyTo(fileStream); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// file to translate here | |
string name = "sample.docx"; | |
// folder path if any | |
string folder = ""; | |
// translation language pair | |
string pair = "en-fr"; | |
// add format of file to translate | |
string format = "docx"; | |
// add format of translated file | |
string outFormat = "docx"; | |
// add storage name | |
string storage = ""; | |
// translated file | |
string saveFile = "sample-fr.docx"; | |
// folder path to save translated file | |
string savePath = ""; | |
// in case of PowerPoint translation, set if master slides should be translated | |
bool masters = false; | |
// in case of Excel or Powerpoint translation, put zero based indexes of worksheets or slides to translate | |
List<int> elements = new List<int>(); | |
// api initialization | |
TranslationApi api = new TranslationApi(configuration); | |
// create document request | |
TranslateDocumentRequest request = api.CreateDocumentRequest(name, folder, pair, format, outFormat, storage, saveFile, savePath, masters, elements); | |
// run translation text | |
TranslationResponse response = api.RunTranslationTask(request); | |
Console.WriteLine(response); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// language pair | |
string pair = "en-fr"; | |
// add text to translate here | |
string text = "This is sample text."; | |
// api initialization | |
TranslationApi api = new TranslationApi(configuration); | |
// create text request | |
TranslateTextRequest request = api.CreateTextRequest(pair, text); | |
// run translation text task | |
TextResponse response = api.RunTranslationTextTask(request); | |
Console.WriteLine(response); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// initialize api | |
FileApi api = new FileApi(configuration); | |
// open file | |
FileStream fileStream = File.Open(@"C:\Files\sample.docx", FileMode.Open); | |
// upload file request | |
UploadFileRequest request = new UploadFileRequest(); | |
request.storageName = ""; | |
request.path = "sample.docx"; | |
request.File = fileStream; | |
// upload file | |
FilesUploadResult response = api.UploadFile(request); | |
Console.WriteLine(response.Uploaded.Count.ToString()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment