Browse Source

国际化的配置

liukai 4 months ago
parent
commit
8ed82cb58d

+ 24 - 4
app/lib/main.dart

@@ -1,6 +1,8 @@
 import 'package:cpt_main/router/component/main_service_provider.dart';
+import 'package:cs_resources/generated/l10n.dart';
 import 'package:flutter/material.dart';
 import 'package:flutter/services.dart';
+import 'package:flutter_localizations/flutter_localizations.dart';
 import 'package:initializer/app_initializer.dart';
 import 'package:plugin_basic/basic_export.dart';
 import 'package:plugin_basic/provider/app_config/app_config_service.dart';
@@ -21,8 +23,7 @@ import 'router/page/app_page_router.dart';
 import 'router/component/app_service_provider.dart';
 
 void main() async {
-  //运行App
-  // FlutterBugly.postCatchedException(() async {
+
   //交给初始化构造器去统一初始化
   await AppInitializer.initializeRunalone();
 
@@ -36,7 +37,7 @@ void main() async {
     parent: globalContainer,
     child: MyApp(),
   ));
-  // });
+
 }
 
 class MyApp extends StatelessWidget {
@@ -95,7 +96,6 @@ class MyApp extends StatelessWidget {
 
   @override
   Widget build(BuildContext context) {
-
     WidgetsBinding.instance.addPostFrameCallback((_) {
       AppConfigService.getInstance().initSize(context);
     });
@@ -114,6 +114,26 @@ class MyApp extends StatelessWidget {
             colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
             useMaterial3: false,
           ),
+          //国际化配置
+          localizationsDelegates: const [
+            S.delegate,
+            GlobalMaterialLocalizations.delegate,
+            GlobalCupertinoLocalizations.delegate,
+            GlobalWidgetsLocalizations.delegate
+          ],
+          //国际化英语为首选项
+          supportedLocales: [const Locale('en', ''), ...S.delegate.supportedLocales],
+          localeResolutionCallback: (locale, supportLocales) {
+            // 中文 简繁体处理
+            if (locale?.languageCode == 'zh') {
+              if (locale?.scriptCode == 'Hant') {
+                return const Locale('zh', 'HK'); //繁体
+              } else {
+                return const Locale('zh', 'CN'); //简体
+              }
+            }
+            return null;
+          },
           //AutoRouter的配置
           routerConfig: appRouter.config(
             navigatorObservers: () => [

+ 7 - 6
packages/cpt_main/lib/modules/home/page/home_page.dart

@@ -1,4 +1,5 @@
 import 'package:cpt_main/modules/visitor/page/visitor_page.dart';
+import 'package:cs_resources/generated/l10n.dart';
 import 'package:flutter/material.dart';
 import 'package:flutter/src/widgets/framework.dart';
 import 'package:hooks_riverpod/hooks_riverpod.dart';
@@ -21,15 +22,15 @@ class HomePage extends HookConsumerWidget {
               children: [
                 ElevatedButton(
                   onPressed: () {},
-                  child: Text('Payment'),
+                  child: Text(S.of(context).payment),  //国际化中英文适配
                 ),
                 ElevatedButton(
                   onPressed: () {},
-                  child: Text('Rewards'),
+                  child: Text(S.of(context).rewards),
                 ),
                 ElevatedButton(
                   onPressed: () {},
-                  child: Text('Notification'),
+                  child: Text(S.of(context).notification),
                 ),
               ],
             ).marginOnly(top: 10),
@@ -38,15 +39,15 @@ class HomePage extends HookConsumerWidget {
               children: [
                 ElevatedButton(
                   onPressed: () {},
-                  child: Text('Facility'),
+                  child: Text(S.of(context).facility),
                 ),
                 ElevatedButton(
                   onPressed: () {},
-                  child: Text('Form'),
+                  child: Text(S.of(context).form),
                 ),
                 ElevatedButton(
                   onPressed: () {},
-                  child: Text('Notice Board'),
+                  child: Text(S.of(context).notice_board),
                 ),
               ],
             ).marginOnly(top: 10),

+ 71 - 0
packages/cs_resources/lib/generated/intl/messages_all.dart

@@ -0,0 +1,71 @@
+// DO NOT EDIT. This is code generated via package:intl/generate_localized.dart
+// This is a library that looks up messages for specific locales by
+// delegating to the appropriate library.
+
+// Ignore issues from commonly used lints in this file.
+// ignore_for_file:implementation_imports, file_names, unnecessary_new
+// ignore_for_file:unnecessary_brace_in_string_interps, directives_ordering
+// ignore_for_file:argument_type_not_assignable, invalid_assignment
+// ignore_for_file:prefer_single_quotes, prefer_generic_function_type_aliases
+// ignore_for_file:comment_references
+
+import 'dart:async';
+
+import 'package:flutter/foundation.dart';
+import 'package:intl/intl.dart';
+import 'package:intl/message_lookup_by_library.dart';
+import 'package:intl/src/intl_helpers.dart';
+
+import 'messages_en.dart' as messages_en;
+import 'messages_zh_CN.dart' as messages_zh_cn;
+import 'messages_zh_HK.dart' as messages_zh_hk;
+
+typedef Future<dynamic> LibraryLoader();
+Map<String, LibraryLoader> _deferredLibraries = {
+  'en': () => new SynchronousFuture(null),
+  'zh_CN': () => new SynchronousFuture(null),
+  'zh_HK': () => new SynchronousFuture(null),
+};
+
+MessageLookupByLibrary? _findExact(String localeName) {
+  switch (localeName) {
+    case 'en':
+      return messages_en.messages;
+    case 'zh_CN':
+      return messages_zh_cn.messages;
+    case 'zh_HK':
+      return messages_zh_hk.messages;
+    default:
+      return null;
+  }
+}
+
+/// User programs should call this before using [localeName] for messages.
+Future<bool> initializeMessages(String localeName) {
+  var availableLocale = Intl.verifiedLocale(
+      localeName, (locale) => _deferredLibraries[locale] != null,
+      onFailure: (_) => null);
+  if (availableLocale == null) {
+    return new SynchronousFuture(false);
+  }
+  var lib = _deferredLibraries[availableLocale];
+  lib == null ? new SynchronousFuture(false) : lib();
+  initializeInternalMessageLookup(() => new CompositeMessageLookup());
+  messageLookup.addLocale(availableLocale, _findGeneratedMessagesFor);
+  return new SynchronousFuture(true);
+}
+
+bool _messagesExistFor(String locale) {
+  try {
+    return _findExact(locale) != null;
+  } catch (e) {
+    return false;
+  }
+}
+
+MessageLookupByLibrary? _findGeneratedMessagesFor(String locale) {
+  var actualLocale =
+      Intl.verifiedLocale(locale, _messagesExistFor, onFailure: (_) => null);
+  if (actualLocale == null) return null;
+  return _findExact(actualLocale);
+}

+ 32 - 0
packages/cs_resources/lib/generated/intl/messages_en.dart

@@ -0,0 +1,32 @@
+// DO NOT EDIT. This is code generated via package:intl/generate_localized.dart
+// This is a library that provides messages for a en locale. All the
+// messages from the main program should be duplicated here with the same
+// function name.
+
+// Ignore issues from commonly used lints in this file.
+// ignore_for_file:unnecessary_brace_in_string_interps, unnecessary_new
+// ignore_for_file:prefer_single_quotes,comment_references, directives_ordering
+// ignore_for_file:annotate_overrides,prefer_generic_function_type_aliases
+// ignore_for_file:unused_import, file_names, avoid_escaping_inner_quotes
+// ignore_for_file:unnecessary_string_interpolations, unnecessary_string_escapes
+
+import 'package:intl/intl.dart';
+import 'package:intl/message_lookup_by_library.dart';
+
+final messages = new MessageLookup();
+
+typedef String MessageIfAbsent(String messageStr, List<dynamic> args);
+
+class MessageLookup extends MessageLookupByLibrary {
+  String get localeName => 'en';
+
+  final messages = _notInlinedMessages(_notInlinedMessages);
+  static Map<String, Function> _notInlinedMessages(_) => <String, Function>{
+        "facility": MessageLookupByLibrary.simpleMessage("Facility"),
+        "form": MessageLookupByLibrary.simpleMessage("Form"),
+        "notice_board": MessageLookupByLibrary.simpleMessage("Notice Board"),
+        "notification": MessageLookupByLibrary.simpleMessage("Notification"),
+        "payment": MessageLookupByLibrary.simpleMessage("Payment"),
+        "rewards": MessageLookupByLibrary.simpleMessage("Rewards")
+      };
+}

+ 32 - 0
packages/cs_resources/lib/generated/intl/messages_zh_CN.dart

@@ -0,0 +1,32 @@
+// DO NOT EDIT. This is code generated via package:intl/generate_localized.dart
+// This is a library that provides messages for a zh_CN locale. All the
+// messages from the main program should be duplicated here with the same
+// function name.
+
+// Ignore issues from commonly used lints in this file.
+// ignore_for_file:unnecessary_brace_in_string_interps, unnecessary_new
+// ignore_for_file:prefer_single_quotes,comment_references, directives_ordering
+// ignore_for_file:annotate_overrides,prefer_generic_function_type_aliases
+// ignore_for_file:unused_import, file_names, avoid_escaping_inner_quotes
+// ignore_for_file:unnecessary_string_interpolations, unnecessary_string_escapes
+
+import 'package:intl/intl.dart';
+import 'package:intl/message_lookup_by_library.dart';
+
+final messages = new MessageLookup();
+
+typedef String MessageIfAbsent(String messageStr, List<dynamic> args);
+
+class MessageLookup extends MessageLookupByLibrary {
+  String get localeName => 'zh_CN';
+
+  final messages = _notInlinedMessages(_notInlinedMessages);
+  static Map<String, Function> _notInlinedMessages(_) => <String, Function>{
+        "facility": MessageLookupByLibrary.simpleMessage("设施"),
+        "form": MessageLookupByLibrary.simpleMessage("表单"),
+        "notice_board": MessageLookupByLibrary.simpleMessage("消息板"),
+        "notification": MessageLookupByLibrary.simpleMessage("通知"),
+        "payment": MessageLookupByLibrary.simpleMessage("支付"),
+        "rewards": MessageLookupByLibrary.simpleMessage("奖励")
+      };
+}

+ 32 - 0
packages/cs_resources/lib/generated/intl/messages_zh_HK.dart

@@ -0,0 +1,32 @@
+// DO NOT EDIT. This is code generated via package:intl/generate_localized.dart
+// This is a library that provides messages for a zh_HK locale. All the
+// messages from the main program should be duplicated here with the same
+// function name.
+
+// Ignore issues from commonly used lints in this file.
+// ignore_for_file:unnecessary_brace_in_string_interps, unnecessary_new
+// ignore_for_file:prefer_single_quotes,comment_references, directives_ordering
+// ignore_for_file:annotate_overrides,prefer_generic_function_type_aliases
+// ignore_for_file:unused_import, file_names, avoid_escaping_inner_quotes
+// ignore_for_file:unnecessary_string_interpolations, unnecessary_string_escapes
+
+import 'package:intl/intl.dart';
+import 'package:intl/message_lookup_by_library.dart';
+
+final messages = new MessageLookup();
+
+typedef String MessageIfAbsent(String messageStr, List<dynamic> args);
+
+class MessageLookup extends MessageLookupByLibrary {
+  String get localeName => 'zh_HK';
+
+  final messages = _notInlinedMessages(_notInlinedMessages);
+  static Map<String, Function> _notInlinedMessages(_) => <String, Function>{
+        "facility": MessageLookupByLibrary.simpleMessage("設施"),
+        "form": MessageLookupByLibrary.simpleMessage("表單"),
+        "notice_board": MessageLookupByLibrary.simpleMessage("通知板"),
+        "notification": MessageLookupByLibrary.simpleMessage("通知"),
+        "payment": MessageLookupByLibrary.simpleMessage("支付"),
+        "rewards": MessageLookupByLibrary.simpleMessage("獎勵")
+      };
+}

+ 140 - 0
packages/cs_resources/lib/generated/l10n.dart

@@ -0,0 +1,140 @@
+// GENERATED CODE - DO NOT MODIFY BY HAND
+import 'package:flutter/material.dart';
+import 'package:intl/intl.dart';
+import 'intl/messages_all.dart';
+
+// **************************************************************************
+// Generator: Flutter Intl IDE plugin
+// Made by Localizely
+// **************************************************************************
+
+// ignore_for_file: non_constant_identifier_names, lines_longer_than_80_chars
+// ignore_for_file: join_return_with_assignment, prefer_final_in_for_each
+// ignore_for_file: avoid_redundant_argument_values, avoid_escaping_inner_quotes
+
+class S {
+  S();
+
+  static S? _current;
+
+  static S get current {
+    assert(_current != null,
+        'No instance of S was loaded. Try to initialize the S delegate before accessing S.current.');
+    return _current!;
+  }
+
+  static const AppLocalizationDelegate delegate = AppLocalizationDelegate();
+
+  static Future<S> load(Locale locale) {
+    final name = (locale.countryCode?.isEmpty ?? false)
+        ? locale.languageCode
+        : locale.toString();
+    final localeName = Intl.canonicalizedLocale(name);
+    return initializeMessages(localeName).then((_) {
+      Intl.defaultLocale = localeName;
+      final instance = S();
+      S._current = instance;
+
+      return instance;
+    });
+  }
+
+  static S of(BuildContext context) {
+    final instance = S.maybeOf(context);
+    assert(instance != null,
+        'No instance of S present in the widget tree. Did you add S.delegate in localizationsDelegates?');
+    return instance!;
+  }
+
+  static S? maybeOf(BuildContext context) {
+    return Localizations.of<S>(context, S);
+  }
+
+  /// `Payment`
+  String get payment {
+    return Intl.message(
+      'Payment',
+      name: 'payment',
+      desc: '',
+      args: [],
+    );
+  }
+
+  /// `Rewards`
+  String get rewards {
+    return Intl.message(
+      'Rewards',
+      name: 'rewards',
+      desc: '',
+      args: [],
+    );
+  }
+
+  /// `Notification`
+  String get notification {
+    return Intl.message(
+      'Notification',
+      name: 'notification',
+      desc: '',
+      args: [],
+    );
+  }
+
+  /// `Facility`
+  String get facility {
+    return Intl.message(
+      'Facility',
+      name: 'facility',
+      desc: '',
+      args: [],
+    );
+  }
+
+  /// `Form`
+  String get form {
+    return Intl.message(
+      'Form',
+      name: 'form',
+      desc: '',
+      args: [],
+    );
+  }
+
+  /// `Notice Board`
+  String get notice_board {
+    return Intl.message(
+      'Notice Board',
+      name: 'notice_board',
+      desc: '',
+      args: [],
+    );
+  }
+}
+
+class AppLocalizationDelegate extends LocalizationsDelegate<S> {
+  const AppLocalizationDelegate();
+
+  List<Locale> get supportedLocales {
+    return const <Locale>[
+      Locale.fromSubtags(languageCode: 'en'),
+      Locale.fromSubtags(languageCode: 'zh', countryCode: 'CN'),
+      Locale.fromSubtags(languageCode: 'zh', countryCode: 'HK'),
+    ];
+  }
+
+  @override
+  bool isSupported(Locale locale) => _isSupported(locale);
+  @override
+  Future<S> load(Locale locale) => S.load(locale);
+  @override
+  bool shouldReload(AppLocalizationDelegate old) => false;
+
+  bool _isSupported(Locale locale) {
+    for (var supportedLocale in supportedLocales) {
+      if (supportedLocale.languageCode == locale.languageCode) {
+        return true;
+      }
+    }
+    return false;
+  }
+}

+ 8 - 0
packages/cs_resources/lib/l10n/intl_en.arb

@@ -0,0 +1,8 @@
+{
+  "payment": "Payment",
+  "rewards": "Rewards",
+  "notification": "Notification",
+  "facility": "Facility",
+  "form": "Form",
+  "notice_board": "Notice Board"
+}

+ 8 - 0
packages/cs_resources/lib/l10n/intl_zh_CN.arb

@@ -0,0 +1,8 @@
+{
+  "payment": "支付",
+  "rewards": "奖励",
+  "notification": "通知",
+  "facility": "设施",
+  "form": "表单",
+  "notice_board": "消息板"
+}

+ 8 - 0
packages/cs_resources/lib/l10n/intl_zh_HK.arb

@@ -0,0 +1,8 @@
+{
+  "payment": "支付",
+  "rewards": "獎勵",
+  "notification": "通知",
+  "facility": "設施",
+  "form": "表單",
+  "notice_board": "通知板"
+}

+ 4 - 0
packages/cs_resources/pubspec.yaml

@@ -27,3 +27,7 @@ flutter:
     - assets/
     - assets/base_lib/
     - assets/base_service/
+
+
+flutter_intl:
+  enabled: true