translateRaw method

  1. @experimental
DynamiteRawResponse<TranslationApiTranslateResponseApplicationJson, void> translateRaw({
  1. required String text,
  2. required String toLanguage,
  3. String? fromLanguage,
  4. bool? oCSAPIRequest,
})

Translate a text.

This method and the response it returns is experimental. The API might change without a major version bump.

Returns a Future containing a DynamiteRawResponse with the raw HttpClientResponse and serialization helpers. Throws a DynamiteApiException if the API call does not return an expected status code.

Parameters:

  • text Text to be translated.
  • fromLanguage Language to translate from.
  • toLanguage Language to translate to.
  • oCSAPIRequest Required to be true for the API request to pass. Defaults to true.

Status codes:

  • 200: Translated text returned
  • 400: Language not detected or unable to translate
  • 412: Translating is not possible
  • 500

See:

Implementation

@experimental
DynamiteRawResponse<TranslationApiTranslateResponseApplicationJson, void> translateRaw({
  required String text,
  required String toLanguage,
  String? fromLanguage,
  bool? oCSAPIRequest,
}) {
  final _parameters = <String, dynamic>{};
  final _headers = <String, String>{
    'Accept': 'application/json',
  };

// coverage:ignore-start
  final authentication = _rootClient.authentications.firstWhereOrNull(
    (auth) => switch (auth) {
      DynamiteHttpBearerAuthentication() || DynamiteHttpBasicAuthentication() => true,
      _ => false,
    },
  );

  if (authentication != null) {
    _headers.addAll(
      authentication.headers,
    );
  }

// coverage:ignore-end
  final $text = jsonSerializers.serialize(text, specifiedType: const FullType(String));
  _parameters['text'] = $text;

  final $toLanguage = jsonSerializers.serialize(toLanguage, specifiedType: const FullType(String));
  _parameters['toLanguage'] = $toLanguage;

  final $fromLanguage = jsonSerializers.serialize(fromLanguage, specifiedType: const FullType(String));
  _parameters['fromLanguage'] = $fromLanguage;

  var $oCSAPIRequest = jsonSerializers.serialize(oCSAPIRequest, specifiedType: const FullType(bool));
  $oCSAPIRequest ??= true;
  _headers['OCS-APIRequest'] = const dynamite_utils.HeaderEncoder().convert($oCSAPIRequest);

  final _path =
      UriTemplate('/ocs/v2.php/translation/translate{?text*,toLanguage*,fromLanguage*}').expand(_parameters);
  return DynamiteRawResponse<TranslationApiTranslateResponseApplicationJson, void>(
    response: _rootClient.executeRequest(
      'post',
      _path,
      _headers,
      null,
      const {200},
    ),
    bodyType: const FullType(TranslationApiTranslateResponseApplicationJson),
    headersType: null,
    serializers: jsonSerializers,
  );
}