TranslateAsync() public method

Microsoft Translator API を使用した翻訳を非同期に行います
public TranslateAsync ( string text, string langFrom, string langTo ) : Task
text string
langFrom string
langTo string
return Task
Exemplo n.º 1
0
        public async Task TranslateAsync_Test()
        {
            var handler = new HttpMessageHandlerMock();
            var bing = new Bing(new HttpClient(handler));

            handler.Enqueue(x =>
            {
                Assert.Equal(HttpMethod.Get, x.Method);
                Assert.Equal("https://api.datamarket.azure.com/Data.ashx/Bing/MicrosoftTranslator/v1/Translate",
                    x.RequestUri.GetLeftPart(UriPartial.Path));

                var query = HttpUtility.ParseQueryString(x.RequestUri.Query);

                Assert.Equal("'hogehoge'", query["Text"]);
                Assert.Equal("'ja'", query["To"]);
                Assert.Equal("Raw", query["$format"]);

                return new HttpResponseMessage(HttpStatusCode.OK)
                {
                    Content = new StringContent("<string>ほげほげ</string>"),
                };
            });

            var translatedText = await bing.TranslateAsync("hogehoge", langFrom: null, langTo: "ja");
            Assert.Equal("ほげほげ", translatedText);

            Assert.Equal(0, handler.QueueCount);
        }
Exemplo n.º 2
0
        public async Task TranslateAsync_Test()
        {
            var handler = new HttpMessageHandlerMock();
            var bing    = new Bing(new HttpClient(handler));

            handler.Enqueue(x =>
            {
                Assert.Equal(HttpMethod.Get, x.Method);
                Assert.Equal("https://api.datamarket.azure.com/Data.ashx/Bing/MicrosoftTranslator/v1/Translate",
                             x.RequestUri.GetLeftPart(UriPartial.Path));

                var query = HttpUtility.ParseQueryString(x.RequestUri.Query);

                Assert.Equal("'hogehoge'", query["Text"]);
                Assert.Equal("'ja'", query["To"]);
                Assert.Equal("Raw", query["$format"]);

                return(new HttpResponseMessage(HttpStatusCode.OK)
                {
                    Content = new StringContent("<string>ほげほげ</string>"),
                });
            });

            var translatedText = await bing.TranslateAsync("hogehoge", langFrom : null, langTo : "ja");

            Assert.Equal("ほげほげ", translatedText);

            Assert.Equal(0, handler.QueueCount);
        }
Exemplo n.º 3
0
        public async Task TranslateAsync_HttpErrorTest()
        {
            var handler = new HttpMessageHandlerMock();
            var bing = new Bing(new HttpClient(handler));

            handler.Enqueue(x =>
            {
                return new HttpResponseMessage(HttpStatusCode.ServiceUnavailable);
            });

            await Assert.ThrowsAsync<HttpRequestException>(async () =>
                await bing.TranslateAsync("hogehoge", langFrom: null, langTo: "ja"));

            Assert.Equal(0, handler.QueueCount);
        }
Exemplo n.º 4
0
        public async Task TranslateAsync_HttpErrorTest()
        {
            var handler = new HttpMessageHandlerMock();
            var bing    = new Bing(new HttpClient(handler));

            handler.Enqueue(x =>
            {
                return(new HttpResponseMessage(HttpStatusCode.ServiceUnavailable));
            });

            await TestUtils.ThrowsAsync <HttpRequestException>(async() =>
                                                               await bing.TranslateAsync("hogehoge", langFrom: null, langTo: "ja"));

            Assert.Equal(0, handler.QueueCount);
        }
Exemplo n.º 5
0
        private async Task doTranslation(string str)
        {
            if (string.IsNullOrEmpty(str))
                return;

            var bing = new Bing();
            try
            {
                var translatedText = await bing.TranslateAsync(str,
                    langFrom: null,
                    langTo: this._cfgCommon.TranslateLanguage);

                this.PostBrowser.DocumentText = this.createDetailHtml(translatedText);
            }
            catch (HttpRequestException e)
            {
                this.StatusLabel.Text = "Err:" + e.Message;
            }
        }
Exemplo n.º 6
0
        private async Task DoTranslation(string str)
        {
            if (string.IsNullOrEmpty(str))
                return;

            var bing = new Bing();
            try
            {
                var translatedText = await bing.TranslateAsync(str,
                    langFrom: null,
                    langTo: SettingCommon.Instance.TranslateLanguage);

                this.PostBrowser.DocumentText = this.Owner.createDetailHtml(translatedText);
            }
            catch (HttpRequestException e)
            {
                this.RaiseStatusChanged("Err:" + e.Message);
            }
            catch (OperationCanceledException)
            {
                this.RaiseStatusChanged("Err:Timeout");
            }
        }
Exemplo n.º 7
0
        private async Task doTranslation(string str)
        {
            if (string.IsNullOrEmpty(str))
                return;

            var bing = new Bing(this.http);
            try
            {
                var translatedText = await bing.TranslateAsync(str,
                    langFrom: null,
                    langTo: this.SettingDialog.TranslateLanguage);

                this.PostBrowser.DocumentText = translatedText;
            }
            catch (HttpRequestException e)
            {
                this.StatusLabel.Text = "Err:" + e.Message;
            }
        }