46 lines
1.6 KiB
C#
46 lines
1.6 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using Newtonsoft.Json;
|
|
|
|
namespace SpectacleTransformer {
|
|
|
|
public class LingvaTranslationRequest : HttpTranslationRequest {
|
|
public LingvaTranslationRequest(Uri InstanceUri, Language Source, Language Target, string Text) : base(InstanceUri, Source, Target, Text) { }
|
|
|
|
public override Uri Url {
|
|
get {
|
|
return new Uri(InstanceUri, String.Format(
|
|
"api/v1/{0}/{1}/{2}",
|
|
Uri.EscapeUriString(Source.SourceName),
|
|
Uri.EscapeUriString(Target.TargetName),
|
|
Uri.EscapeUriString(Text)
|
|
));
|
|
}
|
|
}
|
|
|
|
public static new bool MatchesInstance(Uri InstanceUri) {
|
|
return GoogleTranslationRequest.GoogleTranslateUrl.Host != InstanceUri.Host;
|
|
}
|
|
|
|
public override string DecodeResponse(string responseBody) {
|
|
string translation = null;
|
|
try {
|
|
JsonConvert.DeserializeObject<Dictionary<string, string>>(responseBody).TryGetValue("translation", out translation);
|
|
} catch (Exception) { }
|
|
return translation;
|
|
}
|
|
|
|
public override string DecodeErrorResponse(string responseBody) {
|
|
string errorMessage;
|
|
try {
|
|
if (!JsonConvert.DeserializeObject<Dictionary<string, string>>(responseBody).TryGetValue("errorMsg", out errorMessage)) {
|
|
return responseBody;
|
|
}
|
|
return errorMessage;
|
|
} catch (Exception) {
|
|
return responseBody;
|
|
}
|
|
}
|
|
}
|
|
}
|