From 99cf9eb75904153967b5c0dea8ede2c848ff5518 Mon Sep 17 00:00:00 2001 From: lxm <1274844347@qq.com> Date: Mon, 27 Oct 2025 17:45:43 +0800 Subject: [PATCH] =?UTF-8?q?refactor(translation):=20=E4=BC=98=E5=8C=96?= =?UTF-8?q?=E7=BF=BB=E8=AF=91=E6=9C=8D=E5=8A=A1=E5=88=9D=E5=A7=8B=E5=8C=96?= =?UTF-8?q?=E5=92=8CUI=E7=BB=84=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 改进火山翻译服务初始化逻辑,优先使用AppConfig配置并添加错误日志 调整翻译页面UI组件样式和布局,包括返回按钮图标和语言选择器 --- .../services/volcano_translation_service.dart | 44 +++++-- .../views/text_translation_view.dart | 123 +++++++++--------- 2 files changed, 97 insertions(+), 70 deletions(-) diff --git a/lib/data/services/volcano_translation_service.dart b/lib/data/services/volcano_translation_service.dart index 755d1439d..14af7c856 100644 --- a/lib/data/services/volcano_translation_service.dart +++ b/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 initialize({ - String? accessKey, - String? secretKey, - String? region, - }) async { + Future initialize() async { try { - // final _env = _storage.read("ENV") as Map; - _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; } } diff --git a/lib/modules/text_translation/views/text_translation_view.dart b/lib/modules/text_translation/views/text_translation_view.dart index 294046c79..c652f1cd6 100644 --- a/lib/modules/text_translation/views/text_translation_view.dart +++ b/lib/modules/text_translation/views/text_translation_view.dart @@ -24,15 +24,15 @@ class TextTranslationView extends GetView { // 语言选择区域 _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 { 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 { 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 { ), ), ), - + 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 { /// 构建翻译按钮 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(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(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 { 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),