/// <summary> /// Translates a batch of texts on GCS and stores the result in a GCS location. /// Glossary is applied for translation. /// </summary> /// <param name="inputUri">The GCS path where input configuration is stored.</param> /// <param name="outputUri">The GCS path for translation output.</param> /// <param name="projectId">Your Google Cloud Project ID.</param> /// <param name="location">Region.</param> /// <param name="glossaryId">Required. Translation Glossary ID.</param> /// <param name="targetLanguage">Target language code.</param> /// <param name="sourceLanguage">Source language code.</param> public static void BatchTranslateTextWithGlossarySample( string inputUri = "gs://cloud-samples-data/text.txt", string outputUri = "gs://YOUR_BUCKET_ID/path_to_store_results/", string projectId = "[Google Cloud Project ID]", string location = "us-central1", string glossaryId = "[YOUR_GLOSSARY_ID]", string targetLanguage = "en", string sourceLanguage = "ja") { TranslationServiceClient translationServiceClient = TranslationServiceClient.Create(); string glossaryPath = $"projects/{projectId}/locations/{location}/glossaries/{glossaryId}"; BatchTranslateTextRequest request = new BatchTranslateTextRequest { ParentAsLocationName = new LocationName(projectId, location), SourceLanguageCode = sourceLanguage, TargetLanguageCodes = { // Target language code. targetLanguage, }, InputConfigs = { new InputConfig { GcsSource = new GcsSource { InputUri = inputUri, }, MimeType = "text/plain", }, }, OutputConfig = new OutputConfig { GcsDestination = new GcsDestination { OutputUriPrefix = outputUri, }, }, Glossaries = { { targetLanguage, new TranslateTextGlossaryConfig { Glossary = glossaryPath, } }, }, }; // Poll until the returned long-running operation is completed. BatchTranslateResponse response = translationServiceClient.BatchTranslateText(request).PollUntilCompleted().Result; Console.WriteLine($"Total Characters: {response.TotalCharacters}"); Console.WriteLine($"Translated Characters: {response.TranslatedCharacters}"); }
/// <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 }