Skip to content

Commit 99f437c

Browse files
author
FengChen
committed
[ADD] 国际化适配
1 parent 2d9fec8 commit 99f437c

13 files changed

+274
-6
lines changed

lib/generated/intl/messages_all.dart

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// DO NOT EDIT. This is code generated via package:intl/generate_localized.dart
2+
// This is a library that looks up messages for specific locales by
3+
// delegating to the appropriate library.
4+
5+
// Ignore issues from commonly used lints in this file.
6+
// ignore_for_file:implementation_imports, file_names, unnecessary_new
7+
// ignore_for_file:unnecessary_brace_in_string_interps, directives_ordering
8+
// ignore_for_file:argument_type_not_assignable, invalid_assignment
9+
// ignore_for_file:prefer_single_quotes, prefer_generic_function_type_aliases
10+
// ignore_for_file:comment_references
11+
12+
import 'dart:async';
13+
14+
import 'package:flutter/foundation.dart';
15+
import 'package:intl/intl.dart';
16+
import 'package:intl/message_lookup_by_library.dart';
17+
import 'package:intl/src/intl_helpers.dart';
18+
19+
import 'messages_en.dart' as messages_en;
20+
import 'messages_zh.dart' as messages_zh;
21+
22+
typedef Future<dynamic> LibraryLoader();
23+
Map<String, LibraryLoader> _deferredLibraries = {
24+
'en': () => new SynchronousFuture(null),
25+
'zh': () => new SynchronousFuture(null),
26+
};
27+
28+
MessageLookupByLibrary? _findExact(String localeName) {
29+
switch (localeName) {
30+
case 'en':
31+
return messages_en.messages;
32+
case 'zh':
33+
return messages_zh.messages;
34+
default:
35+
return null;
36+
}
37+
}
38+
39+
/// User programs should call this before using [localeName] for messages.
40+
Future<bool> initializeMessages(String localeName) {
41+
var availableLocale = Intl.verifiedLocale(
42+
localeName, (locale) => _deferredLibraries[locale] != null,
43+
onFailure: (_) => null);
44+
if (availableLocale == null) {
45+
return new SynchronousFuture(false);
46+
}
47+
var lib = _deferredLibraries[availableLocale];
48+
lib == null ? new SynchronousFuture(false) : lib();
49+
initializeInternalMessageLookup(() => new CompositeMessageLookup());
50+
messageLookup.addLocale(availableLocale, _findGeneratedMessagesFor);
51+
return new SynchronousFuture(true);
52+
}
53+
54+
bool _messagesExistFor(String locale) {
55+
try {
56+
return _findExact(locale) != null;
57+
} catch (e) {
58+
return false;
59+
}
60+
}
61+
62+
MessageLookupByLibrary? _findGeneratedMessagesFor(String locale) {
63+
var actualLocale =
64+
Intl.verifiedLocale(locale, _messagesExistFor, onFailure: (_) => null);
65+
if (actualLocale == null) return null;
66+
return _findExact(actualLocale);
67+
}

lib/generated/intl/messages_en.dart

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// DO NOT EDIT. This is code generated via package:intl/generate_localized.dart
2+
// This is a library that provides messages for a en locale. All the
3+
// messages from the main program should be duplicated here with the same
4+
// function name.
5+
6+
// Ignore issues from commonly used lints in this file.
7+
// ignore_for_file:unnecessary_brace_in_string_interps, unnecessary_new
8+
// ignore_for_file:prefer_single_quotes,comment_references, directives_ordering
9+
// ignore_for_file:annotate_overrides,prefer_generic_function_type_aliases
10+
// ignore_for_file:unused_import, file_names, avoid_escaping_inner_quotes
11+
// ignore_for_file:unnecessary_string_interpolations, unnecessary_string_escapes
12+
13+
import 'package:intl/intl.dart';
14+
import 'package:intl/message_lookup_by_library.dart';
15+
16+
final messages = new MessageLookup();
17+
18+
typedef String MessageIfAbsent(String messageStr, List<dynamic> args);
19+
20+
class MessageLookup extends MessageLookupByLibrary {
21+
String get localeName => 'en';
22+
23+
final messages = _notInlinedMessages(_notInlinedMessages);
24+
static Map<String, Function> _notInlinedMessages(_) => <String, Function>{
25+
"bookshelf": MessageLookupByLibrary.simpleMessage("bookshelf"),
26+
"bookstore": MessageLookupByLibrary.simpleMessage("bookstore")
27+
};
28+
}

