Пример #1
0
        /// <summary>
        /// 随机翻译多次到中文
        /// </summary>
        /// <param name="source"></param>
        /// <param name="count">最多就20次</param>
        /// <returns></returns>
        public string RandomTranslateToZh(string source, int count)
        {
            if (source.IsNullOrWhiteSpace())
            {
                return(source);
            }
            if (count <= 0)
            {
                count = 1;
            }
            else if (count >= 20)
            {
                count = 20;
            }
            BaiduTranslateAPI translator = new BaiduTranslateAPI();

            for (int i = 0; i < count; i++)
            {
                source = translator.Translate(source, Language.auto, RandomTool.EnumRandomGetOne <Language>());
                if (source == null)
                {
                    this.InheritError(translator);
                    return(null);
                }
            }
            string result = translator.Translate(source, Language.auto);//最后转为中文

            if (result == null)
            {
                this.InheritError(translator);
                return(null);
            }
            return(result);
        }
Пример #2
0
        public override object Default(string str = null)
        {
            if (str.IsNullOrWhiteSpace())
            {
                return(null);
            }
            Language fromLanguage = Language.auto, toLanguage = Language.zh;
            bool     translateFlag = false; //指示是否需要翻译语言

            if (!string.IsNullOrWhiteSpace(From))
            {
                if (Enum.TryParse(From, out fromLanguage) ||
                    TryParseZhNameToLanguage(From, out fromLanguage))
                {
                    translateFlag = true;
                }
            }
            if (!string.IsNullOrWhiteSpace(To))
            {
                if (Enum.TryParse(To, out toLanguage) || TryParseZhNameToLanguage(To, out toLanguage)
                    ) //测试是否是Language内的
                {
                    translateFlag = true;
                }
                else //其他功能
                {
                    To = To.ToLower();
                    switch (To)
                    {
                    case "jianti":
                    case "简体":
                        str = ToSimplified(str);
                        break;

                    case "fanti":
                    case "繁体":
                        str = ToTraditional(str);
                        break;

                    case "banjiao":
                    case "半角":
                        str = ToHalfWidth(str);
                        break;

                    case "quanjiao":
                    case "全角":
                        str = ToFullWidth(str);
                        break;

                    case "b64":
                    case "base64":
                        str = ToBase64(str);
                        break;

                    case "md5":
                        str = ToMD5(str);
                        break;

                    case "时间戳":
                        str = ToUnixTime(str);
                        break;

                    default:
                        str = "to参数支持翻译API中的Language类(使用翻译的“支持语种”功能查看)以及其他功能:繁体、简体、全角、半角、base64、md5、时间戳";
                        break;
                    }
                }
            }

            if (translateFlag) //若是调用了翻译API
            {
                BaiduTranslateAPI translator = new BaiduTranslateAPI();
                str = translator.Translate(str, fromLanguage, toLanguage);
                if (str == null)
                {
                    this.InheritError(translator);
                    return(null);
                }
            }

            return(ResultPipe(str));
        }