Exemple #1
0
        private void Translate(string orig)
        {
            var OrigText = ReplaceText(orig);

            _trans.TransTextCallbackFunc = transText =>
            {
                foreach (var rep in _reps)
                {
                    transText = transText.Replace(rep.Temp, rep.After);
                }

                return(transText);
            };
            _trans.OrigTextCallbackFunc = origText =>
            {
                foreach (var rep in _reps)
                {
                    origText = origText.Replace(rep.Temp, rep.Before);
                }

                return(origText);
            };
            _trans.DownloadCallbackFunc = tt =>
            {
                AfterBox.Text = tt.GetTransText();
                _tt           = tt;
                // 履歴に追加
                HistoryCmbBox.Items.Insert(0, tt);
                // 直近30個のみ保存し、それ以上は削除
                if (HistoryCmbBox.Items.Count >= 30)
                {
                    HistoryCmbBox.Items.RemoveAt(HistoryCmbBox.Items.Count - 1);
                }
                return(AfterBox.Text);
            };
            var fromLang = ((string)FromComboBox.SelectedItem).Split(',')[0];
            var toLang   = ((string)ToComboBox.SelectedItem).Split(',')[0];

            StopDrawingFunc(AfterBox, afb =>
            {
                ClearSelectionBackColor(afb, _afterBoxDefaultColor, false);
                afb.ScrollToCaret();
                return(true);
            });
            _trans.Translate(OrigText, fromLang, toLang);
        }
Exemple #2
0
        private void HistoryCmbBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            // 指定した翻訳文をコピー

            var isAF = isAutoFormat.Checked;
            var isAT = isAutoTranslation.Checked;

            isAutoFormat.Checked      = false; // 内容を壊さないように一時的に切る
            isAutoTranslation.Checked = false;
            var x = (TranslationText)HistoryCmbBox.SelectedItem;

            _tt                       = x;
            BeforeBox.Text            = _tt.GetOrigText();
            AfterBox.Text             = _tt.GetTransText();
            isAutoFormat.Checked      = isAF;
            isAutoTranslation.Checked = isAT;
        }
        public void CompleteDownloadProc1(Object sender, DownloadStringCompletedEventArgs e)
        {
            GTransResp res;
            var        serializer =
                new DataContractJsonSerializer(typeof(GTransResp));

            using (var ms = new MemoryStream(Encoding.UTF8.GetBytes(e.Result)))
            {
                res = (GTransResp)serializer.ReadObject(ms);
            }

            var transText = "";
            var ptList    = new List <PTrans>();
            var transPos  = 0;
            var origPos   = 0;
            var origText  = "";

            if (res?.sentences == null)
            {
                return;
            }
            foreach (var sentence in res.sentences)
            {
                var oText = sentence.orig;
                var tText = sentence.trans;
                oText = OrigTextCallbackFunc == null ? oText : OrigTextCallbackFunc(oText);
                tText = TransTextCallbackFunc == null ? tText : TransTextCallbackFunc(tText);
                if (oText == null || tText == null)
                {
                    break;
                }
                origText  += oText;
                transText += tText;
                ptList.Add(new PTrans(origPos, transPos, oText, tText));
                origPos  += oText.Length * 2 - oText.TrimStart().Length; // 先頭空白による位置ずれ調整
                transPos += tText.Length;
            }

            _tt = new TranslationText(origText, transText, ptList);

            DownloadCallbackFunc?.Invoke(_tt);
        }