Introduction
The import of a TMX file using Congree's REST API is a three-step process:
- Upload the TMX file
- Acquire an ID for the import process
- Import the TMX
Each of those steps is supported by a corresponding endpoint.
Uploading a TMX
The following sample method demonstrates how to upload a TMX file.
Codeblock |
---|
language | c# |
---|
linenumbers | true |
---|
|
public TmxUploadResponse UploadTmx(string path)
{
var client = new RestClient(this.serverUrl + uploadTmxUrl);
var request = new RestRequest(Method.POST);
request.AddHeader("accept", "*/*");
request.AddHeader("Content-Type", "multipart/form-data");
request.AddHeader("authorization", "Bearer " + this.token);
request.AlwaysMultipartFormData = true;
request.AddFile("file", path);
try
{
var cancellationTokenSource = new CancellationTokenSource();
var response = client.Execute(request);
Console.WriteLine(response.StatusCode);
ResponseStatus status = response.ResponseStatus;
if (status == RestSharp.ResponseStatus.Completed)
{
TmxUploadResponse uploadResponse = JsonConvert.DeserializeObject<TmxUploadResponse>(response.Content);
return uploadResponse;
}
else
{
return new TmxUploadResponse();
}
}
catch (Exception ex)
{
return new TmxUploadResponse();
}
} |
The only parameter the method expcects is the full path to the TMX file to be uploaded. The method returns a TMXUploadResponse shown in the code below.
Codeblock |
---|
|
using System;
using System.Collections.Generic;
using System.Text;
namespace TMXImporter.Congree
{
public class TmxUploadResponse
{
public string Id { get; set; }
}
}
|
The Id return by this object is needed in the next step.
Acquiring an ID for the import process
Before the actual import can start the uploaded TMX file must be processed by Congree Authoring Server. To get to know if the import process can be initiated it is required to get the corresponding information as shown in the code below.
Codeblock |
---|
language | c# |
---|
linenumbers | true |
---|
|
public TmxUploadResult GetTmxUploadResult(string id)
{
var client = new RestClient(this.serverUrl + this.taskUrl + id);
var request = new RestRequest(Method.GET);
request.AddHeader("content-type", "application/json");
request.AddHeader("authorization", "Bearer " + this.token);
IRestResponse response = client.Execute(request);
TmxUploadResult uploadResult = JsonConvert.DeserializeObject<TmxUploadResult>(response.Content);
return uploadResult;
} |
The parameter is the id obtained in the first step.
The method returns an instance of the TmxUploadResult class shown below.
Codeblock |
---|
language | c# |
---|
linenumbers | true |
---|
|
using System;
using System.Collections.Generic;
using System.Text;
namespace TMXImporter.Congree
{
public class TmxUploadResult
{
public Result Result { get; set; }
}
public class Result
{
public string TmxId { get; set; }
public string SourceCultures { get; set; }
public List<string> Cultures { get; set; }
public int TranslationUnitsCount { get; set; }
}
}
|
The reponse will be used in the last step.
Importing the TMX file
A sample method is shown below.
Codeblock |
---|
language | c# |
---|
linenumbers | true |
---|
|
public void ImportTmx(ImportTmxRequest importTmxRequest)
{
var client = new RestClient(this.serverUrl + this.importTmxUrl);
var request = new RestRequest(Method.POST);
request.AddHeader("Content-Type", "application/json");
request.AddHeader("Authorization", "Bearer " + this.token);
JsonSerializerSettings settings = new JsonSerializerSettings();
settings.TypeNameHandling = TypeNameHandling.None;
string serializedRequestJSON = JsonConvert.SerializeObject(importTmxRequest, settings);
request.AddParameter("application/json", serializedRequestJSON, ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
} |
The parameter the method is expecting is an instance of the class TmxRequest which is shown in the sample code below.
Codeblock |
---|
language | c# |
---|
linenumbers | true |
---|
|
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.Text;
namespace TMXImporter.Congree
{
public class ImportTmxRequest
{
public string TmxId { get; set; }
public Dictionary<string,string> ImportedCultures { get; set; }
[JsonExtensionData]
public Dictionary<string, JToken> Rulesets { get; set; }
public Boolean TrackTranslations { get; set; }
}
}
|
Property | Description |
---|
TmxId | The id obtained in the second step |
ImportedCultures | The cultures to be imported. The key value is a culture contained in the TMX file. The value id the culture in Congree's Authoring Memory. |
Rulesets | If certain cultures should be checked during the import the mapping between the culture and the rule set is defined here. The key is the culture in the TMX file. The value is the name of the rule set to be applied for the actual culture. |
TrackTranslations | tbd |