/// <summary> /// Translate a text /// </summary> /// <param name="sourceLanguage">source language</param> /// <param name="destLanguage">target language</param> /// <param name="text">requested text</param> /// <returns>A result object</returns> public string Translate(string sourceLanguage, string destLanguage, string text) { try { LocalTranslationCacheItem cacheItem = Cache.TryGetValue(sourceLanguage, destLanguage, text); if (null != cacheItem) { TranslateOperationResult result = new TranslateOperationResult(TranslateOperationState.Sucseed, text, cacheItem.TranslationText, null, true); RaiseOnTranslation(result); return cacheItem.TranslationText; } else { GoogleTranslator translator = new GoogleTranslator(sourceLanguage, destLanguage, text); translator.Translate(); TranslateOperationResult result = new TranslateOperationResult(TranslateOperationState.Sucseed, text, translator.Translation, null); Cache.Add(sourceLanguage, destLanguage, text, translator.Translation); RaiseOnTranslation(result); return translator.Translation; } } catch (Exception exception) { TranslateOperationResult result = new TranslateOperationResult(TranslateOperationState.Error, text, null, exception); RaiseOnTranslation(result); throw exception; } }
/// <summary> /// Translate a text /// </summary> /// <param name="sourceLanguage">source language</param> /// <param name="destLanguage">target language</param> /// <param name="text">requested text</param> /// <returns>A result object</returns> public string Translate(string sourceLanguage, string destLanguage, string text) { try { LocalTranslationCacheItem cacheItem = Cache.TryGetValue(sourceLanguage, destLanguage, text); if (null != cacheItem) { TranslateOperationResult result = new TranslateOperationResult(TranslateOperationState.Sucseed, text, cacheItem.TranslationText, null, true); RaiseOnTranslation(result); return(cacheItem.TranslationText); } else { GoogleTranslator translator = new GoogleTranslator(sourceLanguage, destLanguage, text); translator.Translate(); TranslateOperationResult result = new TranslateOperationResult(TranslateOperationState.Sucseed, text, translator.Translation, null); Cache.Add(sourceLanguage, destLanguage, text, translator.Translation); RaiseOnTranslation(result); return(translator.Translation); } } catch (Exception exception) { TranslateOperationResult result = new TranslateOperationResult(TranslateOperationState.Error, text, null, exception); RaiseOnTranslation(result); throw exception; } }
/// <summary> /// Retrieves the POST data (if any) to be sent to the url to be fetched. /// The data is returned as a string of the form "arg=val[&arg=val]...". /// </summary> /// <returns>A string containing the POST data or null if none.</returns> protected override string getPostData() { // Set translation mode string strPostData = string.Format("hl=en&ie=UTF8&oe=UTF8submit=Translate&langpair={0}|{1}", GoogleTranslator.LanguageEnumToIdentifier(this.SourceLanguage), GoogleTranslator.LanguageEnumToIdentifier(this.TargetLanguage)); // Set text to be translated strPostData += "&text=\"" + this.SourceText + "\""; return(strPostData); }