Work on gt

This commit is contained in:
Nikolaj Olsson 2018-11-30 15:43:46 +01:00
parent 08729c6944
commit 44379b02d7
3 changed files with 151 additions and 1 deletions

View File

@ -549,6 +549,7 @@
<Compile Include="TextDraw.cs" />
<Compile Include="TextEncodingExtensions.cs" />
<Compile Include="TimeCode.cs" />
<Compile Include="Translate\GoogleTranslator1.cs" />
<Compile Include="Translate\GoogleTranslator2.cs" />
<Compile Include="Translate\ITranslator.cs" />
<Compile Include="Translate\MicrosoftTranslator.cs" />

View File

@ -0,0 +1,84 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
namespace Nikse.SubtitleEdit.Core.Translate
{
/// <summary>
/// Google translate via Google Cloud API - see https://cloud.google.com/translate/
/// </summary>
public class GoogleTranslator1 : ITranslator
{
public List<TranslationPair> GetTranslationPairs()
{
throw new NotImplementedException();
}
public string GetName()
{
return "Google translate (old)";
}
public string GetUrl()
{
return "https://translate.google.com/";
}
public List<string> Translate(string sourceLanguage, string targetLanguage, List<string> texts, StringBuilder log)
{
string baseUrl = "https://translation.googleapis.com/language/translate/v2";
var format = "text";
var input = new StringBuilder();
foreach (var text in texts)
{
if (input.Length > 0)
input.Append("&");
input.Append("q=" + Utilities.UrlEncode(text));
}
string uri = $"{baseUrl}/?{input}&target={targetLanguage}&source={sourceLanguage}&format={format}&key={Configuration.Settings.Tools.GoogleApiV2Key}";
var request = WebRequest.Create(uri);
request.Proxy = Utilities.GetProxy();
request.ContentType = "application/json";
request.ContentLength = 0;
request.Method = "POST";
var response = request.GetResponse();
var reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
string content = reader.ReadToEnd();
var parser = new JsonParser();
var x = (Dictionary<string, object>)parser.Parse(content);
foreach (var k in x.Keys)
{
if (x[k] is Dictionary<string, object> v)
{
foreach (var innerKey in v.Keys)
{
if (v[innerKey] is List<object> l)
{
foreach (var o2 in l)
{
if (o2 is Dictionary<string, object> v2)
{
foreach (var innerKey2 in v2.Keys)
{
if (v2[innerKey2] is string translatedText)
{
translatedText = Regex.Unescape(translatedText);
translatedText = string.Join(Environment.NewLine, translatedText.SplitToLines());
// return PostTranslate(translatedText);
}
}
}
}
}
}
}
}
return null;
}
}
}

View File

@ -1,6 +1,10 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using Nikse.SubtitleEdit.Core.SubtitleFormats;
namespace Nikse.SubtitleEdit.Core.Translate
{
@ -26,7 +30,68 @@ namespace Nikse.SubtitleEdit.Core.Translate
public List<string> Translate(string sourceLanguage, string targetLanguage, List<string> texts, StringBuilder log)
{
throw new NotImplementedException();
string result;
using (var wc = new WebClient())
{
string url = $"https://translate.googleapis.com/translate_a/single?client=gtx&sl={sourceLanguage}&tl={targetLanguage}&dt=t&q={Utilities.UrlEncode("input")}";
wc.Proxy = Utilities.GetProxy();
wc.Encoding = Encoding.UTF8;
wc.Headers.Add("user-agent", "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36");
result = wc.DownloadString(url).Trim();
}
var sbAll = new StringBuilder();
int count = 0;
int i = 1;
int level = result.StartsWith('[') ? 1 : 0;
while (i < result.Length - 1)
{
var sb = new StringBuilder();
var start = false;
for (; i < result.Length - 1; i++)
{
var c = result[i];
if (start)
{
if (c == '"' && result[i - 1] != '\\')
{
count++;
if (count % 2 == 1 && level > 2) // even numbers are original text, level > 3 is translation
sbAll.Append(" " + sb);
i++;
break;
}
sb.Append(c);
}
else if (c == '"')
{
start = true;
}
else if (c == '[')
{
level++;
}
else if (c == ']')
{
level--;
}
}
}
result = sbAll.ToString().Trim();
result = Regex.Unescape(result);
result = Json.DecodeJsonText(result);
string res = result;
res = string.Join(Environment.NewLine, res.SplitToLines());
res = res.Replace("NewlineString", Environment.NewLine);
res = res.Replace(Environment.NewLine + Environment.NewLine, Environment.NewLine);
res = res.Replace(Environment.NewLine + " ", Environment.NewLine);
res = res.Replace(Environment.NewLine + " ", Environment.NewLine);
res = res.Replace(" " + Environment.NewLine, Environment.NewLine);
res = res.Replace(" " + Environment.NewLine, Environment.NewLine).Trim();
return null;
}
}
}