Skip to content

Commit abf5d9a

Browse files
webspiderteamWebSpiderTeamsalarcode
authored
Fixed #17 (#19)
* Fixed #17 Excel Import : Linked field can't translate. Changed linked datasource and fixed default KeyColumn selection * Added DeepL Support * Minor cleanups --------- Co-authored-by: WebSpiderTeam <[email protected]> Co-authored-by: salar2k <[email protected]>
1 parent b7b3a1f commit abf5d9a

File tree

10 files changed

+381
-79
lines changed

10 files changed

+381
-79
lines changed

AutoResxTranslator/AutoResxTranslator.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
<Reference Include="System.Xml" />
5353
</ItemGroup>
5454
<ItemGroup>
55+
<Compile Include="DeepLTranslateService.cs" />
5556
<Compile Include="Definitions\ResultHolder.cs" />
5657
<Compile Include="Definitions\TranslationOptions.cs" />
5758
<Compile Include="frmAbout.cs">
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
using AutoResxTranslator.Definitions;
2+
using Newtonsoft.Json;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Net;
6+
using System.Net.Http;
7+
using System.Threading.Tasks;
8+
9+
namespace AutoResxTranslator
10+
{
11+
/// <summary>
12+
/// Translation service using Microsoft Cogntive service.
13+
///
14+
/// ref: https://azure.microsoft.com/en-in/services/cognitive-services/translator-text-api/
15+
/// </summary>
16+
public class DeepLTranslateService
17+
{
18+
private const string DeepLFreeCognitiveServicesApiUrl = "https://api-free.deepl.com";
19+
private const string DeepLProCognitiveServicesApiUrl = "https://api.deepl.com";
20+
21+
private readonly struct TextTranslateResult
22+
{
23+
/// <summary>Initializes a new instance of <see cref="TextTranslateResult" />, used for JSON deserialization.</summary>
24+
[JsonConstructor]
25+
public TextTranslateResult(TextResult[] translations)
26+
{
27+
Translations = translations;
28+
}
29+
30+
/// <summary>Array of <see cref="TextResult" /> objects holding text translation results.</summary>
31+
public TextResult[] Translations { get; }
32+
public sealed class TextResult
33+
{
34+
/// <summary>Initializes a new instance of <see cref="TextResult" />.</summary>
35+
/// <param name="text">Translated text.</param>
36+
/// <param name="detectedSourceLanguageCode">The detected language code of the input text.</param>
37+
/// <remarks>
38+
/// The constructor for this class (and all other Model classes) should not be used by library users. Ideally it
39+
/// would be marked <see langword="internal" />, but needs to be <see langword="public" /> for JSON deserialization.
40+
/// In future this function may have backwards-incompatible changes.
41+
/// </remarks>
42+
[JsonConstructor]
43+
public TextResult(string text, string detectedSourceLanguageCode)
44+
{
45+
Text = text;
46+
DetectedSourceLanguageCode = detectedSourceLanguageCode;
47+
}
48+
49+
/// <summary>The translated text.</summary>
50+
public string Text { get; }
51+
52+
/// <summary>The language code of the source text detected by DeepL.</summary>
53+
[JsonProperty("detected_source_language")]
54+
public string DetectedSourceLanguageCode { get; }
55+
56+
/// <summary>Returns the translated text.</summary>
57+
/// <returns>The translated text.</returns>
58+
public override string ToString() => Text;
59+
}
60+
}
61+
62+
public static async Task<ResultHolder<string>> TranslateAsync(string text,
63+
string fromLanguage,
64+
string toLanguage,
65+
string subscriptionKey,
66+
string region)
67+
{
68+
if (fromLanguage.Equals("auto"))
69+
{
70+
fromLanguage = "";
71+
}
72+
73+
var route = "/v2/translate";
74+
75+
try
76+
{
77+
using (var client = new HttpClient())
78+
using (var request = new HttpRequestMessage())
79+
{
80+
client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
81+
client.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/x-www-form-urlencoded");
82+
// Build the request.
83+
84+
var data = new Dictionary<string, string>
85+
{
86+
{ "auth_key", subscriptionKey },
87+
{ "text", text },
88+
{ "target_lang", toLanguage.ToUpper() }
89+
};
90+
91+
if (!string.IsNullOrEmpty(fromLanguage))
92+
data.Add("source_lang", fromLanguage.ToUpper());
93+
94+
var response = await client.PostAsync(
95+
(region == "0" ? DeepLFreeCognitiveServicesApiUrl : DeepLProCognitiveServicesApiUrl) + route,
96+
new FormUrlEncodedContent(data));
97+
response.EnsureSuccessStatusCode();
98+
99+
if (response.StatusCode == HttpStatusCode.OK)
100+
{
101+
// Read response as a string.
102+
var resultFromDeepL = await response.Content.ReadAsStringAsync();
103+
var deserializedOutput = JsonConvert.DeserializeObject<TextTranslateResult>(resultFromDeepL);
104+
105+
// Iterate over the results, return the first result
106+
foreach (var t in deserializedOutput.Translations)
107+
{
108+
return new ResultHolder<string>(true, t.Text);
109+
}
110+
}
111+
else
112+
{
113+
return new ResultHolder<string>(false, "Translation failed! Reason: " + response.ReasonPhrase);
114+
}
115+
}
116+
return new ResultHolder<string>(false);
117+
}
118+
catch (Exception e)
119+
{
120+
return new ResultHolder<string>(false, "Translation failed! Exception: " + e.Message);
121+
}
122+
}
123+
}
124+
}

