/// <summary> /// Converts a language to its identifier. /// </summary> /// <param name="language">The language."</param> /// <returns>The identifier or <see cref="string.Empty"/> if none.</returns> private static string LanguageEnumToIdentifier (string language) { string mode = string.Empty; GoogleTranslator.EnsureInitialized(); GoogleTranslator._languageModeMap.TryGetValue(language, out mode); return(mode); }
/// <summary> /// Translates the specified source text. /// </summary> /// <param name="sourceText">The source text.</param> /// <param name="sourceLanguage">The source language.</param> /// <param name="targetLanguage">The target language.</param> /// <returns>The translation.</returns> public override string Translate (string sourceText) { // Initialize this.Error = null; this.TranslationSpeechUrl = null; this.TranslationTime = TimeSpan.Zero; DateTime tmStart = DateTime.Now; string translation = string.Empty; try { // Download translation //string url = string.Format("https://translate.googleapis.com/translate_a/single?client=webclient&sl={0}&tl={1}&dt=t&q={2}", string url = string.Format("https://translate.google.com/translate_a/single?client=webapp&sl={0}&tl={1}&hl=ru&dt=at&dt=bd&dt=ex&dt=ld&dt=md&dt=qca&dt=rw&dt=rm&dt=ss&dt=t&otf=2&ssel=0&tsel=0&kc=3&tk=886167.724685&q={2}", GoogleTranslator.LanguageEnumToIdentifier(SourceLanguage), GoogleTranslator.LanguageEnumToIdentifier(TargetLanguage), HttpUtility.UrlEncode(sourceText)); string outputFile = Path.GetTempFileName(); 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.DownloadFile(url, outputFile); // DEBUG Console.WriteLine("GT: url string: " + url); //DEBUG Console.WriteLine("110:break"); //wc.DownloadFile(url, System.IO.Path.Combine(@"D:\Desktop", "responce")); } // Get translated text if (File.Exists(outputFile)) { // Get phrase collection string text = File.ReadAllText(outputFile); int index = text.IndexOf(string.Format(",,\"{0}\"", 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), TargetLanguage, translation.Length); } } catch (Exception ex) { this.Error = ex; } // Return result this.TranslationTime = DateTime.Now - tmStart; if (this.Error != null) { Console.WriteLine("There was an error in GT"); Console.WriteLine(Error.Message); Console.WriteLine(Error.StackTrace); } Console.WriteLine("GT: returning: " + translation); return(translation); }
public GoogleTranslator(string source, string target) { this.SourceLanguage = GoogleTranslator.LanguageEnumToIdentifier(source); this.TargetLanguage = GoogleTranslator.LanguageEnumToIdentifier(target); }