示例#1
0
        private void SearchButton_Click(object sender, RoutedEventArgs e)
        {
            if (this.wordRepository == null)
            {
                var seletedOne = this.DropDownList.SelectedValue as LanguageDomain;
                this.TableName      = mappingDic[seletedOne.Name];
                this.wordRepository = new WordRepository(this.TableName);
            }
            var textBox = this.Word as TextBox;

            if (textBox != null && !string.IsNullOrEmpty(textBox.Text))
            {
                this.TranslatedInformation.Content = string.Empty;
                if (!this.CheckIfInvalidInput(textBox.Text))
                {
                    TranslationResult result        = null;
                    JinShanDomain     jinshanDomain = null;
                    if (!textBox.Text.Any(p => !Regex.IsMatch(p.ToString(), ChineseRule)))
                    {
                        //result = TranslationMethod.TranslatedWithBaidu(textBox.Text, LanguageEnum.ZH, LanguageEnum.EN);
                        jinshanDomain = JinShanUtil.Translate(textBox.Text);
                    }
                    else if (!textBox.Text.Any(p => !Regex.IsMatch(p.ToString(), JapaneseRule)))
                    {
                        result = TranslationMethod.TranslatedWithBaidu(textBox.Text, LanguageEnum.JP, LanguageEnum.ZH);
                    }
                    else
                    {
                        result = TranslationMethod.TranslatedWithBaidu(textBox.Text, LanguageEnum.Auto, LanguageEnum.ZH);
                    }
                    //|| (!string.IsNullOrEmpty(result.Error_code) && result.Error_code != "52000")
                    if (result == null && jinshanDomain == null)
                    {
                        this.TranslatedInformation.Content    = string.Format("Error code: {0}, {1}", result.Error_code, result.Error_msg);
                        this.TranslatedInformation.Foreground = Brushes.Red;
                    }
                    else
                    {
                        if (jinshanDomain != null)
                        {
                            var wordInfo = new WordDomain
                            {
                                Id         = Guid.NewGuid(),
                                Word       = jinshanDomain.word_name,
                                Flag       = 0,
                                CreateTime = DateTime.Now.Ticks
                            };
                            var defaultOne = jinshanDomain.symbols.FirstOrDefault();
                            wordInfo.Phonetic = defaultOne.ph_en;
                            wordInfo.Voice    = this.ConvertVoice(defaultOne);
                            //var translationStr = string.Empty;
                            //foreach (var part in defaultOne.parts)
                            //{
                            //    var meanStr = string.Empty;
                            //    foreach (var mean in part.means)
                            //    {
                            //        meanStr += mean;
                            //        meanStr += ";";
                            //    }
                            //    translationStr += "{";
                            //    translationStr += part.part;
                            //    translationStr += ":";
                            //    translationStr += meanStr;
                            //    translationStr += "}";
                            //}
                            wordInfo.Trascation = defaultOne.parts.FirstOrDefault().means[0];
                            this.wordRepository = new WordRepository("Language_Custom");
                            this.wordRepository.InsertAll(new List <WordDomain> {
                                wordInfo
                            }, !string.IsNullOrEmpty(wordInfo.Voice));
                            // 删除缓存中最旧的data
                            this.UpdateHistoryResource(wordInfo.Word, string.Join(",", defaultOne.parts.FirstOrDefault().means.FirstOrDefault()));
                        }
                        else
                        {
                            var contentStr = string.Empty;
                            for (var i = 0; i < result.Trans_result.Length; i++)
                            {
                                if (i > 0)
                                {
                                    contentStr += "/r/n";
                                }
                                var line = string.Format("{0}:  {1}", result.Trans_result[i].Src, result.Trans_result[i].Dst);
                                contentStr += line;
                                // 添加到OtherResource
                                //ResourceAction.RemoveAndAddNewResEntry(historyResourceUrl, "Key_" + result.Trans_result[i].Src, result.Trans_result[i].Dst);
                                var wordInfo = new WordDomain()
                                {
                                    Id         = Guid.NewGuid(),
                                    Word       = result.Trans_result[i].Src,
                                    Trascation = result.Trans_result[i].Dst,
                                    Phonetic   = result.Trans_result[i].Src,
                                    Flag       = 0,
                                    CreateTime = DateTime.Now.Ticks
                                };
                                this.wordRepository.InsertAll(new List <WordDomain> {
                                    wordInfo
                                });

                                // 删除缓存中最旧的data
                                this.UpdateHistoryResource(result.Trans_result[i].Src, result.Trans_result[i].Dst);
                            }
                            this.TranslatedInformation.Content = contentStr;
                        }

                        this.TranslatedInformation.Foreground = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#999933"));
                        this.InitHistoryContent();
                    }
                }
                else
                {
                    this.TranslatedInformation.Content    = string.Format("Error: invalid word, please input again.");
                    this.TranslatedInformation.Foreground = Brushes.Red;
                    this.Word.Focus();
                }
            }
        }