key_book.dart 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. import 'dart:convert';
  2. import 'dart:ui';
  3. import 'package:flutter/cupertino.dart';
  4. import 'modules/index.dart';
  5. import 'types.dart'
  6. show FTrStr, IPropQueryable, ModuleBase, StringLocaleExtensions;
  7. import 'types.dart' as t show StringMapUtils;
  8. /// 翻译字典
  9. class FTrKeybook implements IPropQueryable {
  10. FTrKeybook._(
  11. this.locale, {
  12. required this.auth,
  13. required this.bodyParts,
  14. required this.common,
  15. required this.homePage,
  16. required this.remedical,
  17. required this.profileComplete,
  18. required this.setting,
  19. required this.user,
  20. required this.organization,
  21. required this.measure,
  22. required this.upload,
  23. required this.errorCodes,
  24. required this.realTimeConsultation,
  25. required this.notification,
  26. required this.calendar,
  27. required this.laboratory,
  28. required this.live,
  29. required this.remoteControl,
  30. required this.college,
  31. this.moduleMap,
  32. });
  33. /// 当前是否中文
  34. bool get isCurrentChinese => locale.languageCode.toLowerCase() == "zh";
  35. ///当前是否英文
  36. bool get isCurrentEnglish => locale.languageCode.toLowerCase() == "en";
  37. ///当前是否俄语
  38. bool get isCurrentRussian => locale.languageCode.toLowerCase() == "ru";
  39. ///当前是否西班牙语
  40. bool get isCurrentSpanish => locale.languageCode.toLowerCase() == "es";
  41. ///当前是否是罗纳尼亚语
  42. bool get isCurrentRomania => locale.languageCode.toLowerCase() == "ro";
  43. Map<String, dynamic>? moduleMap;
  44. /// 字典语言
  45. final Locale locale;
  46. /// 授权模块
  47. final AuthModule auth;
  48. /// 身体部位模块
  49. final BodyPartsModule bodyParts;
  50. /// 首页模块
  51. final HomePageModule homePage;
  52. /// 公共模块
  53. final CommonModule common;
  54. /// 用户资料完善模块
  55. final ProfileCompleteModule profileComplete;
  56. /// 设置模块
  57. final SettingModule setting;
  58. /// 用户模块
  59. final UserModule user;
  60. /// 诊断模块
  61. final RemedicalModule remedical;
  62. // 组织模块
  63. final OrganizationModule organization;
  64. // 测量模块
  65. final MeasureModule measure;
  66. // 上传模块
  67. final UploadModule upload;
  68. // 实时会诊模块
  69. final RealTimeConsultationModule realTimeConsultation;
  70. // 通知模块
  71. final NotificationModule notification;
  72. // 日历模块
  73. final CalendarModule calendar;
  74. // 实验室模块
  75. final LaboratoryModule laboratory;
  76. // 直播模块
  77. final LiveModule live;
  78. ///远程控制
  79. final RemoteControl remoteControl;
  80. ///杏聆荟学院
  81. final College college;
  82. // 错误码模块
  83. final ErrorCodesModule errorCodes;
  84. /// 翻译字典 - 通过JsonMap构造
  85. ///
  86. /// [locale] 语言
  87. ///
  88. /// [map] 字典集
  89. factory FTrKeybook(Locale locale, dynamic map) {
  90. if (map == null || !(map is Map<String, dynamic>))
  91. throw ArgumentError(
  92. "FTrKeybook .ctor: argument-map must be a not null Map<String, dynamic>.");
  93. return FTrKeybook._(
  94. locale,
  95. auth: map.pick(AuthModule.ModuleName, (v) => AuthModule(v)),
  96. bodyParts:
  97. map.pick(BodyPartsModule.ModuleName, (v) => BodyPartsModule(v)),
  98. common: map.pick(CommonModule.ModuleName, (v) => CommonModule(v)),
  99. homePage: map.pick(HomePageModule.ModuleName, (v) => HomePageModule(v)),
  100. profileComplete: map.pick(
  101. ProfileCompleteModule.ModuleName, (v) => ProfileCompleteModule(v)),
  102. setting: map.pick(SettingModule.ModuleName, (v) => SettingModule(v)),
  103. user: map.pick(UserModule.ModuleName, (v) => UserModule(v)),
  104. remedical:
  105. map.pick(RemedicalModule.ModuleName, (v) => RemedicalModule(v)),
  106. organization:
  107. map.pick(OrganizationModule.ModuleName, (v) => OrganizationModule(v)),
  108. measure: map.pick(MeasureModule.ModuleName, (v) => MeasureModule(v)),
  109. errorCodes:
  110. map.pick(ErrorCodesModule.ModuleName, (v) => ErrorCodesModule(v)),
  111. upload: map.pick(UploadModule.ModuleName, (v) => UploadModule(v)),
  112. realTimeConsultation: map.pick(RealTimeConsultationModule.ModuleName,
  113. (v) => RealTimeConsultationModule(v)),
  114. notification:
  115. map.pick(NotificationModule.ModuleName, (v) => NotificationModule(v)),
  116. calendar: map.pick(CalendarModule.ModuleName, (v) => CalendarModule(v)),
  117. laboratory:
  118. map.pick(LaboratoryModule.ModuleName, (v) => LaboratoryModule(v)),
  119. live: map.pick(LiveModule.ModuleName, (v) => LiveModule(v)),
  120. remoteControl:
  121. map.pick(RemoteControl.ModuleName, (v) => RemoteControl(v)),
  122. college: map.pick(College.ModuleName, (v) => College(v)),
  123. moduleMap: map,
  124. );
  125. }
  126. /// 通过json字符串构造创建字典
  127. static FTrKeybook createFromJson(String text) {
  128. final map = jsonDecode(text);
  129. final localeStr = map['locale'];
  130. if (localeStr == null)
  131. throw ArgumentError(
  132. "FTrKeybook: create from json error - item `locale` is not defined.");
  133. final locale = (localeStr as String).toLocale();
  134. if (locale == null)
  135. throw ArgumentError(
  136. "FTrKeybook: create from json error - item `locale` is not valid.");
  137. return FTrKeybook(locale, map);
  138. }
  139. @override
  140. dynamic getProp(String propName) {
  141. switch (propName) {
  142. case AuthModule.ModuleName:
  143. return auth;
  144. case CommonModule.ModuleName:
  145. return common;
  146. }
  147. return null;
  148. }
  149. /// 获取翻译单元
  150. ///
  151. /// [moduleName] 模块名
  152. ///
  153. /// [key] 翻译键
  154. FTrStr? getTrItem(String moduleName, String key) {
  155. if (moduleMap?.containsKey(moduleName) == false) {
  156. return null;
  157. }
  158. final map = moduleMap![moduleName]! as Map<String, dynamic>;
  159. final item = t.StringMapUtils.pickTrStr(map, key);
  160. return item;
  161. }
  162. }
  163. typedef T _ModuleBuilder<T>(Map<String, dynamic> v);
  164. extension _TrMapPickExtension on Map<String, dynamic> {
  165. /// 剥取翻译值
  166. ///
  167. /// [key] 字典键
  168. ///
  169. /// [defaultValue] 默认翻译
  170. T pick<T extends ModuleBase>(String key, _ModuleBuilder<T> builder) {
  171. dynamic value = this[key];
  172. if (value == null || !(value is Map<String, dynamic>))
  173. throw UnimplementedError(
  174. 'translation keybook is not complete: Module - $key.');
  175. return builder(value);
  176. }
  177. }