lib/generated/intl/messages_zh.dart

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// DO NOT EDIT. This is code generated via package:intl/generate_localized.dart
2+
// This is a library that provides messages for a zh locale. All the
3+
// messages from the main program should be duplicated here with the same
4+
// function name.
5+
6+
// Ignore issues from commonly used lints in this file.
7+
// ignore_for_file:unnecessary_brace_in_string_interps, unnecessary_new
8+
// ignore_for_file:prefer_single_quotes,comment_references, directives_ordering
9+
// ignore_for_file:annotate_overrides,prefer_generic_function_type_aliases
10+
// ignore_for_file:unused_import, file_names, avoid_escaping_inner_quotes
11+
// ignore_for_file:unnecessary_string_interpolations, unnecessary_string_escapes
12+
13+
import 'package:intl/intl.dart';
14+
import 'package:intl/message_lookup_by_library.dart';
15+
16+
final messages = new MessageLookup();
17+
18+
typedef String MessageIfAbsent(String messageStr, List<dynamic> args);
19+
20+
class MessageLookup extends MessageLookupByLibrary {
21+
String get localeName => 'zh';
22+
23+
final messages = _notInlinedMessages(_notInlinedMessages);
24+
static Map<String, Function> _notInlinedMessages(_) => <String, Function>{
25+
"bookshelf": MessageLookupByLibrary.simpleMessage("书架"),
26+
"bookstore": MessageLookupByLibrary.simpleMessage("书店")
27+
};
28+
}

lib/generated/l10n.dart

+99
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/l10n/intl_en.arb

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"bookstore": "bookstore",
3+
"bookshelf": "bookshelf"
4+
}

lib/l10n/intl_zh.arb

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"bookstore": "书店",
3+
"bookshelf": "书架"
4+
}

lib/src/app.dart

+16-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
import 'package:adaptive_navigation/adaptive_navigation.dart';
22
import 'package:flutter/material.dart';
3+
import 'package:flutter_localizations/flutter_localizations.dart';
34
import 'package:go_router/go_router.dart';
45
import 'package:reading_flutter/src/screens/bookshelf.dart';
56
import 'package:reading_flutter/src/screens/bookstore.dart';
67
import 'package:reading_flutter/src/screens/reading.dart';
78
import 'package:reading_flutter/src/screens/test.dart';
89

