コード例 #1
0
        public async Task GetSupportedLanguagesAsync()
        {
            Mock <TranslationService.TranslationServiceClient> mockGrpcClient = new Mock <TranslationService.TranslationServiceClient>(MockBehavior.Strict);

            mockGrpcClient.Setup(x => x.CreateOperationsClient())
            .Returns(new Mock <Operations.OperationsClient>().Object);
            GetSupportedLanguagesRequest expectedRequest = new GetSupportedLanguagesRequest
            {
                ParentAsLocationName = new LocationName("[PROJECT]", "[LOCATION]"),
                DisplayLanguageCode  = "displayLanguageCode30710199",
                Model = "model104069929",
            };
            SupportedLanguages expectedResponse = new SupportedLanguages();

            mockGrpcClient.Setup(x => x.GetSupportedLanguagesAsync(expectedRequest, It.IsAny <CallOptions>()))
            .Returns(new Grpc.Core.AsyncUnaryCall <SupportedLanguages>(Task.FromResult(expectedResponse), null, null, null, null));
            TranslationServiceClient client        = new TranslationServiceClientImpl(mockGrpcClient.Object, null);
            LocationName             parent        = new LocationName("[PROJECT]", "[LOCATION]");
            string             displayLanguageCode = "displayLanguageCode30710199";
            string             model    = "model104069929";
            SupportedLanguages response = await client.GetSupportedLanguagesAsync(parent, displayLanguageCode, model);

            Assert.Same(expectedResponse, response);
            mockGrpcClient.VerifyAll();
        }
コード例 #2
0
        public void SetGoogleAvailableLanguages()
        {
            try
            {
                var request = new GetSupportedLanguagesRequest
                {
                    ParentAsLocationName = new LocationName(_options.ProjectName, "global"),
                };
                var response = _translationServiceClient.GetSupportedLanguages(request);

                foreach (var language in response.Languages)
                {
                    var languageModel = new GoogleV3LanguageModel
                    {
                        GoogleLanguageCode = language.LanguageCode,
                        SupportSource      = language.SupportSource,
                        SupportTarget      = language.SupportTarget,
                        CultureInfo        = new CultureInfo(language.LanguageCode)
                    };
                    SupportedLanguages.Add(languageModel);
                }
            }
            catch (Exception e)
            {
                _logger.Error($"{MethodBase.GetCurrentMethod().Name}: {e}");
            }
        }
コード例 #3
0
        public void GetSupportedLanguages()
        {
            Mock <TranslationService.TranslationServiceClient> mockGrpcClient = new Mock <TranslationService.TranslationServiceClient>(MockBehavior.Strict);

            mockGrpcClient.Setup(x => x.CreateOperationsClient())
            .Returns(new Mock <Operations.OperationsClient>().Object);
            GetSupportedLanguagesRequest expectedRequest = new GetSupportedLanguagesRequest
            {
                Parent = new LocationName("[PROJECT]", "[LOCATION]").ToString(),
                DisplayLanguageCode = "displayLanguageCode30710199",
                Model = "model104069929",
            };
            SupportedLanguages expectedResponse = new SupportedLanguages();

            mockGrpcClient.Setup(x => x.GetSupportedLanguages(expectedRequest, It.IsAny <CallOptions>()))
            .Returns(expectedResponse);
            TranslationServiceClient client        = new TranslationServiceClientImpl(mockGrpcClient.Object, null);
            string             formattedParent     = new LocationName("[PROJECT]", "[LOCATION]").ToString();
            string             displayLanguageCode = "displayLanguageCode30710199";
            string             model    = "model104069929";
            SupportedLanguages response = client.GetSupportedLanguages(formattedParent, displayLanguageCode, model);

            Assert.Same(expectedResponse, response);
            mockGrpcClient.VerifyAll();
        }
 /// <summary>Snippet for GetSupportedLanguages</summary>
 public void GetSupportedLanguages_RequestObject()
 {
     // Snippet: GetSupportedLanguages(GetSupportedLanguagesRequest,CallSettings)
     // Create client
     TranslationServiceClient translationServiceClient = TranslationServiceClient.Create();
     // Initialize request argument(s)
     GetSupportedLanguagesRequest request = new GetSupportedLanguagesRequest
     {
         ParentAsLocationName = new LocationName("[PROJECT]", "[LOCATION]"),
     };
     // Make the request
     SupportedLanguages response = translationServiceClient.GetSupportedLanguages(request);
     // End snippet
 }
