コード例 #1
0
        /// <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);
        }
コード例 #2
0
        /// <summary>
        /// Detect the language for this text.
        /// </summary>
        /// <param name="text">The text you want to test.</param>
        /// <param name="isReliable">Whether the result is reliable</param>
        /// <param name="confidence">The confidence percent of the result.</param>
        /// <returns>The detected language.</returns>
        /// <exception cref="TranslateException">Detect failed.</exception>
        public static Language Detect(string text, out bool isReliable, out double confidence)
        {
            DetectData result;

            try
            {
                result = Detect(text);
            }
            catch (TranslateException ex)
            {
                throw new TranslateException("Detect failed!", ex);
            }
            string languageCode = result.LanguageCode;

            isReliable = result.IsReliable;
            confidence = result.Confidence;
            Language language = LanguageUtility.GetLanguage(languageCode);

            return(language);
        }