Merge pull request #344 from darxmorph/master
We don't need to download the translate output
This commit is contained in:
commit
80c9e4964b
@ -75,6 +75,7 @@ namespace NadekoBot.Modules.Translator.Helpers
|
|||||||
this.TranslationTime = TimeSpan.Zero;
|
this.TranslationTime = TimeSpan.Zero;
|
||||||
DateTime tmStart = DateTime.Now;
|
DateTime tmStart = DateTime.Now;
|
||||||
string translation = string.Empty;
|
string translation = string.Empty;
|
||||||
|
string text = string.Empty;
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
@ -83,67 +84,62 @@ namespace NadekoBot.Modules.Translator.Helpers
|
|||||||
GoogleTranslator.LanguageEnumToIdentifier(sourceLanguage),
|
GoogleTranslator.LanguageEnumToIdentifier(sourceLanguage),
|
||||||
GoogleTranslator.LanguageEnumToIdentifier(targetLanguage),
|
GoogleTranslator.LanguageEnumToIdentifier(targetLanguage),
|
||||||
HttpUtility.UrlEncode(sourceText));
|
HttpUtility.UrlEncode(sourceText));
|
||||||
string outputFile = Path.GetTempFileName();
|
|
||||||
using (WebClient wc = new WebClient())
|
using (WebClient wc = new WebClient())
|
||||||
{
|
{
|
||||||
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");
|
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");
|
||||||
wc.DownloadFile(url, outputFile);
|
text = wc.DownloadString(url);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get translated text
|
// Get translated text
|
||||||
if (File.Exists(outputFile))
|
// Get phrase collection
|
||||||
|
// string text = File.ReadAllText(outputFile);
|
||||||
|
int index = text.IndexOf(string.Format(",,\"{0}\"", GoogleTranslator.LanguageEnumToIdentifier(sourceLanguage)));
|
||||||
|
if (index == -1)
|
||||||
{
|
{
|
||||||
|
// Translation of single word
|
||||||
// Get phrase collection
|
int startQuote = text.IndexOf('\"');
|
||||||
string text = File.ReadAllText(outputFile);
|
if (startQuote != -1)
|
||||||
int index = text.IndexOf(string.Format(",,\"{0}\"", GoogleTranslator.LanguageEnumToIdentifier(sourceLanguage)));
|
|
||||||
if (index == -1)
|
|
||||||
{
|
{
|
||||||
// Translation of single word
|
int endQuote = text.IndexOf('\"', startQuote + 1);
|
||||||
int startQuote = text.IndexOf('\"');
|
if (endQuote != -1)
|
||||||
if (startQuote != -1)
|
|
||||||
{
|
{
|
||||||
int endQuote = text.IndexOf('\"', startQuote + 1);
|
translation = text.Substring(startQuote + 1, endQuote - startQuote - 1);
|
||||||
if (endQuote != -1)
|
|
||||||
{
|
|
||||||
translation = text.Substring(startQuote + 1, endQuote - startQuote - 1);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
|
||||||
// Translation of phrase
|
|
||||||
text = text.Substring(0, index);
|
|
||||||
text = text.Replace("],[", ",");
|
|
||||||
text = text.Replace("]", string.Empty);
|
|
||||||
text = text.Replace("[", string.Empty);
|
|
||||||
text = text.Replace("\",\"", "\"");
|
|
||||||
|
|
||||||
// Get translated phrases
|
|
||||||
string[] phrases = text.Split(new[] { '\"' }, StringSplitOptions.RemoveEmptyEntries);
|
|
||||||
for (int i = 0; (i < phrases.Count()); i += 2)
|
|
||||||
{
|
|
||||||
string translatedPhrase = phrases[i];
|
|
||||||
if (translatedPhrase.StartsWith(",,"))
|
|
||||||
{
|
|
||||||
i--;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
translation += translatedPhrase + " ";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Fix up translation
|
|
||||||
translation = translation.Trim();
|
|
||||||
translation = translation.Replace(" ?", "?");
|
|
||||||
translation = translation.Replace(" !", "!");
|
|
||||||
translation = translation.Replace(" ,", ",");
|
|
||||||
translation = translation.Replace(" .", ".");
|
|
||||||
translation = translation.Replace(" ;", ";");
|
|
||||||
|
|
||||||
// And translation speech URL
|
|
||||||
this.TranslationSpeechUrl = string.Format("https://translate.googleapis.com/translate_tts?ie=UTF-8&q={0}&tl={1}&total=1&idx=0&textlen={2}&client=gtx",
|
|
||||||
HttpUtility.UrlEncode(translation), GoogleTranslator.LanguageEnumToIdentifier(targetLanguage), translation.Length);
|
|
||||||
}
|
}
|
||||||
|
else {
|
||||||
|
// Translation of phrase
|
||||||
|
text = text.Substring(0, index);
|
||||||
|
text = text.Replace("],[", ",");
|
||||||
|
text = text.Replace("]", string.Empty);
|
||||||
|
text = text.Replace("[", string.Empty);
|
||||||
|
text = text.Replace("\",\"", "\"");
|
||||||
|
|
||||||
|
// Get translated phrases
|
||||||
|
string[] phrases = text.Split(new[] { '\"' }, StringSplitOptions.RemoveEmptyEntries);
|
||||||
|
for (int i = 0; (i < phrases.Count()); i += 2)
|
||||||
|
{
|
||||||
|
string translatedPhrase = phrases[i];
|
||||||
|
if (translatedPhrase.StartsWith(",,"))
|
||||||
|
{
|
||||||
|
i--;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
translation += translatedPhrase + " ";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fix up translation
|
||||||
|
translation = translation.Trim();
|
||||||
|
translation = translation.Replace(" ?", "?");
|
||||||
|
translation = translation.Replace(" !", "!");
|
||||||
|
translation = translation.Replace(" ,", ",");
|
||||||
|
translation = translation.Replace(" .", ".");
|
||||||
|
translation = translation.Replace(" ;", ";");
|
||||||
|
|
||||||
|
// And translation speech URL
|
||||||
|
this.TranslationSpeechUrl = string.Format("https://translate.googleapis.com/translate_tts?ie=UTF-8&q={0}&tl={1}&total=1&idx=0&textlen={2}&client=gtx",
|
||||||
|
HttpUtility.UrlEncode(translation), GoogleTranslator.LanguageEnumToIdentifier(targetLanguage), translation.Length);
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
|
Loading…
Reference in New Issue
Block a user