Translating Markdown files with GroupDocs.Translation SDK

Although you can directly call the GroupDocs.Translation Cloud REST API to send Markdown file for translation and fetch translated file, there is a much easier way to implement translation functionality in your applications. We provide software development kits (SDKs) for all popular programming languages. They wrap up all routine operations such as establishing connections, sending API requests, and parsing responses into a few simple methods. It makes interaction with GroupDocs.Translation Cloud services much easier, allowing you to focus on business logic rather than technical details.

using System.IO;
using System.Collections.Generic;
using System.Net.Http;
using GroupDocs.Translation.Cloud.Sdk.Api;
using GroupDocs.Translation.Cloud.Sdk.Client;
using GroupDocs.Translation.Cloud.Sdk.Client.Auth;
using GroupDocs.Translation.Cloud.Sdk.Model;
using Configuration = GroupDocs.Translation.Cloud.Sdk.Client.Configuration;
using HttpStatusCode = System.Net.HttpStatusCode;

namespace GroupDocs.Translation.Cloud.Sdk
{
	public class FileTranslator
	{
		public FileTranslator()
		{
			Configuration config = new Configuration();
			/** Authorize your requests to GroupDocs.Translation Cloud */
			config.OAuthFlow = OAuthFlow.APPLICATION;
			config.OAuthClientId = "YOU_CLIENT_ID";
			config.OAuthClientSecret = "YOU_CLIENT_SECRET";
			/** Initialize GroupDocs.Translation API */
			config.BasePath = "https://api.groupdocs.cloud/v2.0/translation";
			TranslationApi api = new TranslationApi(config);
			FileApi fileApi = new FileApi(config);
			/** Specify translation parameters */
			string filePath = "/path/to/myfile.md";
			string savePath = "/path/to/savefile/";
			string sourceLanguage = "en";
			var targetLanguages = new List<string>() { "de" };
			string format = "md";
			string outputFormat = "md";
			byte[] file = File.ReadAllBytes(filePath);
			MemoryStream ms = new MemoryStream(file);
			string url = fileApi.FileUploadPost(format, ms);
			CloudFileResponse response = new CloudFileResponse();
			var request = new MarkdownFileRequest(
				sourceLanguage: sourceLanguage,
				targetLanguages: targets,
				url: url,
				outputFormat: outputFormat,
				savingMode: MarkdownFileRequest.SavingModeEnum.Files);
			/** Send file to translation */
			var responseId = await api.AutoPostAsync(request);			
			/** Wait for results from translation queue */
			try
			{
				if ((HttpStatusCode)responseId.Status == HttpStatusCode.Accepted)
				{
					while (true)
					{
						response = await api.DocumentRequestIdGetAsync(responseId.Id);
						if (response.Urls.Count > 0)
							break;
						else
							Thread.Sleep(2000);
					}
					Console.WriteLine("File translated");
					using (var client = new HttpClient())
					{
						foreach (string lang in targets)
						{
							string savePath = $"{savepath}{lang}.{outputFormat}";    
							var result = await client.GetByteArrayAsync(response.Urls[lang].Url);                            File.WriteAllBytes(savePath, result);
						}
					}
				}
				else
				{
					response = new CloudFileResponse() { Status = responseId.Status, Message = responseId.Message };
					Console.WriteLine("error: " + response.Message);
				}
			}
			catch (Exception ex)
			{
				Console.WriteLine("exception: " + ex.ToString());
			}    
		}
	}
}

Visit our GitHub repository for a working code and sample files: https://github.com/groupdocs-translation-cloud/groupdocs-translation-cloud-dotnet

import time

import groupdocs_translation_cloud
from groupdocs_translation_cloud import MarkdownFileRequest, Format

api = groupdocs_translation_cloud.api.TranslationApi()
file_api = groupdocs_translation_cloud.api.FileApi()
api.api_client.configuration.client_id = "YOU_CLIENT_ID"
api.api_client.configuration.client_secret = "YOU_CLIENT_SECRET"

url = file_api.file_upload_post(file="/path/to/yourfile.md", format=Format.Md)
file_request = MarkdownFileRequest(source_language="en", 
							           target_languages=["ru"], 
							           url=url, 
							           saving_mode=SavingMode.Files, 
							           output_format=Format.Md)
response = api.markdown_post(file_request)
if response.status == 202:
    while True:
        file_response = api.document_request_id_get(request_id)
        if file_response.status == 200:
            print(file_response.message)
            for lang in file_response.urls:
                print(lang + '_' + url + ': ' + file_response.urls[
                    lang].url)
            break
        time.sleep(2)

Visit our GitHub repository for a working code and sample files: https://github.com/groupdocs-translation-cloud/groupdocs-translation-cloud-python

package com.groupdocs;
// Import classes:

import com.groupdocs.model.*;
import org.openapitools.client.api.TranslationApi;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;

public class MdDemo {
    public static void main(String[] args) {
        String basePath = "https://api.groupdocs.cloud/v2.0/translation";
        String cliendId = "YOUR_CLIENT_ID";
        String clientSecret = "YOUR_CLIENT_SECRET";

        ApiClient defaultClient = new ApiClient(basePath, cliendId, clientSecret, null);
        TranslationApi translationApi = new TranslationApi(defaultClient);
        FileApi fileApi = new FileApi(defaultClient);

        MarkdownFileRequest fileRequest = new MarkdownFileRequest();

        String fileName = "FILE_PATH";
        File fileToTranslate = new File(fileName);
        String file_url = fileApi.fileUploadPost("FILE_FORMAT", fileToTranslate);
        fileRequest.setSourceLanguage("ru");
        fileRequest.addTargetLanguagesItem("en");
        fileRequest.setOutputFormat("md");
        fileRequest.setSavingMode(MarkdownFileRequest.SavingModeEnum.FILES);
        fileRequest.setShortCodeList(SHORT_CODE_LIST);
        fileRequest.setFrontMatterList(FRONT_MATTER_LIST);
        fileRequest.setUrl(file_url);

        try {
            StatusResponse response = translationApi.markdownPost(fileRequest);
            String _id = response.getId();
            if (!response.getStatus().toString().equals("500")) {
                while (true) {
                    CloudFileResponse fileResponse = translationApi.documentRequestIdGet(_id);
                    if (fileResponse.getStatus().toString().equals("200")){
                        System.out.println(fileResponse);
                        break;
                    }
                    try {
                        Thread.sleep(2000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
        catch(ApiException e){
            System.err.println("Exception when calling TranslationApi#markdownPost");
            System.err.println("Status code: " + e.getCode());
            System.err.println("Reason: " + e.getResponseBody());
            System.err.println("Response headers: " + e.getResponseHeaders());
            e.printStackTrace();
        }
        catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Visit our GitHub repository for a working code and sample files: https://github.com/groupdocs-translation-cloud/groupdocs-translation-cloud-java