/// <summary> /// Translates a batch of texts on GCS and stores the result in a GCS location. /// </summary> /// <param name="projectId">Your Google Cloud Project ID.</param> /// <param name="location">Region.</param> /// <param name="inputUri">The GCS path where input configuration is stored.</param> /// <param name="outputUri">The GCS path for translation output.</param> /// <param name="sourceLanguage">Source language code.</param> /// <param name="targetLanguage">Target language code.</param> public static void BatchTranslateTextSample(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 sourceLanguage = "en", string targetLanguage = "ja") { TranslationServiceClient translationServiceClient = TranslationServiceClient.Create(); BatchTranslateTextRequest request = new BatchTranslateTextRequest { ParentAsLocationName = new LocationName(projectId, location), SourceLanguageCode = sourceLanguage, TargetLanguageCodes = { targetLanguage, }, InputConfigs = { new InputConfig { GcsSource = new GcsSource { InputUri = inputUri, }, MimeType = "text/plain", }, }, OutputConfig = new OutputConfig { GcsDestination = new GcsDestination { OutputUriPrefix = outputUri, }, }, }; // 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 BatchTranslateText</summary> public void BatchTranslateText_RequestObject() { // Snippet: BatchTranslateText(BatchTranslateTextRequest,CallSettings) // Create client TranslationServiceClient translationServiceClient = TranslationServiceClient.Create(); // 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 = translationServiceClient.BatchTranslateText(request); // Poll until the returned long-running operation is complete Operation <BatchTranslateResponse, BatchTranslateMetadata> completedResponse = response.PollUntilCompleted(); // 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 = translationServiceClient.PollOnceBatchTranslateText(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 }
/// <summary> /// Translates a batch of texts on GCS and stores the result in a GCS location. /// Glossary is applied for translation. /// A model is used for translation. Can be AutoML or General (built-in) Model. /// </summary> /// <param name="targetLanguage">Target language code.</param> /// <param name="sourceLanguage">Source language code.</param> /// <param name="modelId">Translation model ID.</param> /// <param name="glossaryId">Translation Glossary ID.</param> /// <param name="projectId">Your Google Cloud Project ID.</param> /// <param name="location"> Region.</param> /// <param name="inputUri">The GCS path where input configuration is stored.</param> /// <param name="outputUri">The GCS path for translation output.</param> public static void BatchTranslateTextWithGlossaryAndModelSample( string inputUri = "gs://cloud-samples-data/translation/text_with_custom_model_and_glossary.txt", string outputUri = "gs://YOUR_BUCKET_ID/path_to_store_results/", string projectId = "[Google Cloud Project ID]", string location = "us-central1", string targetLanguage = "ja", string sourceLanguage = "en", string modelId = "[YOUR-MODEL-ID]", string glossaryId = "[YOUR_GLOSSARY_ID]") { TranslationServiceClient translationServiceClient = TranslationServiceClient.Create(); string modelPath = $"projects/{projectId}/locations/{location}/models/{modelId}"; 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, }, }, Models = { // A model is used for Translation. { targetLanguage, modelPath }, }, 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}"); }