/// <summary> /// Translate the text from <paramref name="from"/> to <paramref name="to"/>. /// </summary> /// <param name="text">The content to translate.</param> /// <param name="from">The language of the original text. You can set it as <c>Language.Unknown</c> to the auto detect it.</param> /// <param name="to">The target language you want to translate to.</param> /// <param name="format">The format of the text.</param> /// <returns>The translate result.</returns> /// <exception cref="TranslateException">Translate failed.</exception> /// <example> /// This is the c# code example. /// <code> /// string text = GetYourHtmlString(); /// string translated = Translator.Translate(text, Language.English, Language.French, TranslateFormat.html); /// </code> /// </example> public static string Translate(string text, Language from, Language to, TranslateFormat format) { if (from != Language.Unknown && !LanguageUtility.IsTranslatable(from)) { throw new TranslateException("Can not translate this language : " + from); } if (!LanguageUtility.IsTranslatable(to)) { throw new TranslateException(string.Format("Can not translate this language to \"{0}\"", to)); } TranslateData result; try { result = Translate(text, LanguageUtility.GetLanguageCode(from), LanguageUtility.GetLanguageCode(to), format); } catch (TranslateException ex) { throw new TranslateException("Translate failed!", ex); } if (format == TranslateFormat.text) { return(HttpUtility.HtmlDecode(result.TranslatedText)); } return(result.TranslatedText); }
internal static TranslateData Translate(string text, string from, string to, TranslateFormat format) { if (text == null) { throw new ArgumentNullException("text"); } if (from == null) { throw new ArgumentNullException("from"); } if (to == null) { throw new ArgumentNullException("to"); } TranslateRequest request = new TranslateRequest(text, from, to, format); WebRequest webRequest; if (Timeout != 0) { webRequest = request.GetWebRequest(Timeout); } else { webRequest = request.GetWebRequest(); } TranslateData responseData; try { responseData = RequestUtility.GetResponseData <TranslateData>(webRequest); } catch (GoogleAPIException ex) { throw new TranslateException(string.Format("request:\"{0}\"", request), ex); } return(responseData); }
/// <summary> /// Translate the text from <paramref name="from"/> to <paramref name="to"/>. /// </summary> /// <param name="text">The content to translate.</param> /// <param name="from">The language of the original text. You can set it as <c>Language.Unknown</c> to the auto detect it.</param> /// <param name="to">The target language you want to translate to.</param> /// <returns>The translate result.</returns> /// <exception cref="GoogleAPIException">Translate failed.</exception> /// <example> /// This is the c# code example. /// <code> /// string text = "Ξ�Ο²»¶Εά²½΅£"; /// TranslateClient client = new TranslateClient(/* Enter the URL of your site here */); /// string translated = client.Translate(text, Language.ChineseSimplified, Language.English); /// Console.WriteLine(translated); /// // I like running. /// </code> /// </example> public string Translate(string text, string from, string to) { return(this.Translate(text, from, to, TranslateFormat.GetDefault())); }
/// <summary> /// Begins an asynchronous request for translating the text to <paramref name="to"/> and auto detect which language the text is from. /// </summary> /// <param name="text">The content to translate.</param> /// <param name="to">The target language you want to translate to.</param> /// <param name="callback">The <see cref="AsyncCallback"/> delegate.</param> /// <param name="state">An object containing state information for this asynchronous request.</param> /// <returns>An <see cref="IAsyncResult"/> that references the asynchronous request.</returns> public IAsyncResult BeginTranslateAndDetect(string text, string to, AsyncCallback callback, object state) { return(this.BeginTranslateAndDetect(text, to, TranslateFormat.GetDefault(), callback, state)); }
/// <summary> /// Translate the text to <paramref name="to"/> and auto detect which language the text is from. /// </summary> /// <param name="text">The content to translate.</param> /// <param name="to">The target language you want to translate to.</param> /// <param name="from">The detected language of the original text.</param> /// <returns>The translate result.</returns> /// <exception cref="GoogleAPIException">Translate failed.</exception> /// <example> /// This is the c# code example. /// <code> /// string text = "Je t'aime."; /// string from; /// TranslateClient client = new TranslateClient(/* Enter the URL of your site here */); /// string translated = client.TranslateAndDetect(text, Language.English, out from); /// Language fromLanguage = from; /// Console.WriteLine("\"{0}\" is \"{1}\" in {2}", text, translated, fromLanguage); /// // "Je t'aime." is "I love you." in French. /// </code> /// </example> public string TranslateAndDetect(string text, string to, out string from) { return(this.TranslateAndDetect(text, to, TranslateFormat.GetDefault(), out from)); }