You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
97 lines
2.2 KiB
Dart
97 lines
2.2 KiB
Dart
//Question for Jordan - is this for an individual token or could it be a span?
|
|
|
|
import 'dart:convert';
|
|
|
|
import 'package:http/http.dart';
|
|
|
|
import '../config/environment.dart';
|
|
import '../constants/model_keys.dart';
|
|
import '../network/requests.dart';
|
|
import '../network/urls.dart';
|
|
|
|
class FullTextTranslationRepo {
|
|
static Future<FullTextTranslationResponseModel> translate({
|
|
required String accessToken,
|
|
required FullTextTranslationRequestModel request,
|
|
}) async {
|
|
final Requests req = Requests(
|
|
choreoApiKey: Environment.choreoApiKey,
|
|
accessToken: accessToken,
|
|
);
|
|
|
|
final Response res = await req.post(
|
|
url: PApiUrls.simpleTranslation,
|
|
body: request.toJson(),
|
|
);
|
|
|
|
return FullTextTranslationResponseModel.fromJson(
|
|
jsonDecode(utf8.decode(res.bodyBytes)),
|
|
);
|
|
}
|
|
}
|
|
|
|
class FullTextTranslationRequestModel {
|
|
String text;
|
|
String? srcLang;
|
|
String tgtLang;
|
|
String userL1;
|
|
String userL2;
|
|
bool? deepL;
|
|
int? offset;
|
|
int? length;
|
|
|
|
FullTextTranslationRequestModel({
|
|
required this.text,
|
|
this.srcLang,
|
|
required this.tgtLang,
|
|
required this.userL2,
|
|
required this.userL1,
|
|
this.deepL = false,
|
|
this.offset,
|
|
this.length,
|
|
});
|
|
|
|
//PTODO throw error for null
|
|
|
|
Map<String, dynamic> toJson() => {
|
|
"text": text,
|
|
ModelKey.srcLang: srcLang,
|
|
ModelKey.tgtLang: tgtLang,
|
|
ModelKey.userL2: userL2,
|
|
ModelKey.userL1: userL1,
|
|
ModelKey.deepL: deepL,
|
|
ModelKey.offset: offset,
|
|
ModelKey.length: length,
|
|
};
|
|
}
|
|
|
|
class FullTextTranslationResponseModel {
|
|
List<String> translations;
|
|
|
|
/// detected source
|
|
/// PTODO -
|
|
String source;
|
|
String? deepL;
|
|
|
|
FullTextTranslationResponseModel({
|
|
required this.translations,
|
|
required this.source,
|
|
required this.deepL,
|
|
});
|
|
|
|
factory FullTextTranslationResponseModel.fromJson(Map<String, dynamic> json) {
|
|
return FullTextTranslationResponseModel(
|
|
translations: (json["translations"] as Iterable)
|
|
.map<String>(
|
|
(e) => e,
|
|
)
|
|
.toList()
|
|
.cast<String>(),
|
|
source: json[ModelKey.srcLang],
|
|
deepL: json['deepl_res'],
|
|
);
|
|
}
|
|
|
|
String get bestTranslation => deepL ?? translations.first;
|
|
}
|