/// <summary> /// Translates a single item of text synchronously. /// </summary> /// <remarks> /// This implementation simply delegates to <see cref="TranslateText(IEnumerable{string}, string, string, TranslationModel?)"/> /// </remarks> /// <param name="text">The text to translate.</param> /// <param name="targetLanguage">The code for the language to translate into. Must not be null.</param> /// <param name="sourceLanguage">The code for the language to translate from. May be null, in which case the server /// will detect the source language.</param> /// <param name="model">The model to request for translation. May be null, in which case <see cref="DefaultModel"/> is /// used.</param> /// <returns>The translation of <paramref name="text"/>.</returns> public virtual TranslationResult TranslateText( string text, string targetLanguage, string sourceLanguage = null, TranslationModel?model = null) => TranslateText( new[] { GaxPreconditions.CheckNotNull(text, nameof(text)) }, targetLanguage, sourceLanguage, model)[0];
private string GetEffectiveModelName(TranslationModel?model) { var effectiveModel = model ?? DefaultModel; TranslationModels.ValidateModel(effectiveModel); return(effectiveModel.ToApiName()); }
/// <summary> /// Translates a single item of HTML synchronously. /// </summary> /// <remarks> /// <para> /// This implementation simply delegates to <see cref="TranslateHtml(IEnumerable{string}, string, string, TranslationModel?)"/> /// </para> /// <para> /// See the [Translation API documentation](https://cloud.google.com/translate/markup) for more details /// on how to translate HTML and the terms of service. /// </para> /// </remarks> /// <param name="html">The HTML to translate.</param> /// <param name="targetLanguage">The code for the language to translate into. Must not be null.</param> /// <param name="sourceLanguage">The code for the language to translate from. May be null, in which case the server /// will detect the source language.</param> /// <param name="model">The model to request for translation. May be null, in which case <see cref="DefaultModel"/> is /// used.</param> /// <returns>The translation of <paramref name="html"/>.</returns> public virtual TranslationResult TranslateHtml( string html, string targetLanguage, string sourceLanguage = null, TranslationModel?model = null) => TranslateHtml( new[] { GaxPreconditions.CheckNotNull(html, nameof(html)) }, targetLanguage, sourceLanguage, model)[0];
/// <inheritdoc /> public override IList <Language> ListLanguages(string target = null, TranslationModel?model = null) { var request = Service.Languages.List(); request.Target = target; request.Model = GetEffectiveModelName(model); return(request.Execute().Languages.Select(Language.FromResource).ToList()); }
/// <summary> /// Translates a single item of HTML asynchronously. /// </summary> /// <remarks> /// <para> /// This implementation simply delegates to <see cref="TranslateHtmlAsync(IEnumerable{string}, string, string, TranslationModel?, CancellationToken)"/> /// </para> /// <para> /// See the [Translation API documentation](https://cloud.google.com/translate/markup) for more details /// on how to translate HTML and the terms of service. /// </para> /// </remarks> /// <param name="html">The HTML to translate. Must not be null.</param> /// <param name="targetLanguage">The code for the language to translate into. Must not be null.</param> /// <param name="sourceLanguage">The code for the language to translate from. May be null, in which case the server /// will detect the source language.</param> /// <param name="model">The model to request for translation. May be null, in which case <see cref="DefaultModel"/> is /// used.</param> /// <param name="cancellationToken">The token to monitor for cancellation requests.</param> /// <returns>The translation of <paramref name="html"/>.</returns> public virtual async Task <TranslationResult> TranslateHtmlAsync(string html, string targetLanguage, string sourceLanguage = null, TranslationModel?model = null, CancellationToken cancellationToken = default(CancellationToken)) => (await TranslateHtmlAsync( new[] { GaxPreconditions.CheckNotNull(html, nameof(html)) }, targetLanguage, sourceLanguage, model, cancellationToken).ConfigureAwait(false))[0];
/// <summary> /// Constructs an instance. /// </summary> /// <remarks>This constructor is for the benefit of testability, in case you wish to provide your own /// fake implementation of <see cref="TranslationClient"/>.</remarks> /// <param name="originalText">The original text.</param> /// <param name="translatedText">The translated text.</param> /// <param name="detectedSourceLanguage">The source language code detected by the server, if any.</param> /// <param name="specifiedSourceLanguage">The source language code specified by the API caller.</param> /// <param name="targetLanguage">The target language code.</param> /// <param name="model">The model used to perform the translation, if known.</param> public TranslationResult(string originalText, string translatedText, string detectedSourceLanguage, string specifiedSourceLanguage, string targetLanguage, TranslationModel?model) { OriginalText = originalText; TranslatedText = translatedText; SpecifiedSourceLanguage = specifiedSourceLanguage; DetectedSourceLanguage = detectedSourceLanguage; TargetLanguage = targetLanguage; }
/// <inheritdoc /> public override async Task <TranslationResult> TranslateHtmlAsync(string text, string targetLanguage, string sourceLanguage = null, TranslationModel?model = null, CancellationToken cancellationToken = default(CancellationToken)) { var request = Service.Translations.List(new Repeatable <string>(new[] { text }), targetLanguage); ModifyRequest(request, sourceLanguage, FormatEnum.Html, model); var result = (await request.ExecuteAsync(cancellationToken).ConfigureAwait(false)).Translations[0]; return(new TranslationResult(result, text, sourceLanguage, targetLanguage)); }
/// <inheritdoc /> public override async Task <IList <TranslationResult> > TranslateHtmlAsync(IEnumerable <string> htmlItems, string targetLanguage, string sourceLanguage = null, TranslationModel?model = null, CancellationToken cancellationToken = default(CancellationToken)) { // Evaluate once so we can zip suitably. List <string> items = htmlItems.ToList(); var request = Service.Translations.List(new Repeatable <string>(items), targetLanguage); ModifyRequest(request, sourceLanguage, FormatEnum.Html, model); return((await request.ExecuteAsync(cancellationToken).ConfigureAwait(false)).Translations .Zip(items, (result, item) => new TranslationResult(result, item, sourceLanguage, targetLanguage)) .ToList()); }
/// <inheritdoc /> public override async Task <IList <TranslationResult> > TranslateHtmlAsync(IEnumerable <string> htmlItems, string targetLanguage, string sourceLanguage = null, TranslationModel?model = null, CancellationToken cancellationToken = default) { var items = ConvertToListAndCheckNoNullElements(htmlItems, nameof(htmlItems)); GaxPreconditions.CheckNotNull(targetLanguage, nameof(targetLanguage)); var request = Service.Translations.List(new Repeatable <string>(items), targetLanguage); ModifyRequest(request, sourceLanguage, FormatEnum.Html, model); var response = await request.ExecuteAsync(cancellationToken).ConfigureAwait(false); return(UnpackTranslationResponse(items, sourceLanguage, targetLanguage, response)); }
/// <inheritdoc /> public override async Task <IList <TranslationResult> > TranslateTextAsync(IEnumerable <string> textItems, string targetLanguage, string sourceLanguage = null, TranslationModel?model = null, CancellationToken cancellationToken = default) { var items = ConvertToListAndCheckNoNullElements(textItems, nameof(textItems)); GaxPreconditions.CheckNotNull(targetLanguage, nameof(targetLanguage)); var translateRequest = new TranslateTextRequest { Q = items, Target = targetLanguage }; var request = Service.Translations.Translate(translateRequest); ModifyRequest(request, translateRequest, sourceLanguage, TextFormat, model); var response = await request.ExecuteAsync(cancellationToken).ConfigureAwait(false); return(UnpackTranslationResponse(items, sourceLanguage, targetLanguage, response)); }
/// <summary> /// Translates multiple items of HTML synchronously. /// </summary> /// <remarks> /// See the [Translation API documentation](https://cloud.google.com/translate/markup) for more details /// on how to translate HTML and the terms of service. /// </remarks> /// <param name="htmlItems">The HTML strings to translate. Must not be null or contain null elements.</param> /// <param name="targetLanguage">The code for the language to translate into. Must not be null.</param> /// <param name="sourceLanguage">The code for the language to translate from. May be null, in which case the server /// will detect the source language.</param> /// <param name="model">The model to request for translation. May be null, in which case <see cref="DefaultModel"/> is /// used.</param> /// <returns>A list of translations. This will be the same size as <paramref name="htmlItems"/>, in the same order.</returns> public virtual IList <TranslationResult> TranslateHtml(IEnumerable <string> htmlItems, string targetLanguage, string sourceLanguage = null, TranslationModel?model = null) { throw new NotImplementedException(); }
private void ModifyRequest(TranslateRequest request, TranslateTextRequest body, string sourceLanguage, string format, TranslationModel?model) { request.ModifyRequest += _versionHeaderAction; body.Source = sourceLanguage; body.Format = format; body.Model = GetEffectiveModelName(model); }
private void ModifyRequest(ListRequest request, string sourceLanguage, FormatEnum format, TranslationModel?model) { request.ModifyRequest += _versionHeaderAction; request.Source = sourceLanguage; request.Format = format; var effectiveModel = model ?? DefaultModel; TranslationModels.ValidateModel(effectiveModel); // Never explicitly request the base model. request.Model = effectiveModel == TranslationModel.Base ? null : effectiveModel.ToApiName(); }
/// <inheritdoc /> public override IList <TranslationResult> TranslateHtml(IEnumerable <string> htmlItems, string targetLanguage, string sourceLanguage = null, TranslationModel?model = null) { var items = ConvertToListAndCheckNoNullElements(htmlItems, nameof(htmlItems)); GaxPreconditions.CheckNotNull(targetLanguage, nameof(targetLanguage)); var translateRequest = new TranslateTextRequest { Q = items, Target = targetLanguage }; var request = Service.Translations.Translate(translateRequest); ModifyRequest(request, translateRequest, sourceLanguage, HtmlFormat, model); return(UnpackTranslationResponse(items, sourceLanguage, targetLanguage, request.Execute())); }
/// <summary> /// Translates multiple items of HTML asynchronously. /// </summary> /// <remarks> /// See the [Translation API documentation](https://cloud.google.com/translate/markup) for more details /// on how to translate HTML and the terms of service. /// </remarks> /// <param name="htmlItems">The HTML strings to translate. Must not be null or contain null elements.</param> /// <param name="targetLanguage">The code for the language to translate into. Must not be null.</param> /// <param name="sourceLanguage">The code for the language to translate from. May be null, in which case the server /// will detect the source language.</param> /// <param name="model">The model to request for translation. May be null, in which case <see cref="DefaultModel"/> is /// used.</param> /// <param name="cancellationToken">The token to monitor for cancellation requests.</param> /// <returns>A list of translations. This will be the same size as <paramref name="htmlItems"/>, in the same order.</returns> public virtual Task <IList <TranslationResult> > TranslateHtmlAsync(IEnumerable <string> htmlItems, string targetLanguage, string sourceLanguage = null, TranslationModel?model = null, CancellationToken cancellationToken = default(CancellationToken)) { throw new NotImplementedException(); }
/// <summary> /// Lists the language supported by the Translate API asynchronously. /// </summary> /// <param name="target">The target language in which to return the language names in the results, /// for display purposes. May be null, in which case only the language codes are returned.</param> /// <param name="model">The model to request languages for. May be null, in which case <see cref="DefaultModel"/> is /// used.</param> /// <param name="cancellationToken">The token to monitor for cancellation requests.</param> /// <returns>A list of supported languages.</returns> public virtual Task <IList <Language> > ListLanguagesAsync(string target = null, TranslationModel?model = null, CancellationToken cancellationToken = default(CancellationToken)) { throw new NotImplementedException(); }
/// <inheritdoc /> public override TranslationResult TranslateText(string text, string targetLanguage, string sourceLanguage = null, TranslationModel?model = null) { var request = Service.Translations.List(new Repeatable <string>(new[] { text }), targetLanguage); ModifyRequest(request, sourceLanguage, FormatEnum.Text, model); var result = request.Execute().Translations[0]; return(new TranslationResult(result, text, sourceLanguage, targetLanguage)); }
/// <summary> /// Constructs an instance. /// </summary> /// <remarks>This constructor is for the benefit of testability, in case you wish to provide your own /// fake implementation of <see cref="TranslationClient"/>.</remarks> /// <param name="originalText">The original text.</param> /// <param name="translatedText">The translated text.</param> /// <param name="detectedSourceLanguage">The source language code detected by the server, if any.</param> /// <param name="specifiedSourceLanguage">The source language code specified by the API caller.</param> /// <param name="targetLanguage">The target language code.</param> /// <param name="model">The model used to perform the translation, if known.</param> public TranslationResult(string originalText, string translatedText, string detectedSourceLanguage, string specifiedSourceLanguage, string targetLanguage, TranslationModel?model) : this(originalText, translatedText, detectedSourceLanguage, specifiedSourceLanguage, targetLanguage, model, model == null ? null : TranslationModels.ToApiName(model.Value)) { }
/// <summary> /// Translates a single item of HTML asynchronously. /// </summary> /// <remarks> /// See the [Translation API documentation](https://cloud.google.com/translate/markup) for more details /// on how to translate HTML and the terms of service. /// </remarks> /// <param name="html">The HTML to translate.</param> /// <param name="targetLanguage">The code for the language to translate into. Must not be null.</param> /// <param name="sourceLanguage">The code for the language to translate from. May be null, in which case the server /// will detect the source language.</param> /// <param name="model">The model to request for translation. May be null, in which case <see cref="DefaultModel"/> is /// used.</param> /// <param name="cancellationToken">The token to monitor for cancellation requests.</param> /// <returns>The translation of <paramref name="html"/>.</returns> public virtual Task <TranslationResult> TranslateHtmlAsync(string html, string targetLanguage, string sourceLanguage = null, TranslationModel?model = null, CancellationToken cancellationToken = default(CancellationToken)) { throw new NotImplementedException(); }
/// <inheritdoc /> public override async Task <IList <Language> > ListLanguagesAsync(string target = null, TranslationModel?model = null, CancellationToken cancellationToken = default) { var request = Service.Languages.List(); request.ModifyRequest += _versionHeaderAction; request.Target = target; request.Model = GetEffectiveModelName(model); return((await request.ExecuteAsync(cancellationToken).ConfigureAwait(false)).Languages.Select(Language.FromResource).ToList()); }
/// <summary> /// Translates a single item of HTML synchronously. /// </summary> /// <remarks> /// See the [Translation API documentation](https://cloud.google.com/translate/markup) for more details /// on how to translate HTML and the terms of service. /// </remarks> /// <param name="html">The HTML to translate.</param> /// <param name="targetLanguage">The code for the language to translate into. Must not be null.</param> /// <param name="sourceLanguage">The code for the language to translate from. May be null, in which case the server /// will detect the source language.</param> /// <param name="model">The model to request for translation. May be null, in which case <see cref="DefaultModel"/> is /// used.</param> /// <returns>The translation of <paramref name="html"/>.</returns> public virtual TranslationResult TranslateHtml(string html, string targetLanguage, string sourceLanguage = null, TranslationModel?model = null) { throw new NotImplementedException(); }
private void ModifyRequest(ListRequest request, string sourceLanguage, FormatEnum format, TranslationModel?model) { request.ModifyRequest += _versionHeaderAction; request.Source = sourceLanguage; request.Format = format; request.Model = GetEffectiveModelName(model); }
/// <summary> /// Lists the language supported by the Translate API synchronously. /// </summary> /// <param name="target">The target language in which to return the language names in the results, /// for display purposes. May be null, in which case only the language codes are returned.</param> /// <param name="model">The model to request languages for. May be null, in which case <see cref="DefaultModel"/> is /// used.</param> /// <returns>A list of supported languages.</returns> public virtual IList <Language> ListLanguages(string target = null, TranslationModel?model = null) { throw new NotImplementedException(); }
/// <inheritdoc /> public override IList <TranslationResult> TranslateText(IEnumerable <string> textItems, string targetLanguage, string sourceLanguage = null, TranslationModel?model = null) { var items = ConvertToListAndCheckNoNullElements(textItems, nameof(textItems)); GaxPreconditions.CheckNotNull(targetLanguage, nameof(targetLanguage)); var request = Service.Translations.List(new Repeatable <string>(items), targetLanguage); ModifyRequest(request, sourceLanguage, FormatEnum.Text, model); return(UnpackTranslationResponse(items, sourceLanguage, targetLanguage, request.Execute())); }
/// <inheritdoc /> public override IList <TranslationResult> TranslateHtml(IEnumerable <string> htmlItems, string targetLanguage, string sourceLanguage = null, TranslationModel?model = null) { // Evaluate once so we can zip suitably. List <string> items = htmlItems.ToList(); var request = Service.Translations.List(new Repeatable <string>(items), targetLanguage); ModifyRequest(request, sourceLanguage, FormatEnum.Html, model); return(request.Execute().Translations .Zip(items, (result, item) => new TranslationResult(result, item, sourceLanguage, targetLanguage)) .ToList()); }