AutoResxTranslator/Definitions/ServiceTypeEnum.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
public enum ServiceTypeEnum
44
{
55
Microsoft,
6-
Google
6+
Google,
7+
DeepL
78
}
89
}

AutoResxTranslator/Definitions/TranslationOptions.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,9 @@ public class TranslationOptions
1313
public string MsSubscriptionKey { get; set; }
1414

1515
public string MsSubscriptionRegion { get; set; }
16+
17+
public string DeepLSubscriptionKey { get; set; }
18+
19+
public string DeepLSubscriptionRegion { get; set; }
1620
}
1721
}

AutoResxTranslator/Properties/Settings.Designer.cs

Lines changed: 25 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

AutoResxTranslator/Properties/Settings.settings

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,11 @@
88
<Setting Name="MicrosoftTranslatorRegion" Type="System.String" Scope="User">
99
<Value Profile="(Default)">global</Value>
1010
</Setting>
11+
<Setting Name="DeepLTranslatorKey" Type="System.String" Scope="User">
12+
<Value Profile="(Default)">YOUR_KEY_HERE</Value>
13+
</Setting>
14+
<Setting Name="DeepLTranslatorType" Type="System.Int16" Scope="User">
15+
<Value Profile="(Default)">0</Value>
16+
</Setting>
1117
</Settings>
1218
</SettingsFile>

AutoResxTranslator/ResxExcel.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ public class ResxExcel
1515
public class ExcelFileInfo
1616
{
1717
public string[] SheetNames { get; set; }
18-
public string[] SheetColumns { get; set; }
18+
public string[] SheetColumnsKey { get; set; }
19+
public string[] SheetColumnsTranslation { get; set; }
1920
}
2021

2122
public static ExcelFileInfo ReadExcel(string excelFile)
@@ -68,7 +69,8 @@ public static ExcelFileInfo ReadExcel(string excelFile)
6869
columns.Add(column.ColumnName);
6970
}
7071
}
71-
result.SheetColumns = columns.ToArray();
72+
result.SheetColumnsKey = columns.ToArray();
73+
result.SheetColumnsTranslation = columns.ToArray();
7274
}
7375
}
7476

@@ -125,7 +127,7 @@ public static string[] GetExcelSheetNames(string excelFile)
125127
}
126128
return excelSheets;
127129
}
128-
catch (Exception ex)
130+
catch (Exception)
129131
{
130132
return null;
131133
}

AutoResxTranslator/app.config

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,18 @@
1010
</startup>
1111
<userSettings>
1212
<AutoResxTranslator.Properties.Settings>
13-
<setting name="MicrosoftTranslatorKey" serializeAs="String">
14-
<value>YOUR_KEY_HERE</value>
15-
</setting>
16-
<setting name="MicrosoftTranslatorRegion" serializeAs="String">
17-
<value>global</value>
18-
</setting>
19-
</AutoResxTranslator.Properties.Settings>
13+
<setting name="MicrosoftTranslatorKey" serializeAs="String">
14+
<value>YOUR_KEY_HERE</value>
15+
</setting>
16+
<setting name="MicrosoftTranslatorRegion" serializeAs="String">
17+
<value>global</value>
18+
</setting>
19+
<setting name="DeepLTranslatorKey" serializeAs="String">
20+
<value>YOUR_KEY_HERE</value>
21+
</setting>
22+
<setting name="DeepLTranslatorType" serializeAs="String">
23+
<value>0</value>
24+
</setting>
25+
</AutoResxTranslator.Properties.Settings>
2026
</userSettings>
2127
</configuration>

0 commit comments

Comments
 (0)