Skip to content

Instantly share code, notes, and snippets.

@groupdocs-cloud-gists
Last active August 5, 2021 19:01
Show Gist options
  • Save groupdocs-cloud-gists/b90601ff74a27b320b8a3a8c2906837d to your computer and use it in GitHub Desktop.
Save groupdocs-cloud-gists/b90601ff74a27b320b8a3a8c2906837d to your computer and use it in GitHub Desktop.
Translate Text or Documents using REST API in C#
1. Translate Documents using a REST API in C#
2. Translate Text using a REST API in C#
Configuration conf = new Configuration();
conf.ClientId = "659fe7da-715b-4744-a0f7-cf469a392b73";
conf.ClientSecret = "b377c36cfa28fa69960ebac6b6e36421";
string storageName = "";
// 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);
}
// 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);
// 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);
// 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