Browse Source

refactor(translation): 优化翻译服务初始化和UI组件

改进火山翻译服务初始化逻辑,优先使用AppConfig配置并添加错误日志
调整翻译页面UI组件样式和布局,包括返回按钮图标和语言选择器
weicu
lxm 12 months ago
parent
commit
c8be164a9a
  1. 44
      lib/data/services/volcano_translation_service.dart
  2. 123
      lib/modules/text_translation/views/text_translation_view.dart

44
lib/data/services/volcano_translation_service.dart

@ -9,6 +9,7 @@ import 'package:get/get.dart';
import '../../core/utils/logger.dart';
import 'language_manager.dart';
//// 火山引擎机器翻译服务
class VolcanoTranslationService extends GetxService {
// API 配置
late String _baseUrl;
@ -28,29 +29,44 @@ class VolcanoTranslationService extends GetxService {
VolcanoTranslationService();
// 初始化服务
Future<bool> initialize({
String? accessKey,
String? secretKey,
String? region,
}) async {
Future<bool> initialize() async {
try {
// final _env = _storage.read("ENV") as Map<String, String>;
_accessKey =
accessKey ?? AppConfig.env('VOLCANO_TRANSLATION_ACCESS_KEY') ?? '';
_secretKey =
secretKey ?? AppConfig.env('VOLCANO_TRANSLATION_SECRET_KEY') ?? '';
_region =
region ?? AppConfig.env('VOLCANO_TRANSLATION_REGION') ?? 'cn-north-1';
// 优先使用后端 AppConfig,其次回退到 .env
final bool appConfigReady = AppConfig.isInitialized();
final accessKeyFromConfig = appConfigReady
? AppConfig.env('VOLCANO_TRANSLATION_ACCESS_KEY')
: null;
final secretKeyFromConfig = appConfigReady
? AppConfig.env('VOLCANO_TRANSLATION_SECRET_KEY')
: null;
final regionFromConfig =
appConfigReady ? AppConfig.env('VOLCANO_TRANSLATION_REGION') : null;
_accessKey = accessKeyFromConfig ??
dotenv.env['VOLCANO_TRANSLATION_ACCESS_KEY'] ??
'';
_secretKey = secretKeyFromConfig ??
dotenv.env['VOLCANO_TRANSLATION_SECRET_KEY'] ??
'';
_region = regionFromConfig ??
dotenv.env['VOLCANO_TRANSLATION_REGION'] ??
'cn-north-1';
_service = 'translate';
_baseUrl = 'https://translate.volcengineapi.com';
if (_accessKey.isEmpty || _secretKey.isEmpty) {
Logger.error(
'lxm---Volcano Translation Service initialized失败,accessKey或secretKey为空');
return false;
}
_isInitialized = true;
Logger.info('lxm---Volcano Translation Service initialized成功');
return true;
} catch (e) {
Logger.error('lxm---Volcano Translation Service initialized失败,$e');
return false;
}
}
@ -142,6 +158,7 @@ class VolcanoTranslationService extends GetxService {
required String targetLanguageCode,
}) async {
if (!_isInitialized) {
Logger.error('翻译服务未初始化');
return null;
}
@ -246,8 +263,11 @@ class VolcanoTranslationService extends GetxService {
return translation;
}
Logger.error(
'翻译HTTP错误: status=${response.statusCode}, body=${response.body}');
return null;
} catch (e) {
Logger.error('翻译请求异常: $e');
return null;
}
}

123
lib/modules/text_translation/views/text_translation_view.dart

@ -24,15 +24,15 @@ class TextTranslationView extends GetView<TextTranslationController> {
// 语言选择区域
_buildLanguageSelector(isDarkMode),
SizedBox(height: 20.h),
// 输入区域
_buildInputSection(isDarkMode),
SizedBox(height: 20.h),
// 翻译按钮
_buildTranslateButton(isDarkMode),
SizedBox(height: 20.h),
// 翻译结果区域
_buildResultSection(isDarkMode),
],
@ -49,8 +49,9 @@ class TextTranslationView extends GetView<TextTranslationController> {
elevation: 0,
leading: IconButton(
icon: Icon(
Icons.arrow_back,
Icons.arrow_back_ios,
color: isDarkMode ? Colors.white : Colors.black,
size: 20.sp,
),
onPressed: () => Get.back(),
),
@ -94,14 +95,15 @@ class TextTranslationView extends GetView<TextTranslationController> {
children: [
// 源语言选择
Obx(() => _buildLanguageButton(
label: 'sourceLanguage'.tr,
selectedLanguage: controller.sourceLanguageChineseName.value,
onTap: () => controller.showLanguageSelector(isSourceLanguage: true),
isDarkMode: isDarkMode,
)),
label: 'sourceLanguage'.tr,
selectedLanguage: controller.sourceLanguageChineseName.value,
onTap: () =>
controller.showLanguageSelector(isSourceLanguage: true),
isDarkMode: isDarkMode,
)),
SizedBox(height: 12.h),
// 交换语言按钮
Center(
child: GestureDetector(
@ -121,16 +123,17 @@ class TextTranslationView extends GetView<TextTranslationController> {
),
),
),
SizedBox(height: 12.h),
// 目标语言选择
Obx(() => _buildLanguageButton(
label: 'targetLanguage'.tr,
selectedLanguage: controller.targetLanguageChineseName.value,
onTap: () => controller.showLanguageSelector(isSourceLanguage: false),
isDarkMode: isDarkMode,
)),
label: 'targetLanguage'.tr,
selectedLanguage: controller.targetLanguageChineseName.value,
onTap: () =>
controller.showLanguageSelector(isSourceLanguage: false),
isDarkMode: isDarkMode,
)),
],
),
);
@ -255,50 +258,53 @@ class TextTranslationView extends GetView<TextTranslationController> {
/// 构建翻译按钮
Widget _buildTranslateButton(bool isDarkMode) {
return Obx(() => SizedBox(
width: double.infinity,
child: ElevatedButton(
onPressed: controller.isTranslating.value ? null : controller.translateText,
style: ElevatedButton.styleFrom(
backgroundColor: Colors.blue,
foregroundColor: Colors.white,
padding: EdgeInsets.symmetric(vertical: 16.h),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(12.r),
),
elevation: 0,
disabledBackgroundColor: Colors.grey,
),
child: controller.isTranslating.value
? Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
SizedBox(
width: 20.w,
height: 20.w,
child: const CircularProgressIndicator(
strokeWidth: 2,
valueColor: AlwaysStoppedAnimation<Color>(Colors.white),
),
),
SizedBox(width: 12.w),
Text(
'translating'.tr,
width: double.infinity,
child: ElevatedButton(
onPressed: controller.isTranslating.value
? null
: controller.translateText,
style: ElevatedButton.styleFrom(
backgroundColor: Colors.blue,
foregroundColor: Colors.white,
padding: EdgeInsets.symmetric(vertical: 16.h),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(12.r),
),
elevation: 0,
disabledBackgroundColor: Colors.grey,
),
child: controller.isTranslating.value
? Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
SizedBox(
width: 20.w,
height: 20.w,
child: const CircularProgressIndicator(
strokeWidth: 2,
valueColor:
AlwaysStoppedAnimation<Color>(Colors.white),
),
),
SizedBox(width: 12.w),
Text(
'translating'.tr,
style: TextStyle(
fontSize: 16.sp,
fontWeight: FontWeight.w600,
),
),
],
)
: Text(
'translate'.tr,
style: TextStyle(
fontSize: 16.sp,
fontWeight: FontWeight.w600,
),
),
],
)
: Text(
'translate'.tr,
style: TextStyle(
fontSize: 16.sp,
fontWeight: FontWeight.w600,
),
),
),
));
),
));
}
/// 构建翻译结果区域
@ -339,7 +345,8 @@ class TextTranslationView extends GetView<TextTranslationController> {
GestureDetector(
onTap: controller.copyResult,
child: Container(
padding: EdgeInsets.symmetric(horizontal: 12.w, vertical: 6.h),
padding:
EdgeInsets.symmetric(horizontal: 12.w, vertical: 6.h),
decoration: BoxDecoration(
color: Colors.blue.withOpacity(0.1),
borderRadius: BorderRadius.circular(6.r),

Loading…
Cancel
Save