Fork me on GitHub

一个翻译小程序(带一点点抒情)

     博客园里有几篇关于父亲节的文章?什么狗屁的父亲节母亲节,为什么不去想想让父母天天像过节一样,为什么只有到了外国人的父亲节,母亲节才想起有个爸妈,除此在外boss比爹还亲呢。

     很多人把时间都用在了自己的所谓的事业上,但是又给家庭成员带来了什么呢,也许你会常常看见,有人写本书写文章,说是献给亲爱的父母,老婆,孩子。但是父母,老婆,孩子知道程序是个锤子,人生没有"乌云"和"闪电"的时间也就短短几年的时间,程序没了可以重写,书没了,等老了经验丰富了,无牵无挂了可以慢慢写点此生最高水准的,可是亲人没了,就再也没了,严重的自我安慰其实损失最大的还是亲人,搞不定那天就为写程序写书过劳死了,提前挂点了,何必了,以后写书,就改写为了自己,别浪费亲人感情了。

    总之,写这些,不是为了发牢骚,而是告诉大家一个道理,这个世界除了父母谁都可能会嫌弃你背叛你,父母才是世界上最可爱的人,用心去爱,不要想起什么狗屁的节日才去问候一下,不要为世界做点可有可无的所谓的贡献的时候扯上父母。

    好了,不闲扯了,继续切入正题,今天女朋友让我给写一个翻译器,于是就花了半个小时过些弄个简单的,给她用,也就适合新手看看,简单的程序,也基本没什么注释,基本看下就明白了。

效果如下:

直接就贴代码了:

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Windows.Forms;
  4 using System.Speech.Synthesis;
  5 using System.Threading;
  6 using System.Configuration;
  7 using System.IO;
  8 
  9 namespace 老婆的翻译器
 10 {
 11     /// <summary>
 12     /// Create By Rohelm.X 
 13     ///http://www.cnblogs.com/rohelm
 14     /// 2012.6.17
 15     /// </summary>
 16     public partial class Form1 : Form
 17     {
 18         Translator.LanguageServiceClient client = new Translator.LanguageServiceClient();
 19         private string[] languagecodes = null;
 20         private string[] languagenames = null;
 21         private static readonly string appid = ConfigurationManager.AppSettings["appid"];
 22         private delegate void AsyncFunction(string text);
 23         private AsyncFunction _asyncFunction = null;
 24         SpeechSynthesizer _synthesizer = null;
 25 
 26         public Form1()
 27         {
 28             InitializeComponent();
 29         }
 30        
 31         private void Form1_Load(object sender, EventArgs e)
 32         {
 33             cbb_slan.DataSource = this.GetAllLanguageCode();
 34             cbb_slan.SelectedIndex = 3;
 35             cbb_tarlan.DataSource = this.GetAllLanguageCode();
 36             cbb_tarlan.SelectedIndex = 8;
 37         }
 38 
 39         private void bt_transfram_Click(object sender, EventArgs e)
 40         {
 41             rtb_target.Text = client.Translate(appid, rtb_soruce.Text, ((LanguageCode)cbb_slan.SelectedItem).Code, ((LanguageCode)cbb_tarlan.SelectedItem).Code, "text/html", "general");
 42         }
 43         private void btn_auto_Click(object sender, EventArgs e)
 44         {
 45             if (this.GetNameByCode(rtb_soruce.Text) != "No")
 46             {
 47                 lb_disaply.Text = "亲爱的老婆,当前软件检测到的需要翻译的语言为" + this.GetNameByCode(rtb_soruce.Text) + "并已正常翻译。";
 48                 rtb_target.Text = client.Translate(appid, rtb_soruce.Text, client.Detect(appid, rtb_soruce.Text), ((LanguageCode)cbb_tarlan.SelectedItem).Code, "text/html", "general");
 49             }
 50             else
 51             {
 52                 lb_disaply.Text = "老婆你可能输入有误!";
 53             }
 54         }
 55         //  获取语言代码对应的名称
 56         public string GetNameByCode(string socrcetext)
 57         {
 58             string languagecode= client.Detect(appid,socrcetext);
 59             for (int i = 0; i < languagecodes.Length; i++)
 60             {
 61                 if (languagecode == languagecodes[i])
 62                 { 
 63                     return languagenames[i];
 64                 }
 65             }
 66             return "No";
 67         }
 68         //写了一个方法将有的代码和语言名字的集合
 69         public IEnumerable<LanguageCode> GetAllLanguageCode()
 70         {
 71             List<LanguageCode> list = new List<LanguageCode>();
 72             languagecodes = client.GetLanguagesForTranslate(appid);
 73             languagenames = client.GetLanguageNames(appid, "zh-cn", languagecodes);
 74             for (int i = 0; i < languagecodes.Length; i++)
 75             {
 76                 LanguageCode lan = new LanguageCode();
 77                 lan.Name = languagenames[i];
 78                 lan.Code = languagecodes[i];
 79                 list.Add(lan);
 80             }
 81             return list;
 82         }
 83       
 84         private void pb_speakSource_Click(object sender, EventArgs e)
 85         {
 86             Speak(rtb_soruce.Text);
 87         }
 88 
 89         private void Speak(string textString)
 90         {
 91             _asyncFunction = new AsyncFunction(AsyncSpeak);
 92             _asyncFunction.BeginInvoke(textString, null, null);
 93         }
 94 
 95         private void AsyncSpeak(string textString)
 96         {
 97             try
 98             {
 99                 if (_synthesizer == null)
100                     _synthesizer = new SpeechSynthesizer();
101 
102                 _synthesizer.SpeakAsyncCancelAll();
103                 _synthesizer.Speak(textString);
104             }
105             catch (Exception ex)
106             {
107                 using (TextWriter sw = new StreamWriter("Log.txt", true))
108                 {
109                     sw.WriteLine(ex.Message);
110                 }
111             }
112         }
113 
114         private void pb_speaktarget_Click(object sender, EventArgs e)
115         {
116             Speak(rtb_target.Text);
117         }
118     }
119 
120     public class LanguageCode
121     {
122         public string Name { get; set; }
123         public string Code { get; set; }
124     }
125 }
posted @ 2012-06-17 23:51  Halower  阅读(2866)  评论(27编辑  收藏  举报