/// <summary>Snippet for DeleteGlossaryAsync</summary>
        public async Task DeleteGlossaryAsync()
        {
            // Snippet: DeleteGlossaryAsync(GlossaryName,CallSettings)
            // Additional: DeleteGlossaryAsync(GlossaryName,CancellationToken)
            // Create client
            TranslationServiceClient translationServiceClient = await TranslationServiceClient.CreateAsync();

            // Initialize request argument(s)
            GlossaryName name = new GlossaryName("[PROJECT]", "[LOCATION]", "[GLOSSARY]");
            // Make the request
            Operation <DeleteGlossaryResponse, DeleteGlossaryMetadata> response =
                await translationServiceClient.DeleteGlossaryAsync(name);

            // Poll until the returned long-running operation is complete
            Operation <DeleteGlossaryResponse, DeleteGlossaryMetadata> completedResponse =
                await response.PollUntilCompletedAsync();

            // Retrieve the operation result
            DeleteGlossaryResponse result = completedResponse.Result;

            // Or get the name of the operation
            string operationName = response.Name;
            // This name can be stored, then the long-running operation retrieved later by name
            Operation <DeleteGlossaryResponse, DeleteGlossaryMetadata> retrievedResponse =
                await translationServiceClient.PollOnceDeleteGlossaryAsync(operationName);

            // Check if the retrieved long-running operation has completed
            if (retrievedResponse.IsCompleted)
            {
                // If it has completed, then access the result
                DeleteGlossaryResponse retrievedResult = retrievedResponse.Result;
            }
            // End snippet
        }
        /// <summary>Snippet for CreateGlossaryAsync</summary>
        public async Task CreateGlossaryAsync()
        {
            // Snippet: CreateGlossaryAsync(string,Glossary,CallSettings)
            // Additional: CreateGlossaryAsync(string,Glossary,CancellationToken)
            // Create client
            TranslationServiceClient translationServiceClient = await TranslationServiceClient.CreateAsync();

            // Initialize request argument(s)
            string   formattedParent = new LocationName("[PROJECT]", "[LOCATION]").ToString();
            Glossary glossary        = new Glossary();
            // Make the request
            Operation <Glossary, CreateGlossaryMetadata> response =
                await translationServiceClient.CreateGlossaryAsync(formattedParent, glossary);

            // Poll until the returned long-running operation is complete
            Operation <Glossary, CreateGlossaryMetadata> completedResponse =
                await response.PollUntilCompletedAsync();

            // Retrieve the operation result
            Glossary result = completedResponse.Result;

            // Or get the name of the operation
            string operationName = response.Name;
            // This name can be stored, then the long-running operation retrieved later by name
            Operation <Glossary, CreateGlossaryMetadata> retrievedResponse =
                await translationServiceClient.PollOnceCreateGlossaryAsync(operationName);

            // Check if the retrieved long-running operation has completed
            if (retrievedResponse.IsCompleted)
            {
                // If it has completed, then access the result
                Glossary retrievedResult = retrievedResponse.Result;
            }
            // End snippet
        }
        /// <summary>Snippet for GetGlossaryAsync</summary>
        public async Task GetGlossaryAsync()
        {
            // Snippet: GetGlossaryAsync(GlossaryName,CallSettings)
            // Additional: GetGlossaryAsync(GlossaryName,CancellationToken)
            // Create client
            TranslationServiceClient translationServiceClient = await TranslationServiceClient.CreateAsync();

            // Initialize request argument(s)
            GlossaryName name = new GlossaryName("[PROJECT]", "[LOCATION]", "[GLOSSARY]");
            // Make the request
            Glossary response = await translationServiceClient.GetGlossaryAsync(name);

            // End snippet
        }
        /// <summary>Snippet for GetSupportedLanguagesAsync</summary>
        public async Task GetSupportedLanguagesAsync()
        {
            // Snippet: GetSupportedLanguagesAsync(LocationName,string,string,CallSettings)
            // Additional: GetSupportedLanguagesAsync(LocationName,string,string,CancellationToken)
            // Create client
            TranslationServiceClient translationServiceClient = await TranslationServiceClient.CreateAsync();

            // Initialize request argument(s)
            LocationName parent = new LocationName("[PROJECT]", "[LOCATION]");
            string       displayLanguageCode = "";
            string       model = "";
            // Make the request
            SupportedLanguages response = await translationServiceClient.GetSupportedLanguagesAsync(parent, displayLanguageCode, model);

            // End snippet
        }
        /// <summary>Snippet for ListGlossariesAsync</summary>
        public async Task ListGlossariesAsync_RequestObject()
        {
            // Snippet: ListGlossariesAsync(ListGlossariesRequest,CallSettings)
            // Create client
            TranslationServiceClient translationServiceClient = await TranslationServiceClient.CreateAsync();

            // Initialize request argument(s)
            ListGlossariesRequest request = new ListGlossariesRequest
            {
                ParentAsLocationName = new LocationName("[PROJECT]", "[LOCATION]"),
            };
            // Make the request
            PagedAsyncEnumerable <ListGlossariesResponse, Glossary> response =
                translationServiceClient.ListGlossariesAsync(request);

            // Iterate over all response items, lazily performing RPCs as required
            await response.ForEachAsync((Glossary item) =>
            {
                // Do something with each item
                Console.WriteLine(item);
            });

            // Or iterate over pages (of server-defined size), performing one RPC per page
            await response.AsRawResponses().ForEachAsync((ListGlossariesResponse page) =>
            {
                // Do something with each page of items
                Console.WriteLine("A page of results:");
                foreach (Glossary item in page)
                {
                    Console.WriteLine(item);
                }
            });

            // Or retrieve a single page of known size (unless it's the final page), performing as many RPCs as required
            int             pageSize   = 10;
            Page <Glossary> singlePage = await response.ReadPageAsync(pageSize);

            // Do something with the page of items
            Console.WriteLine($"A page of {pageSize} results (unless it's the final page):");
            foreach (Glossary item in singlePage)
            {
                Console.WriteLine(item);
            }
            // Store the pageToken, for when the next page is required.
            string nextPageToken = singlePage.NextPageToken;
            // End snippet
        }
        /// <summary>Snippet for DetectLanguageAsync</summary>
        public async Task DetectLanguageAsync()
        {
            // Snippet: DetectLanguageAsync(LocationName,string,string,IDictionary<string, string>,CallSettings)
            // Additional: DetectLanguageAsync(LocationName,string,string,IDictionary<string, string>,CancellationToken)
            // Create client
            TranslationServiceClient translationServiceClient = await TranslationServiceClient.CreateAsync();

            // Initialize request argument(s)
            LocationName parent   = new LocationName("[PROJECT]", "[LOCATION]");
            string       model    = "";
            string       mimeType = "";
            IDictionary <string, string> labels = new Dictionary <string, string>();
            // Make the request
            DetectLanguageResponse response = await translationServiceClient.DetectLanguageAsync(parent, model, mimeType, labels);

            // End snippet
        }
        /// <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
        }
        /// <summary>Snippet for DetectLanguageAsync</summary>
        public async Task DetectLanguageAsync_RequestObject()
        {
            // Snippet: DetectLanguageAsync(DetectLanguageRequest,CallSettings)
            // Additional: DetectLanguageAsync(DetectLanguageRequest,CancellationToken)
            // Create client
            TranslationServiceClient translationServiceClient = await TranslationServiceClient.CreateAsync();

            // Initialize request argument(s)
            DetectLanguageRequest request = new DetectLanguageRequest
            {
                Parent = new LocationName("[PROJECT]", "[LOCATION]").ToString(),
            };
            // Make the request
            DetectLanguageResponse response = await translationServiceClient.DetectLanguageAsync(request);

            // End snippet
        }
        public async static Task <String> googleTranslateApiV3(String textToTranslate, String targetLanguage, String projectName, ILogger logger, IOptions <GoogleTranslateApiCredentials> googleTranslateApiCredentials)
        {
            if (Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT").Equals("Development"))
            {
                //var credential = GoogleCredential.GetApplicationDefaultAsync();
                //Hostirano lokalno
                TranslationServiceClientBuilder translationServiceClientBuilder = new TranslationServiceClientBuilder();
                translationServiceClientBuilder.JsonCredentials = JsonConvert.SerializeObject(googleTranslateApiCredentials.Value);
                TranslationServiceClient translationServiceClient = await translationServiceClientBuilder.BuildAsync();

                TranslateTextRequest request = new TranslateTextRequest
                {
                    Contents =
                    {
                        // The content to translate.
                        textToTranslate,
                    },
                    TargetLanguageCode   = targetLanguage,
                    ParentAsLocationName = new LocationName(projectName, "global"),
                };
                TranslateTextResponse response = await translationServiceClient.TranslateTextAsync(request);

                return(response.Translations[0].TranslatedText);
            }
            if (Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT").Equals("Production"))
            {
                TranslationServiceClient translationServiceClient = await TranslationServiceClient.CreateAsync();

                TranslateTextRequest request = new TranslateTextRequest
                {
                    Contents =
                    {
                        // The content to translate.
                        textToTranslate,
                    },
                    TargetLanguageCode   = targetLanguage,
                    ParentAsLocationName = new LocationName(projectName, "global"),
                };
                TranslateTextResponse response = await translationServiceClient.TranslateTextAsync(request);

                return(response.Translations[0].TranslatedText);
            }

            return(null);
        }
        /// <summary>Snippet for TranslateTextAsync</summary>
        public async Task TranslateTextAsync_RequestObject()
        {
            // Snippet: TranslateTextAsync(TranslateTextRequest,CallSettings)
            // Additional: TranslateTextAsync(TranslateTextRequest,CancellationToken)
            // Create client
            TranslationServiceClient translationServiceClient = await TranslationServiceClient.CreateAsync();

            // Initialize request argument(s)
            TranslateTextRequest request = new TranslateTextRequest
            {
                Contents             = { },
                TargetLanguageCode   = "",
                ParentAsLocationName = new LocationName("[PROJECT]", "[LOCATION]"),
            };
            // Make the request
            TranslateTextResponse response = await translationServiceClient.TranslateTextAsync(request);

            // End snippet
        }
        /// <summary>Snippet for BatchTranslateTextAsync</summary>
        public async Task BatchTranslateTextAsync_RequestObject()
        {
            // Snippet: BatchTranslateTextAsync(BatchTranslateTextRequest,CallSettings)
            // Create client
            TranslationServiceClient translationServiceClient = await TranslationServiceClient.CreateAsync();

            // Initialize request argument(s)
            BatchTranslateTextRequest request = new BatchTranslateTextRequest
            {
                ParentAsLocationName = new LocationName("[PROJECT]", "[LOCATION]"),
                SourceLanguageCode   = "",
                TargetLanguageCodes  = { },
                InputConfigs         = { },
                OutputConfig         = new OutputConfig(),
            };
            // Make the request
            Operation <BatchTranslateResponse, BatchTranslateMetadata> response =
                await translationServiceClient.BatchTranslateTextAsync(request);

            // Poll until the returned long-running operation is complete
            Operation <BatchTranslateResponse, BatchTranslateMetadata> completedResponse =
                await response.PollUntilCompletedAsync();

            // Retrieve the operation result
            BatchTranslateResponse result = completedResponse.Result;

            // Or get the name of the operation
            string operationName = response.Name;
            // This name can be stored, then the long-running operation retrieved later by name
            Operation <BatchTranslateResponse, BatchTranslateMetadata> retrievedResponse =
                await translationServiceClient.PollOnceBatchTranslateTextAsync(operationName);

            // Check if the retrieved long-running operation has completed
            if (retrievedResponse.IsCompleted)
            {
                // If it has completed, then access the result
                BatchTranslateResponse retrievedResult = retrievedResponse.Result;
            }
            // End snippet
        }
Пример #12
0
        public static async Task <TranslationServiceClient> GetTranslationServiceClientAsync()
        {
            var path = Properties.Settings.Default.CredentialsPath;

            if (!TranslationServiceClientCache.TryGetTarget(out var tuple) ||
                tuple.CredentialsPath != path)
            {
                var client = string.IsNullOrWhiteSpace(path)
                    ? await TranslationServiceClient.CreateAsync()
                    : await new TranslationServiceClientBuilder
                {
                    CredentialsPath = path
                }

                .BuildAsync();
                tuple = new TranslationServiceClientTuple {
                    Client = client, CredentialsPath = path
                };
                TranslationServiceClientCache.SetTarget(tuple);
            }
            return(tuple.Client);
        }