Example #1
0
        /// <summary>
        ///     调用API获取翻译结果并返回
        /// </summary>
        private void Translate()
        {
            // MessageBox.Show("!");
            var i       = comboBox1.SelectedIndex; //搜索引擎编号
            var j       = comboBox2.SelectedIndex; //源语言编号
            var k       = comboBox3.SelectedIndex; //目标语言编号
            var d       = richTextBox1.Text.ToCharArray();
            var newStr  = "";
            var newStr2 = "";

            foreach (var cr in d)                     //处理换行符以免JSON脚本出错
            {
                if (cr == (char)10 || cr == (char)13) //遍历字符 如过等于换行符或Enter则替换为空格+#+空格
                {
                    newStr += (char)32;               //空格键
                    newStr += (char)51;               //#号键
                    newStr += (char)32;               //空格键
                    continue;
                }
                newStr += cr.ToString(); //将处理后的字节加入到新的字符串内
            }
            state = 1;
            switch (i) //根据不同的编号选择搜索引擎
            {
            case 0:
                var translate1 = new Google();   //新建谷歌翻译对象
                var sd         = translate1.GoogleTranslate(newStr, TransFrom(j, i), TransTo(k, i))
                                 .ToCharArray(); //将翻译返回的结果转换为字符数组
                foreach (var cr in sd)           //遍历结果
                {
                    if (cr == (char)51)          //把结果中的#重新转换为换行符
                    {
                        //newStr2 += (char)32;
                        newStr2 += (char)13;     //Enter
                        continue;
                    }
                    newStr2 += cr.ToString();
                }
                C = newStr2;
                显示结果(newStr2);     //显示结果
                break;

            case 1:     //百度接口   _appid    _密钥
                var translate2 = new BaiduTranslator("20171014000088294", "mxwQ2q5FS78tLiWMSZTS");
                显示结果(C = translate2.Translate(richTextBox1.Text, TransFrom(j, i), TransTo(k, i)));
                break;
            }
            metroLabel2.Text = "提示:翻译成功";
            button1.Enabled  = true;
            button3.Enabled  = true;
        }
Example #2
0
        /// <summary>
        ///     将两个引擎的翻译结果同时显示
        /// </summary>
        private void TranslateAll()
        {
            var j       = comboBox2.SelectedIndex; //源语言编号
            var k       = comboBox3.SelectedIndex; //目标语言编号
            var d       = richTextBox1.Text.ToCharArray();
            var newStr  = "";
            var newStr2 = "";

            foreach (var cr in d)                     //处理换行符以免JSON脚本出错
            {
                if (cr == (char)10 || cr == (char)13) //遍历字符 如过等于换行符或Enter则替换为空格+#+空格
                {
                    newStr += (char)32;               //空格键
                    newStr += (char)51;               //#号键
                    newStr += (char)32;               //空格键
                    continue;
                }
                newStr += cr.ToString();                                                                       //将处理后的字节加入到新的字符串内
            }
            var translate1 = new Google();                                                                     //新建谷歌翻译对象
            var sd         = translate1.GoogleTranslate(newStr, TransFrom(j, 0), TransTo(k, 0)).ToCharArray(); //将翻译返回的结果转换为字符数组

            foreach (var cr in sd)                                                                             //遍历结果
            {
                if (cr == (char)51)                                                                            //把结果中的#重新转换为换行符
                {
                    //newStr2 += (char)32;
                    newStr2 += (char)13; //Enter
                    continue;
                }
                newStr2 += cr.ToString();
            }

            //百度接口   _appid    _密钥
            var translate2 = new BaiduTranslator("20171014000088294", "mxwQ2q5FS78tLiWMSZTS");
            var ds         = translate2.Translate(richTextBox1.Text, TransFrom(j, 1), TransTo(k, 1));

            A     = newStr2;
            B     = ds;
            state = 0;

            显示标题("谷歌翻译:\n");
            显示结果(newStr2); //显示结果
            显示标题("\n百度翻译:\n");
            显示结果(ds);      //显示结果
            metroLabel2.Text = "提示:翻译成功";
            button1.Enabled  = true;
            button3.Enabled  = true;
        }