コード例 #5
0
ファイル: HomePage.xaml.cs プロジェクト: AlissaSabre/GTBench
        private async void Page_Loaded(object sender, RoutedEventArgs e)
        {
            MainWindow.Current.Busy = true;
            status.Text             = string.Empty;
            warning.Visibility      = Visibility.Collapsed;

            var settings = Properties.Settings.Default;
            var sb       = new StringBuilder();

            try
            {
                languages_label.Text  = string.Empty;
                languages.ItemsSource = null;

                sb.AppendLine();
                sb.AppendLine($"Project: {settings.ProjectID}");
                sb.AppendLine($"Location: {settings.LocationID}");
                status.Text = sb.ToString();

                var client = await GetTranslationServiceClientAsync();

                var request = new GetSupportedLanguagesRequest
                {
                    DisplayLanguageCode  = "en",
                    ParentAsLocationName = GetLocationName(),
                };

                var response = await client.GetSupportedLanguagesAsync(request);

                languages_label.Text  = $"Available languages ({response.Languages.Count})";
                languages.ItemsSource = response.Languages.Select(lang => lang.LanguageCode + "\t" + lang.DisplayName);

                sb.AppendLine();
                sb.AppendLine("Google Translate service via Cloud Translation (Advanced) API is working.");
                status.Text = sb.ToString();
            }
            catch (Exception exception)
            {
                warning.Visibility = Visibility.Visible;
                // Let the warning panel show its contents.
                await Task.Yield();

                new ExceptionDialog {
                    Exception = exception
                }.ShowDialog();
            }

            MainWindow.Current.Busy = false;
        }
コード例 #6
0
        /// <summary>
        /// Getting a list of supported language codes
        /// </summary>
        /// <param name="projectId">Your Google Cloud Project ID.</param>
        public static void GetSupportedLanguagesSample(string projectId = "[Google Cloud Project ID]")
        {
            TranslationServiceClient     translationServiceClient = TranslationServiceClient.Create();
            GetSupportedLanguagesRequest request = new GetSupportedLanguagesRequest
            {
                ParentAsLocationName = new LocationName(projectId, "global"),
            };
            SupportedLanguages response = translationServiceClient.GetSupportedLanguages(request);

            // List language codes of supported languages
            foreach (SupportedLanguage language in response.Languages)
            {
                Console.WriteLine($"Language Code: {language.LanguageCode}");
            }
        }
        /// <summary>Snippet for GetSupportedLanguagesAsync</summary>
        public async Task GetSupportedLanguagesAsync_RequestObject()
        {
            // Snippet: GetSupportedLanguagesAsync(GetSupportedLanguagesRequest,CallSettings)
            // Additional: GetSupportedLanguagesAsync(GetSupportedLanguagesRequest,CancellationToken)
            // Create client
            TranslationServiceClient translationServiceClient = await TranslationServiceClient.CreateAsync();

            // Initialize request argument(s)
            GetSupportedLanguagesRequest request = new GetSupportedLanguagesRequest
            {
                ParentAsLocationName = new LocationName("[PROJECT]", "[LOCATION]"),
            };
            // Make the request
            SupportedLanguages response = await translationServiceClient.GetSupportedLanguagesAsync(request);

            // End snippet
        }
