generated from karashiiro/DalamudPluginProjectTemplate
-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
GoogleTranslator.cs
129 lines (102 loc) · 4.03 KB
/
GoogleTranslator.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// <copyright file="GoogleTranslator.cs" company="lokinmodar">
// Copyright (c) lokinmodar. All rights reserved.
// Licensed under the Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International Public License license.
// </copyright>
using System;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Web;
using Dalamud.Plugin.Services;
namespace Echoglossian
{
public class GoogleTranslator : ITranslator
{
private readonly IPluginLog pluginLog;
private readonly HttpClient httpClient;
private const string NewGTranslateUrl = "https://translate.google.com/m";
private const string UaString = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.104 Safari/537.36";
public GoogleTranslator(IPluginLog pluginLog)
{
this.pluginLog = pluginLog;
this.httpClient = new HttpClient();
this.httpClient.DefaultRequestHeaders.Add("User-Agent", UaString);
}
string ITranslator.Translate(string text, string sourceLanguage, string targetLanguage)
{
this.pluginLog.Debug("inside GoogleTranslator translate method");
try
{
string parsedText = text;
parsedText = parsedText.Replace("\u200B", string.Empty);
string url = $"{NewGTranslateUrl}?hl=en&sl={sourceLanguage}&tl={targetLanguage}&q={Uri.EscapeDataString(parsedText)}";
this.pluginLog.Debug($"URL: {url}");
var requestResult = this.httpClient.GetStreamAsync(url).Result;
StreamReader reader = new StreamReader(requestResult ?? throw new Exception());
return this.FormatStreamReader(reader.ReadToEnd());
}
catch (Exception e)
{
this.pluginLog.Warning(e.ToString());
throw;
}
}
async Task<string> ITranslator.TranslateAsync(string text, string sourceLanguage, string targetLanguage)
{
this.pluginLog.Debug("inside GoogleTranslator translateAsync method");
try
{
string parsedText = text;
parsedText = parsedText.Replace("\u200B", string.Empty);
string url = $"{NewGTranslateUrl}?hl=en&sl={sourceLanguage}&tl={targetLanguage}&q={Uri.EscapeDataString(parsedText)}";
this.pluginLog.Debug($"URL: {url}");
var requestResult = await this.httpClient.GetStreamAsync(url);
StreamReader reader = new StreamReader(requestResult ?? throw new Exception());
return this.FormatStreamReader(reader.ReadToEnd());
}
catch (Exception e)
{
this.pluginLog.Warning(e.ToString());
throw;
}
}
private string FormatStreamReader(string read)
{
string finalText;
if (read.StartsWith("[\""))
{
char[] start = { '[', '\"' };
char[] end = { '\"', ']' };
var dialogueText = read.TrimStart(start);
finalText = dialogueText.TrimEnd(end);
}
else
{
finalText = this.ParseHtml(read);
}
finalText = finalText.Replace("\u200B", string.Empty);
finalText = finalText.Replace("\u005C\u0022", "\u0022");
finalText = finalText.Replace("\u005C\u002F", "\u002F");
finalText = finalText.Replace("\\u003C", "<");
finalText = finalText.Replace("'", "\u0027");
finalText = Regex.Replace(finalText, @"(?<=.)(─)(?=.)", " \u2015 ");
this.pluginLog.Debug($"FinalTranslatedText: {finalText}");
return finalText;
}
private string ParseHtml(string html)
{
using StringWriter stringWriter = new StringWriter();
HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(html);
var text = doc.DocumentNode.Descendants()
.Where(n => n.HasClass("result-container")).ToList();
var parsedText = text.Single(n => n.InnerText.Length > 0).InnerText;
HttpUtility.HtmlDecode(parsedText, stringWriter);
string decodedString = stringWriter.ToString();
this.pluginLog.Debug($"In parser: " + parsedText);
return decodedString;
}
}
}