10+
import '../generated/l10n.dart';
11+
912
/// 主页
1013
1114
class Reading extends StatefulWidget {
@@ -21,6 +24,14 @@ class ReadingState extends State<Reading> {
2124
@override
2225
Widget build(BuildContext context) {
2326
return MaterialApp.router(
27+
localizationsDelegates: const [
28+
S.delegate,
29+
GlobalMaterialLocalizations.delegate,
30+
GlobalCupertinoLocalizations.delegate,
31+
GlobalWidgetsLocalizations.delegate
32+
],
33+
supportedLocales: S.delegate.supportedLocales,
34+
// locale: const Locale('en', 'US'), // 可以手动指定当前的语言
2435
routerConfig: GoRouter(
2536
initialLocation: bookstoreRouter,
2637
routes: [
@@ -89,9 +100,11 @@ class HomePage extends StatelessWidget {
89100
goRouter.go(testRouter);
90101
}
91102
},
92-
destinations: const [
93-
AdaptiveScaffoldDestination(title: "书店", icon: Icons.book),
94-
AdaptiveScaffoldDestination(title: "书架", icon: Icons.mark_chat_read),
103+
destinations: [
104+
AdaptiveScaffoldDestination(
105+
title: S.current.bookstore, icon: Icons.book),
106+
AdaptiveScaffoldDestination(
107+
title: S.current.bookshelf, icon: Icons.mark_chat_read),
95108
AdaptiveScaffoldDestination(
96109
title: "Test", icon: Icons.mark_chat_read),
97110
],

lib/src/screens/bookshelf.dart

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import 'package:reading_flutter/src/db/dao/bookChapterDao.dart';
99
import 'package:reading_flutter/src/screens/reading.dart';
1010
import 'package:reading_flutter/src/utils/LocalPageLoaderUtil.dart';
1111

12+
import '../../generated/l10n.dart';
1213
import '../data/bookChapter.dart';
1314
import '../data/bookEntity.dart';
1415
import '../db/dao/bookDap.dart';
@@ -81,7 +82,7 @@ class _BookshelfState extends State<BookshelfScreen> {
8182
@override
8283
Widget build(BuildContext context) {
8384
return Scaffold(
84-
appBar: AppBar(title: const Text("书架"), actions: [
85+
appBar: AppBar(title: Text(S.current.bookshelf), actions: [
8586
// 添加右侧的图标按钮
8687
IconButton(
8788
icon: const Icon(Icons.add),

lib/src/screens/bookstore.dart

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ import 'package:flutter/material.dart';
66
import 'package:http/http.dart';
77
import 'package:provider/provider.dart';
88

9+
import '../../generated/l10n.dart';
910
import '../data/bookEntity.dart';
1011

1112
/// 书店页面
1213
1314
const bookstoreRouter = "/bookstore";
1415

1516
class BookstoreScreen extends StatefulWidget {
16-
1717
//测试方法
1818
Future<List<BookEntity>> fetchBookList() async {
1919
// 在这里实现异步逻辑,例如从网络请求数据
@@ -58,7 +58,7 @@ class _BookstoreState extends State<BookstoreScreen> {
5858
@override
5959
Widget build(BuildContext context) {
6060
return Scaffold(
61-
appBar: AppBar(title: const Text("书店")),
61+
appBar: AppBar(title: Text(S.current.bookstore)),
6262
body: FutureBuilder(
6363
// future: _bookListFuture,
6464
future: _getBooks(),

linux/flutter/generated_plugins.cmake

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ list(APPEND FLUTTER_PLUGIN_LIST
66
)
77

88
list(APPEND FLUTTER_FFI_PLUGIN_LIST
9+
jni
910
)
1011

1112
set(PLUGIN_BUNDLED_LIBRARIES)

pubspec.lock

+13
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,11 @@ packages:
278278
url: "https://pub.dev"
279279
source: hosted
280280
version: "2.0.3"
281+
flutter_localizations:
282+
dependency: "direct main"
283+
description: flutter
284+
source: sdk
285+
version: "0.0.0"
281286
flutter_plugin_android_lifecycle:
282287
dependency: transitive
283288
description:
@@ -376,6 +381,14 @@ packages:
376381
url: "https://pub.dev"
377382
source: hosted
378383
version: "4.0.2"
384+
intl:
385+
dependency: transitive
386+
description:
387+
name: intl
388+
sha256: "3bc132a9dbce73a7e4a21a17d06e1878839ffbf975568bc875c60537824b0c4d"
389+
url: "https://pub.dev"
390+
source: hosted
391+
version: "0.18.1"
379392
io:
380393
dependency: transitive
381394
description:

pubspec.yaml

+9
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ environment:
3030
dependencies:
3131
flutter:
3232
sdk: flutter
33+
34+
flutter_localizations:
35+
sdk: flutter
36+
3337
go_router: ^13.0.1
3438
adaptive_navigation: ^0.0.10
3539
uuid: ^4.3.3
@@ -58,6 +62,9 @@ dev_dependencies:
5862
flutter_test:
5963
sdk: flutter
6064

65+
flutter_localizations:
66+
sdk: flutter
67+
6168
build_runner: ^2.2.1
6269
freezed: ^2.1.1
6370
json_serializable: ^6.4.0
@@ -110,3 +117,5 @@ flutter:
110117
#
111118
# For details regarding fonts from package dependencies,
112119
# see https://flutter.dev/custom-fonts/#from-packages
120+
flutter_intl:
121+
enabled: true

windows/flutter/generated_plugins.cmake

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ list(APPEND FLUTTER_PLUGIN_LIST
66
)
77

88
list(APPEND FLUTTER_FFI_PLUGIN_LIST
9+
jni
910
)
1011

1112
set(PLUGIN_BUNDLED_LIBRARIES)

0 commit comments

Comments
 (0)