import 'dart:convert'; import 'dart:ui'; import 'package:flutter/cupertino.dart'; import 'modules/index.dart'; import 'types.dart' show FTrStr, IPropQueryable, ModuleBase, StringLocaleExtensions; import 'types.dart' as t show StringMapUtils; /// 翻译字典 class FTrKeybook implements IPropQueryable { FTrKeybook._( this.locale, { required this.auth, required this.bodyParts, required this.common, required this.homePage, required this.remedical, required this.profileComplete, required this.setting, required this.user, required this.organization, required this.measure, required this.upload, required this.errorCodes, required this.realTimeConsultation, required this.notification, required this.calendar, required this.laboratory, required this.live, required this.remoteControl, required this.college, this.moduleMap, }); /// 当前是否中文 bool get isCurrentChinese => locale.languageCode.toLowerCase() == "zh"; ///当前是否英文 bool get isCurrentEnglish => locale.languageCode.toLowerCase() == "en"; ///当前是否俄语 bool get isCurrentRussian => locale.languageCode.toLowerCase() == "ru"; ///当前是否西班牙语 bool get isCurrentSpanish => locale.languageCode.toLowerCase() == "es"; ///当前是否是罗纳尼亚语 bool get isCurrentRomania => locale.languageCode.toLowerCase() == "ro"; Map? moduleMap; /// 字典语言 final Locale locale; /// 授权模块 final AuthModule auth; /// 身体部位模块 final BodyPartsModule bodyParts; /// 首页模块 final HomePageModule homePage; /// 公共模块 final CommonModule common; /// 用户资料完善模块 final ProfileCompleteModule profileComplete; /// 设置模块 final SettingModule setting; /// 用户模块 final UserModule user; /// 诊断模块 final RemedicalModule remedical; // 组织模块 final OrganizationModule organization; // 测量模块 final MeasureModule measure; // 上传模块 final UploadModule upload; // 实时会诊模块 final RealTimeConsultationModule realTimeConsultation; // 通知模块 final NotificationModule notification; // 日历模块 final CalendarModule calendar; // 实验室模块 final LaboratoryModule laboratory; // 直播模块 final LiveModule live; ///远程控制 final RemoteControl remoteControl; ///杏聆荟学院 final College college; // 错误码模块 final ErrorCodesModule errorCodes; /// 翻译字典 - 通过JsonMap构造 /// /// [locale] 语言 /// /// [map] 字典集 factory FTrKeybook(Locale locale, dynamic map) { if (map == null || !(map is Map)) throw ArgumentError( "FTrKeybook .ctor: argument-map must be a not null Map."); return FTrKeybook._( locale, auth: map.pick(AuthModule.ModuleName, (v) => AuthModule(v)), bodyParts: map.pick(BodyPartsModule.ModuleName, (v) => BodyPartsModule(v)), common: map.pick(CommonModule.ModuleName, (v) => CommonModule(v)), homePage: map.pick(HomePageModule.ModuleName, (v) => HomePageModule(v)), profileComplete: map.pick( ProfileCompleteModule.ModuleName, (v) => ProfileCompleteModule(v)), setting: map.pick(SettingModule.ModuleName, (v) => SettingModule(v)), user: map.pick(UserModule.ModuleName, (v) => UserModule(v)), remedical: map.pick(RemedicalModule.ModuleName, (v) => RemedicalModule(v)), organization: map.pick(OrganizationModule.ModuleName, (v) => OrganizationModule(v)), measure: map.pick(MeasureModule.ModuleName, (v) => MeasureModule(v)), errorCodes: map.pick(ErrorCodesModule.ModuleName, (v) => ErrorCodesModule(v)), upload: map.pick(UploadModule.ModuleName, (v) => UploadModule(v)), realTimeConsultation: map.pick(RealTimeConsultationModule.ModuleName, (v) => RealTimeConsultationModule(v)), notification: map.pick(NotificationModule.ModuleName, (v) => NotificationModule(v)), calendar: map.pick(CalendarModule.ModuleName, (v) => CalendarModule(v)), laboratory: map.pick(LaboratoryModule.ModuleName, (v) => LaboratoryModule(v)), live: map.pick(LiveModule.ModuleName, (v) => LiveModule(v)), remoteControl: map.pick(RemoteControl.ModuleName, (v) => RemoteControl(v)), college: map.pick(College.ModuleName, (v) => College(v)), moduleMap: map, ); } /// 通过json字符串构造创建字典 static FTrKeybook createFromJson(String text) { final map = jsonDecode(text); final localeStr = map['locale']; if (localeStr == null) throw ArgumentError( "FTrKeybook: create from json error - item `locale` is not defined."); final locale = (localeStr as String).toLocale(); if (locale == null) throw ArgumentError( "FTrKeybook: create from json error - item `locale` is not valid."); return FTrKeybook(locale, map); } @override dynamic getProp(String propName) { switch (propName) { case AuthModule.ModuleName: return auth; case CommonModule.ModuleName: return common; } return null; } /// 获取翻译单元 /// /// [moduleName] 模块名 /// /// [key] 翻译键 FTrStr? getTrItem(String moduleName, String key) { if (moduleMap?.containsKey(moduleName) == false) { return null; } final map = moduleMap![moduleName]! as Map; final item = t.StringMapUtils.pickTrStr(map, key); return item; } } typedef T _ModuleBuilder(Map v); extension _TrMapPickExtension on Map { /// 剥取翻译值 /// /// [key] 字典键 /// /// [defaultValue] 默认翻译 T pick(String key, _ModuleBuilder builder) { dynamic value = this[key]; if (value == null || !(value is Map)) throw UnimplementedError( 'translation keybook is not complete: Module - $key.'); return builder(value); } }