Fixed translate
This commit is contained in:
parent
29daf48e4f
commit
b30a804123
@ -2,6 +2,7 @@
|
||||
// License: Code Project Open License
|
||||
// http://www.codeproject.com/info/cpol10.aspx
|
||||
|
||||
using Newtonsoft.Json.Linq;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
@ -27,23 +28,6 @@ namespace NadekoBot.Modules.Translator.Helpers
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the time taken to perform the translation.
|
||||
/// </summary>
|
||||
public TimeSpan TranslationTime {
|
||||
get;
|
||||
private set;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the url used to speak the translation.
|
||||
/// </summary>
|
||||
/// <value>The url used to speak the translation.</value>
|
||||
public string TranslationSpeechUrl {
|
||||
get;
|
||||
private set;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the error.
|
||||
/// </summary>
|
||||
@ -69,15 +53,10 @@ namespace NadekoBot.Modules.Translator.Helpers
|
||||
string targetLanguage)
|
||||
{
|
||||
// Initialize
|
||||
this.Error = null;
|
||||
this.TranslationSpeechUrl = null;
|
||||
this.TranslationTime = TimeSpan.Zero;
|
||||
DateTime tmStart = DateTime.Now;
|
||||
string translation = string.Empty;
|
||||
string text = string.Empty;
|
||||
|
||||
try
|
||||
{
|
||||
|
||||
// Download translation
|
||||
string url = string.Format("https://translate.googleapis.com/translate_a/single?client=gtx&sl={0}&tl={1}&dt=t&q={2}",
|
||||
GoogleTranslator.LanguageEnumToIdentifier(sourceLanguage),
|
||||
@ -89,66 +68,7 @@ namespace NadekoBot.Modules.Translator.Helpers
|
||||
text = wc.DownloadString(url);
|
||||
}
|
||||
|
||||
// Get translated text
|
||||
// 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
|
||||
int startQuote = text.IndexOf('\"');
|
||||
if (startQuote != -1)
|
||||
{
|
||||
int endQuote = text.IndexOf('\"', 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);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
this.Error = ex;
|
||||
}
|
||||
|
||||
// Return result
|
||||
this.TranslationTime = DateTime.Now - tmStart;
|
||||
return translation;
|
||||
return JArray.Parse(text)[0][0][0].ToString();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
@ -27,13 +27,17 @@ namespace NadekoBot.Modules.Translator
|
||||
await e.Channel.SendIsTyping().ConfigureAwait(false);
|
||||
string from = e.GetArg("langs").ToLowerInvariant().Split('>')[0];
|
||||
string to = e.GetArg("langs").ToLowerInvariant().Split('>')[1];
|
||||
var text = e.GetArg("text")?.Trim();
|
||||
if (string.IsNullOrWhiteSpace(text))
|
||||
return;
|
||||
|
||||
string translation = t.Translate(e.GetArg("text"), from, to);
|
||||
string translation = t.Translate(text, from, to);
|
||||
await e.Channel.SendMessage(translation).ConfigureAwait(false);
|
||||
}
|
||||
catch
|
||||
catch (Exception ex)
|
||||
{
|
||||
await e.Channel.SendMessage("Bad input format, or sth went wrong...").ConfigureAwait(false);
|
||||
Console.WriteLine(ex);
|
||||
await e.Channel.SendMessage("Bad input format, or something went wrong...").ConfigureAwait(false);
|
||||
}
|
||||
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user