|
| 1 | +// ignore_for_file: use_super_parameters |
| 2 | + |
| 3 | +import 'dart:convert'; |
| 4 | + |
| 5 | +import 'package:crypto/crypto.dart'; |
| 6 | +import 'package:equatable/equatable.dart'; |
| 7 | +import 'package:freezed_annotation/freezed_annotation.dart'; |
| 8 | +import 'package:json_annotation/json_annotation.dart'; |
| 9 | +import 'package:qack/presentation/home/models/base_translation_details.dart'; |
| 10 | + |
| 11 | +part 'youdao_translation.g.dart'; |
| 12 | + |
| 13 | +@JsonSerializable() |
| 14 | +final class YouDaoTranslation extends BaseTranslationDetails { |
| 15 | + YouDaoTranslation({ |
| 16 | + required this.errorCode, |
| 17 | + required this.query, |
| 18 | + required this.translation, |
| 19 | + required this.langPair, |
| 20 | + required this.dict, |
| 21 | + required this.webdict, |
| 22 | + required this.tSpeakUrl, |
| 23 | + required this.speakUrl, |
| 24 | + TranslationStatus status = TranslationStatus.success, |
| 25 | + Exception? exception, |
| 26 | + }) : super( |
| 27 | + srcLanguage: langPair.split('2').first, |
| 28 | + targetLanguage: langPair.split('2').last, |
| 29 | + translatedText: TranslatedText( |
| 30 | + inputText: query, |
| 31 | + outputText: translation.fold( |
| 32 | + '', |
| 33 | + (previousValue, element) => '$previousValue \n $element\n', |
| 34 | + ), |
| 35 | + ), |
| 36 | + status: status, |
| 37 | + exception: exception, |
| 38 | + ); |
| 39 | + |
| 40 | + YouDaoTranslation.loading(String query) |
| 41 | + : this( |
| 42 | + errorCode: '', |
| 43 | + query: query, |
| 44 | + translation: [], |
| 45 | + langPair: '', |
| 46 | + dict: null, |
| 47 | + webdict: null, |
| 48 | + tSpeakUrl: null, |
| 49 | + speakUrl: null, |
| 50 | + exception: null, |
| 51 | + status: TranslationStatus.loading, |
| 52 | + ); |
| 53 | + |
| 54 | + YouDaoTranslation.error( |
| 55 | + Exception e, { |
| 56 | + required String errorCode, |
| 57 | + required String query, |
| 58 | + required String langPair, |
| 59 | + }) : this( |
| 60 | + errorCode: errorCode, |
| 61 | + query: query, |
| 62 | + translation: [], |
| 63 | + langPair: langPair, |
| 64 | + dict: null, |
| 65 | + webdict: null, |
| 66 | + tSpeakUrl: null, |
| 67 | + speakUrl: null, |
| 68 | + exception: e, |
| 69 | + ); |
| 70 | + |
| 71 | + factory YouDaoTranslation.fromJson(Map<String, dynamic> json) => |
| 72 | + _$YouDaoTranslationFromJson(json); |
| 73 | + |
| 74 | + final String errorCode; |
| 75 | + |
| 76 | + final String query; |
| 77 | + final List<String> translation; |
| 78 | + final Map<String, String>? dict; // Dictionary deeplink |
| 79 | + final Map<String, String>? webdict; // Web deeplink |
| 80 | + final String? tSpeakUrl; // Translated text speech URL |
| 81 | + final String? speakUrl; // Source text speech URL |
| 82 | + |
| 83 | + @JsonKey(name: 'l') |
| 84 | + final String langPair; // Language pair, e.g., "en2zh" for English to Chinese |
| 85 | + |
| 86 | + /// This should not be implemented since the toJson method is not used in the |
| 87 | + /// YouDaoTranslation class. |
| 88 | + // Map<String, dynamic> toJson() => _$YouDaoTranslationToJson(this); |
| 89 | +} |
| 90 | + |
| 91 | +@JsonSerializable() |
| 92 | +final class FailedYouDaoTranslation extends BaseTranslationError |
| 93 | + with EquatableMixin { |
| 94 | + const FailedYouDaoTranslation({ |
| 95 | + required this.errorCode, |
| 96 | + required this.langPair, |
| 97 | + }) : super( |
| 98 | + errorMessage: 'YouDao translation failed with error code: $errorCode', |
| 99 | + ); |
| 100 | + |
| 101 | + factory FailedYouDaoTranslation.fromJson(Map<String, dynamic> json) => |
| 102 | + _$FailedYouDaoTranslationFromJson(json); |
| 103 | + |
| 104 | + @JsonKey(name: 'errorCode', includeToJson: false) |
| 105 | + final String errorCode; |
| 106 | + |
| 107 | + // Translation language pair, e.g., "en2zh" for English to Chinese |
| 108 | + @JsonKey(name: 'l', includeToJson: false) |
| 109 | + final String langPair; |
| 110 | + |
| 111 | + @override |
| 112 | + List<Object?> get props => [errorCode, langPair]; |
| 113 | + |
| 114 | + @override |
| 115 | + String toString() => 'YouDaoTranslation err code: $errorCode'; |
| 116 | +} |
| 117 | + |
| 118 | +/// This should not be implemented since the toJson method is not used in the |
| 119 | +/// YouDaoTranslation class. |
| 120 | +@override |
| 121 | +List<Map<String, String>> toJson(TranslatedText object) { |
| 122 | + throw UnimplementedError(); |
| 123 | +} |
| 124 | + |
| 125 | +/// {@template youdao_translation_request} |
| 126 | +/// Request model for YouDao translation API. |
| 127 | +/// {@endtemplate} |
| 128 | +@JsonSerializable() |
| 129 | +final class YouDaoTranslationRequest { |
| 130 | + /// {@macro youdao_translation_request} |
| 131 | + const YouDaoTranslationRequest({ |
| 132 | + required this.inputText, |
| 133 | + required this.srcLanguage, |
| 134 | + required this.targetLanguage, |
| 135 | + required this.appID, |
| 136 | + required this.salt, |
| 137 | + required this.secondsSinceEpoch, |
| 138 | + this.signType = 'v3', |
| 139 | + }); |
| 140 | + |
| 141 | + @JsonKey(name: 'q') |
| 142 | + final String inputText; |
| 143 | + |
| 144 | + @JsonKey(name: 'from') |
| 145 | + final String srcLanguage; |
| 146 | + @JsonKey(name: 'to') |
| 147 | + final String targetLanguage; |
| 148 | + |
| 149 | + @JsonKey(name: 'appKey') |
| 150 | + final String appID; |
| 151 | + |
| 152 | + @JsonKey(name: 'salt') |
| 153 | + final String salt; |
| 154 | + |
| 155 | + @JsonKey(name: 'signType') |
| 156 | + final String signType; |
| 157 | + |
| 158 | + @JsonKey(name: 'curtime') |
| 159 | + final int secondsSinceEpoch; |
| 160 | + |
| 161 | + /// Creates a sign for the YouDao translation request. |
| 162 | + static String createSignJson({ |
| 163 | + required String appID, |
| 164 | + required String inputText, |
| 165 | + required String salt, |
| 166 | + required int secondsSinceEpoch, |
| 167 | + required String secretKey, |
| 168 | + }) { |
| 169 | + // According to the YouDao API docs, if the inputText is longer than 20 |
| 170 | + // characters, the input should be constructed as inputText |
| 171 | + // first 10 characters + inputText length + inputText last 10 characters. |
| 172 | + |
| 173 | + late final String editedInputText; |
| 174 | + |
| 175 | + if (inputText.length > 20) { |
| 176 | + editedInputText = '${inputText.substring(0, 10)}${inputText.length}' |
| 177 | + '${inputText.substring(inputText.length - 10)}'; |
| 178 | + } else { |
| 179 | + editedInputText = inputText; |
| 180 | + } |
| 181 | + |
| 182 | + print( |
| 183 | + 'YouDao sign: $appID$editedInputText$salt$secondsSinceEpoch$secretKey', |
| 184 | + ); |
| 185 | + |
| 186 | + final sign = '$appID$editedInputText$salt$secondsSinceEpoch$secretKey'; |
| 187 | + |
| 188 | + final hashedSign = utf8.encode(sign); |
| 189 | + |
| 190 | + return sha256.convert(hashedSign).toString(); |
| 191 | + } |
| 192 | + |
| 193 | + Map<String, dynamic> toJson() => _$YouDaoTranslationRequestToJson(this); |
| 194 | +} |
0 commit comments