You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
56 lines
1.9 KiB
56 lines
1.9 KiB
import 'package:get/get.dart';
|
|
|
|
import '../../core/utils/logger.dart';
|
|
import 'sparkchain_session.dart';
|
|
|
|
/// 文本机器翻译走讯飞 SDK 的 gtrans,不再直连微软翻译,也不再打自家网关。
|
|
class ServerTranslationService extends GetxService {
|
|
static ServerTranslationService get to => Get.find<ServerTranslationService>();
|
|
|
|
static const _tag = 'ServerTrans';
|
|
|
|
/// 与旧实现保持同样的接口形态。讯飞 SDK 在真正翻译时才初始化。
|
|
bool get isInitialized => true;
|
|
|
|
/// 保留这个方法只为兼容调用方(translation_controller 会先调 initialize)。
|
|
Future<bool> initialize() async => true;
|
|
|
|
/// 最近一次实际使用的服务商 / 服务配置 id,排查"到底用的哪家"时看它。
|
|
final lastProvider = ''.obs;
|
|
final lastSvcId = ''.obs;
|
|
|
|
/// 讯飞 gtrans 不回填检测语种,自动检测时这里保持空。
|
|
final lastDetectedFrom = ''.obs;
|
|
|
|
static const Duration realtimeTimeout = Duration(seconds: 12);
|
|
|
|
Future<String?> translateText({
|
|
required String text,
|
|
required String sourceLanguageCode,
|
|
required String targetLanguageCode,
|
|
Object? cancelToken,
|
|
Duration? timeout = realtimeTimeout,
|
|
bool tryOnDevice = true,
|
|
}) async {
|
|
final src = text.trim();
|
|
if (src.isEmpty) return null;
|
|
if (!tryOnDevice) return null;
|
|
// 签名留给原来的调用方。翻译本身在 SDK 里,不再用取消令牌和超时打网关。
|
|
if (cancelToken != null || timeout != null) {
|
|
Logger.i(_tag, '文本翻译走讯飞 SDK gtrans');
|
|
}
|
|
final onDevice = await SparkChainSession.translate(
|
|
from: sourceLanguageCode,
|
|
to: targetLanguageCode,
|
|
text: src,
|
|
);
|
|
if (onDevice == null) {
|
|
Logger.w(_tag, '讯飞 SDK 没返回译文: $sourceLanguageCode→$targetLanguageCode');
|
|
return null;
|
|
}
|
|
lastProvider.value = 'sparkchain';
|
|
lastSvcId.value = 'sparkchain';
|
|
lastDetectedFrom.value = '';
|
|
return onDevice;
|
|
}
|
|
}
|
|
|