/// <summary>
        /// Add Translation Target to the configuration.
        /// </summary>
        /// <param name="targetUri">The SAS URI for the target container to which the translated documents will be written.</param>
        /// <param name="languageCode">Language code to translate documents to. For supported languages see
        /// <a href="https://docs.microsoft.com/azure/cognitive-services/translator/language-support#translate"/>.</param>
        /// <param name="glossary">Custom <see cref="TranslationGlossary"/> to be used in the translation operation. For supported file types see
        /// <see cref="DocumentTranslationClient.GetGlossaryFormatsAsync(System.Threading.CancellationToken)"/>.</param>
        public void AddTarget(Uri targetUri, string languageCode, TranslationGlossary glossary = default)
        {
            var target = new TranslationTarget(targetUri, languageCode);

            if (glossary != null)
            {
                target.Glossaries.Add(glossary);
            }
            Targets.Add(target);
        }
        /// <summary>
        /// Initializes a new instance of TranslationConfiguration.
        /// </summary>
        /// <param name="sourceUri">The SAS URI for the source container containing documents to be translated.</param>
        /// <param name="targetUri">The SAS URI for the target container to which the translated documents will be written.</param>
        /// <param name="targetLanguageCode">Language code to translate documents to. For supported languages see
        /// <a href="https://docs.microsoft.com/azure/cognitive-services/translator/language-support#translate"/>.</param>
        /// <param name="glossary">Custom <see cref="TranslationGlossary"/> to be used in the translation operation. For supported file types see
        /// <see cref="DocumentTranslationClient.GetGlossaryFormatsAsync(System.Threading.CancellationToken)"/>.</param>
        public TranslationConfiguration(Uri sourceUri, Uri targetUri, string targetLanguageCode, TranslationGlossary glossary = default)
        {
            Source = new TranslationSource(sourceUri);
            var target = new TranslationTarget(targetUri, targetLanguageCode);

            if (glossary != null)
            {
                target.Glossaries.Add(glossary);
            }
            Targets.Add(target);
        }
        /// <summary>
        /// Initializes a new instance of DocumentTranslationInput.
        /// </summary>
        /// <param name="sourceUri">The SAS URI for the source container containing documents to be translated.</param>
        /// <param name="targetUri">The SAS URI for the target container to which the translated documents will be written.</param>
        /// <param name="targetLanguageCode">Language code to translate documents to. For supported languages see
        /// <a href="https://docs.microsoft.com/azure/cognitive-services/translator/language-support#translate"/>.</param>
        /// <param name="glossary">Custom <see cref="TranslationGlossary"/> to be used in the translation operation. For supported file types see
        /// <see cref="DocumentTranslationClient.GetGlossaryFormatsAsync(System.Threading.CancellationToken)"/>.</param>
        public DocumentTranslationInput(Uri sourceUri, Uri targetUri, string targetLanguageCode, TranslationGlossary glossary = default)
        {
            Source = new TranslationSource(sourceUri);
            var target = new TranslationTarget(targetUri, targetLanguageCode);

            if (glossary != null)
            {
                target.Glossaries.Add(glossary);
            }
            Targets = new List <TranslationTarget> {
                target
            };
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Starts a translation operation for documents in an Azure Blob Container.
        /// For document length limits, maximum batch size, and supported document formats, see
        /// <a href="https://docs.microsoft.com/azure/cognitive-services/translator/document-translation/overview"/>.
        /// </summary>
        /// <param name="sourceBlobContainerSas">The SAS URL for the source container containing documents to be translated. </param>
        /// <param name="targetBlobContainerSas">The SAS URL for the target container to which the translated documents will be written. </param>
        /// <param name="targetLanguageCode">Language code to translate documents to. For supported languages see
        /// <a href="https://docs.microsoft.com/azure/cognitive-services/translator/language-support#translate"/>.</param>
        /// <param name="glossary">Custom translation glossary to be used in the translation operation. For supported file types see
        /// <see cref="GetGlossaryFormatsAsync(CancellationToken)"/>.</param>
        /// <param name="options">Set translation options including source language and custom translation category.</param>
        /// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param>
        public virtual async Task <DocumentTranslationOperation> StartTranslationAsync(Uri sourceBlobContainerSas, Uri targetBlobContainerSas, string targetLanguageCode, TranslationGlossary glossary = default, TranslationOperationOptions options = default, CancellationToken cancellationToken = default)
        {
            var source = new TranslationSource(sourceBlobContainerSas)
            {
                LanguageCode = options?.SourceLanguage,
                Filter       = options?.Filter
            };

            var target = new TranslationTarget(targetBlobContainerSas, targetLanguageCode)
            {
                Category = options?.Category
            };

            if (glossary != null)
            {
                target.Glossaries.Add(glossary);
            }

            var targets = new List <TranslationTarget>
            {
                target
            };

            var request = new BatchSubmissionRequest(new List <TranslationConfiguration>
            {
                new TranslationConfiguration(source, targets)
                {
                    StorageType = options?.StorageType
                }
            }
                                                     );

            using DiagnosticScope scope = _clientDiagnostics.CreateScope($"{nameof(DocumentTranslationClient)}.{nameof(StartTranslationAsync)}");
            scope.Start();

            try
            {
                ResponseWithHeaders <DocumentTranslationSubmitBatchRequestHeaders> job = await _serviceRestClient.SubmitBatchRequestAsync(request, cancellationToken).ConfigureAwait(false);

                return(new DocumentTranslationOperation(_serviceRestClient, _clientDiagnostics, job.Headers.OperationLocation));
            }
            catch (Exception e)
            {
                scope.Failed(e);
                throw;
            }
        }