コード例 #8
0
        public void GetSupportedLanguages2()
        {
            Mock <TranslationService.TranslationServiceClient> mockGrpcClient = new Mock <TranslationService.TranslationServiceClient>(MockBehavior.Strict);

            mockGrpcClient.Setup(x => x.CreateOperationsClient())
            .Returns(new Mock <Operations.OperationsClient>().Object);
            GetSupportedLanguagesRequest request = new GetSupportedLanguagesRequest
            {
                ParentAsLocationName = new LocationName("[PROJECT]", "[LOCATION]"),
            };
            SupportedLanguages expectedResponse = new SupportedLanguages();

            mockGrpcClient.Setup(x => x.GetSupportedLanguages(request, It.IsAny <CallOptions>()))
            .Returns(expectedResponse);
            TranslationServiceClient client   = new TranslationServiceClientImpl(mockGrpcClient.Object, null);
            SupportedLanguages       response = client.GetSupportedLanguages(request);

            Assert.Same(expectedResponse, response);
            mockGrpcClient.VerifyAll();
        }
コード例 #9
0
        public async Task GetSupportedLanguagesAsync2()
        {
            Mock <TranslationService.TranslationServiceClient> mockGrpcClient = new Mock <TranslationService.TranslationServiceClient>(MockBehavior.Strict);

            mockGrpcClient.Setup(x => x.CreateOperationsClient())
            .Returns(new Mock <Operations.OperationsClient>().Object);
            GetSupportedLanguagesRequest request = new GetSupportedLanguagesRequest
            {
                Parent = new LocationName("[PROJECT]", "[LOCATION]").ToString(),
            };
            SupportedLanguages expectedResponse = new SupportedLanguages();

            mockGrpcClient.Setup(x => x.GetSupportedLanguagesAsync(request, It.IsAny <CallOptions>()))
            .Returns(new Grpc.Core.AsyncUnaryCall <SupportedLanguages>(Task.FromResult(expectedResponse), null, null, null, null));
            TranslationServiceClient client   = new TranslationServiceClientImpl(mockGrpcClient.Object, null);
            SupportedLanguages       response = await client.GetSupportedLanguagesAsync(request);

            Assert.Same(expectedResponse, response);
            mockGrpcClient.VerifyAll();
        }
コード例 #10
0
ファイル: GoogleAsync.cs プロジェクト: cathoderaydude/Babel
        private async Task DoGSL()
        {
            try
            {
                string Identifer = Utility.RandomHex();
                DebugLog.Log("Making get supported languages request [" + Identifer + "]");

                if (!File.Exists(Properties.Settings.Default.apiKeyPath))
                {
                    throw new FileNotFoundException("Keyfile not present at " + Properties.Settings.Default.apiKeyPath);
                }

                // Wait for rate limiter before starting the clock
                GoogleAsyncStatic.rate.Check();
                Stopwatch sw = new Stopwatch();

                // Make our connection client
                TranslationServiceClient translationServiceClient = new TranslationServiceClientBuilder
                {
                    CredentialsPath = Properties.Settings.Default.apiKeyPath,
                }.Build();

                // Request supported languages
                GetSupportedLanguagesRequest request = new GetSupportedLanguagesRequest
                {
                    DisplayLanguageCode  = "en",
                    ParentAsLocationName = new LocationName(Properties.Settings.Default.projectName, "global"),
                };

                // Send request
                sw.Start();
                SupportedLanguages response = await translationServiceClient.GetSupportedLanguagesAsync(request);

                sw.Stop();

                // Convert these to a format the combo box will like
                _languages = response.Languages
                             .Where(lang => lang.SupportTarget)
                             .Select(ConvertLanguage)
                             .ToArray();

                _timeStamp = string.Format("{0:00}:{1:00}:{2:00}.{3:000}",
                                           sw.Elapsed.Hours,
                                           sw.Elapsed.Minutes,
                                           sw.Elapsed.Seconds,
                                           sw.Elapsed.Milliseconds);

                isDone = true;

                callback?.Invoke(this);
                DebugLog.Log("Finishing getting supported languages [" + Identifer + "]");
            }
            catch (Grpc.Core.RpcException e)
            {
                Form.Invoke(Form.SafeLogWorkerError, new object[] { e.Message, "http://www.yahoo" });
            }
            catch (Exception e)
            {
                Form.Invoke(Form.SafeLogWorkerError, new object[] { e.Message, "" });
            }
        }