Add translation request abstraction
This commit is contained in:
parent
88f4557a02
commit
1e99e58e1d
|
@ -0,0 +1,42 @@
|
|||
using System;
|
||||
using System.Collections.Specialized;
|
||||
using System.Windows.Forms;
|
||||
|
||||
namespace SpectacleTransformer {
|
||||
|
||||
public class GoogleTranslationRequest : HttpTranslationRequest {
|
||||
public static Uri GoogleTranslateUrl = new Uri("https://translate.google.com/m");
|
||||
|
||||
public GoogleTranslationRequest(Uri InstanceUri, Language Source, Language Target, string Text) : base(InstanceUri, Source, Target, Text) { }
|
||||
|
||||
public static new bool MatchesInstance(Uri InstanceUri) {
|
||||
return GoogleTranslateUrl.Host == InstanceUri.Host;
|
||||
}
|
||||
|
||||
public override Uri Url {
|
||||
get {
|
||||
NameValueCollection nvc = new NameValueCollection(3);
|
||||
nvc["sl"] = Source.SourceName;
|
||||
nvc["tl"] = Target.TargetName;
|
||||
nvc["q"] = Text;
|
||||
return new Uri(GoogleTranslateUrl, Utils.ToQueryString(nvc));
|
||||
}
|
||||
}
|
||||
|
||||
public override string DecodeResponse(string response) {
|
||||
WebBrowser wb = new WebBrowser();
|
||||
wb.DocumentText = response;
|
||||
foreach (HtmlElement element in wb.Document.GetElementsByTagName("div")) {
|
||||
if (element.GetAttribute("class") == "result-container") {
|
||||
return element.InnerText.Trim();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public override string DecodeErrorResponse(string responseBody) {
|
||||
// I have no idea what Google Translate errors look like.
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,102 @@
|
|||
using System;
|
||||
using System.IO;
|
||||
using System.Net;
|
||||
using System.Text;
|
||||
using System.Windows.Forms;
|
||||
|
||||
namespace SpectacleTransformer {
|
||||
|
||||
public abstract class HttpTranslationRequest : TranslationRequest {
|
||||
/// <summary>
|
||||
/// The URL to use when performing the request.
|
||||
/// </summary>
|
||||
public abstract Uri Url { get; }
|
||||
|
||||
public HttpTranslationRequest(Uri InstanceUri, Language Source, Language Target, string Text) : base(InstanceUri, Source, Target, Text) { }
|
||||
|
||||
public override string Run() {
|
||||
HttpWebResponse response = null;
|
||||
try {
|
||||
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(Url);
|
||||
request.UserAgent = String.Format("{0}/{1}", Application.ProductName, Application.ProductVersion);
|
||||
|
||||
response = (HttpWebResponse)request.GetResponse();
|
||||
|
||||
string responseBody;
|
||||
using (Stream stream = response.GetResponseStream()) {
|
||||
StreamReader reader = new StreamReader(stream, Encoding.UTF8);
|
||||
responseBody = reader.ReadToEnd();
|
||||
}
|
||||
|
||||
if (response.StatusCode == HttpStatusCode.OK || response.StatusCode == HttpStatusCode.Created) {
|
||||
string translation = DecodeResponse(responseBody);
|
||||
|
||||
if (translation == null) {
|
||||
MessageBox.Show(
|
||||
"The API request was performed successfully, but the server did not return a valid translation payload. Response payload:\r\n" + responseBody,
|
||||
"API error",
|
||||
MessageBoxButtons.OK,
|
||||
MessageBoxIcon.Error
|
||||
);
|
||||
}
|
||||
|
||||
return translation;
|
||||
}
|
||||
|
||||
string errorMessage = DecodeErrorResponse(responseBody);
|
||||
|
||||
if (String.IsNullOrEmpty(errorMessage)) {
|
||||
errorMessage = String.Format(
|
||||
"An unknown error occured while translating (HTTP {0} {1})",
|
||||
response.StatusCode,
|
||||
response.StatusDescription
|
||||
);
|
||||
} else {
|
||||
errorMessage = String.Format(
|
||||
"An error occured while translating (HTTP {0} {1}):\r\n{2}",
|
||||
response.StatusCode,
|
||||
response.StatusDescription,
|
||||
errorMessage
|
||||
);
|
||||
}
|
||||
|
||||
MessageBox.Show(
|
||||
errorMessage,
|
||||
"API error",
|
||||
MessageBoxButtons.OK,
|
||||
MessageBoxIcon.Error
|
||||
);
|
||||
return null;
|
||||
} catch (WebException ex) {
|
||||
MessageBox.Show(
|
||||
"An error occured while running the API request:\r\n" + ex.ToString(),
|
||||
"API error",
|
||||
MessageBoxButtons.OK,
|
||||
MessageBoxIcon.Error
|
||||
);
|
||||
return null;
|
||||
} finally {
|
||||
if (response != null) response.Close();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Decode a successful response body into a translated text.
|
||||
/// </summary>
|
||||
/// <param name="responseBody">The retrieved response body.</param>
|
||||
/// <returns>The translated text, or null if the translation failed.</returns>
|
||||
public virtual string DecodeResponse(string responseBody) {
|
||||
return responseBody;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Decode an error response body into an error message.
|
||||
/// </summary>
|
||||
/// <param name="responseBody">The retrieved response body.</param>
|
||||
/// <returns>An error message, or null if there is no particular error message.</returns>
|
||||
public virtual string DecodeErrorResponse(string responseBody) {
|
||||
return responseBody;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,45 @@
|
|||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
92
MainForm.cs
92
MainForm.cs
|
@ -1,11 +1,5 @@
|
|||
using System;
|
||||
using System.IO;
|
||||
using System.Net;
|
||||
using System.Text;
|
||||
using System.Windows.Forms;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace SpectacleTransformer {
|
||||
public partial class MainForm : Form {
|
||||
|
@ -211,80 +205,36 @@ namespace SpectacleTransformer {
|
|||
|
||||
private void translateButton_Click(object sender, EventArgs e) {
|
||||
mainTableLayoutPanel.Enabled = false;
|
||||
Uri targetUri = new Uri(
|
||||
new Uri(instanceUrlPicker.Text),
|
||||
String.Format(
|
||||
"api/v1/{0}/{1}/{2}",
|
||||
Uri.EscapeUriString(((Language)sourceLanguagePicker.SelectedItem).SourceName),
|
||||
Uri.EscapeUriString(((Language)destinationLanguagePicker.SelectedItem).TargetName),
|
||||
Uri.EscapeUriString(sourceTextBox.Text)
|
||||
)
|
||||
|
||||
// TODO: Use a list to allow registering various translation services
|
||||
TranslationRequest tr;
|
||||
Uri instanceUri = new Uri(instanceUrlPicker.Text);
|
||||
if (GoogleTranslationRequest.MatchesInstance(instanceUri)) tr = new GoogleTranslationRequest(
|
||||
instanceUri,
|
||||
(Language)sourceLanguagePicker.SelectedItem,
|
||||
(Language)destinationLanguagePicker.SelectedItem,
|
||||
sourceTextBox.Text
|
||||
);
|
||||
translator.RunWorkerAsync(targetUri);
|
||||
else tr = new LingvaTranslationRequest(
|
||||
instanceUri,
|
||||
(Language)sourceLanguagePicker.SelectedItem,
|
||||
(Language)destinationLanguagePicker.SelectedItem,
|
||||
sourceTextBox.Text
|
||||
);
|
||||
|
||||
translator.RunWorkerAsync(tr);
|
||||
}
|
||||
|
||||
private void translator_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e) {
|
||||
HttpWebResponse response = null;
|
||||
try {
|
||||
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create((Uri)e.Argument);
|
||||
request.UserAgent = String.Format("{0}/{1}", Application.ProductName, Application.ProductVersion);
|
||||
|
||||
response = (HttpWebResponse)request.GetResponse();
|
||||
|
||||
string responseBody;
|
||||
using (Stream stream = response.GetResponseStream()) {
|
||||
StreamReader reader = new StreamReader(stream, Encoding.UTF8);
|
||||
responseBody = reader.ReadToEnd();
|
||||
}
|
||||
|
||||
if (response.StatusCode == HttpStatusCode.OK || response.StatusCode == HttpStatusCode.Created) {
|
||||
string translation = null;
|
||||
try {
|
||||
JsonConvert.DeserializeObject<Dictionary<string, string>>(responseBody).TryGetValue("translation", out translation);
|
||||
} catch (Exception) { }
|
||||
|
||||
if (String.IsNullOrEmpty(translation)) {
|
||||
MessageBox.Show(
|
||||
"The API request was performed successfully, but the server did not return a valid translation payload. Response payload:\r\n" + responseBody,
|
||||
"Unexpected API response",
|
||||
MessageBoxButtons.OK,
|
||||
MessageBoxIcon.Error
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
e.Result = translation;
|
||||
} else {
|
||||
string errorMessage;
|
||||
try {
|
||||
if (!JsonConvert.DeserializeObject<Dictionary<string, string>>(responseBody).TryGetValue("errorMsg", out errorMessage)) {
|
||||
errorMessage = responseBody;
|
||||
}
|
||||
} catch (Exception) {
|
||||
errorMessage = responseBody;
|
||||
}
|
||||
|
||||
MessageBox.Show(
|
||||
String.Format(
|
||||
"An error occured while translating (HTTP {0} {1}):\r\n{2}",
|
||||
response.StatusCode,
|
||||
response.StatusDescription,
|
||||
errorMessage
|
||||
),
|
||||
"API error",
|
||||
MessageBoxButtons.OK,
|
||||
MessageBoxIcon.Error
|
||||
);
|
||||
}
|
||||
} catch (WebException ex) {
|
||||
e.Result = ((TranslationRequest)e.Argument).Run();
|
||||
} catch (Exception ex) {
|
||||
MessageBox.Show(
|
||||
"An error occured while running the API request:\r\n" + ex.ToString(),
|
||||
"API request error",
|
||||
"An unexpected error occurred when running the translation request: " + ex.ToString(),
|
||||
"Error",
|
||||
MessageBoxButtons.OK,
|
||||
MessageBoxIcon.Error
|
||||
);
|
||||
} finally {
|
||||
if (response != null) response.Close();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -58,7 +58,8 @@
|
|||
<ApplicationIcon>icon.ico</ApplicationIcon>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Reference Include="Newtonsoft.Json">
|
||||
<Reference Include="Newtonsoft.Json, Version=13.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
|
||||
<SpecificVersion>False</SpecificVersion>
|
||||
<HintPath>lib\Newtonsoft.Json\Newtonsoft.Json.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="System" />
|
||||
|
@ -66,7 +67,10 @@
|
|||
<Reference Include="System.Windows.Forms" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="GoogleTranslationRequest.cs" />
|
||||
<Compile Include="HttpTranslationRequest.cs" />
|
||||
<Compile Include="Language.cs" />
|
||||
<Compile Include="LingvaTranslationRequest.cs" />
|
||||
<Compile Include="MainForm.cs">
|
||||
<SubType>Form</SubType>
|
||||
</Compile>
|
||||
|
@ -74,6 +78,9 @@
|
|||
<DependentUpon>MainForm.cs</DependentUpon>
|
||||
</Compile>
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
<Compile Include="TranslationResult.cs" />
|
||||
<Compile Include="Utils.cs" />
|
||||
<Compile Include="TranslationRequest.cs" />
|
||||
<EmbeddedResource Include="MainForm.resx">
|
||||
<DependentUpon>MainForm.cs</DependentUpon>
|
||||
</EmbeddedResource>
|
||||
|
|
|
@ -0,0 +1,49 @@
|
|||
using System;
|
||||
|
||||
namespace SpectacleTransformer {
|
||||
public abstract class TranslationRequest {
|
||||
public readonly Uri InstanceUri;
|
||||
public readonly Language Source;
|
||||
public readonly Language Target;
|
||||
public readonly string Text;
|
||||
|
||||
/// <summary>
|
||||
/// Create a new translation request.
|
||||
/// </summary>
|
||||
/// <param name="InstanceUri">URI pointing to the server instance to use.</param>
|
||||
/// <param name="Source">The source language.</param>
|
||||
/// <param name="Target">The target language.</param>
|
||||
/// <param name="Text">The text to translate.</param>
|
||||
public TranslationRequest(Uri InstanceUri, Language Source, Language Target, string Text) {
|
||||
if (Source.Equals(Target)) throw new ArgumentException("The source and target languages cannot be the same.");
|
||||
this.InstanceUri = InstanceUri;
|
||||
this.Source = Source;
|
||||
this.Target = Target;
|
||||
this.Text = Text;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Determines if this TranslationRequest can be instanciated with an instance URI.
|
||||
/// </summary>
|
||||
/// <param name="InstanceUri">The instance URI to check.</param>
|
||||
/// <returns>Whether this translation request can be performed on this instance URI.</returns>
|
||||
public static bool MatchesInstance(Uri InstanceUri) {
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Perform a translation request.
|
||||
/// </summary>
|
||||
/// <returns>The translated text, or null if the translation failed.</returns>
|
||||
public abstract string Run();
|
||||
|
||||
/// <summary>
|
||||
/// Called in the main thread with UI controls to allow further post-processing that could require them.
|
||||
/// </summary>
|
||||
/// <param name="response">The response returned by Run().</param>
|
||||
/// <returns>A processed response.</returns>
|
||||
public virtual string PostProcess(string response) {
|
||||
return response;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,21 @@
|
|||
using System;
|
||||
using System.Collections.Specialized;
|
||||
using System.Text;
|
||||
|
||||
namespace SpectacleTransformer {
|
||||
internal static class Utils {
|
||||
internal static string ToQueryString(NameValueCollection nvc) {
|
||||
if (nvc.Count <= 0) return "";
|
||||
StringBuilder sb = new StringBuilder("?");
|
||||
foreach (string key in nvc.AllKeys) {
|
||||
foreach (string value in nvc.GetValues(key)) {
|
||||
sb.Append(Uri.EscapeUriString(key));
|
||||
sb.Append("=");
|
||||
sb.Append(Uri.EscapeUriString(value));
|
||||
sb.Append("&");
|
||||
}
|
||||
}
|
||||
return sb.ToString().TrimEnd('&');
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue