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

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

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

Loading…
Cancel
Save