From 48d38652bf7770c9d060b6790413dd2f34087d6e Mon Sep 17 00:00:00 2001 From: lifegpc Date: Tue, 29 Aug 2023 14:06:23 +0800 Subject: [PATCH] Add i18n support --- l10n.yaml | 3 +++ lib/l10n/app_en.arb | 3 +++ lib/l10n/app_zh.arb | 3 +++ lib/main.dart | 17 ++++++++++++++--- lib/utils.dart | 4 ++++ pubspec.lock | 21 +++++++++++++++++---- pubspec.yaml | 4 ++++ 7 files changed, 48 insertions(+), 7 deletions(-) create mode 100644 l10n.yaml create mode 100644 lib/l10n/app_en.arb create mode 100644 lib/l10n/app_zh.arb create mode 100644 lib/utils.dart diff --git a/l10n.yaml b/l10n.yaml new file mode 100644 index 0000000..15338f2 --- /dev/null +++ b/l10n.yaml @@ -0,0 +1,3 @@ +arb-dir: lib/l10n +template-arb-file: app_en.arb +output-localization-file: app_localizations.dart diff --git a/lib/l10n/app_en.arb b/lib/l10n/app_en.arb new file mode 100644 index 0000000..d623046 --- /dev/null +++ b/lib/l10n/app_en.arb @@ -0,0 +1,3 @@ +{ + "title": "E-Hentai Downloader Dashboard" +} diff --git a/lib/l10n/app_zh.arb b/lib/l10n/app_zh.arb new file mode 100644 index 0000000..d79a9d0 --- /dev/null +++ b/lib/l10n/app_zh.arb @@ -0,0 +1,3 @@ +{ + "title": "E-Hentai 下载器面板" +} \ No newline at end of file diff --git a/lib/main.dart b/lib/main.dart index bb2b87f..550e51a 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,11 +1,12 @@ -import 'dart:io' show Platform; import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; +import 'package:flutter_gen/gen_l10n/app_localizations.dart'; import 'package:go_router/go_router.dart'; import 'package:window_manager/window_manager.dart'; import 'globals.dart'; import 'home.dart'; import 'set_server.dart'; +import 'utils.dart'; final _router = GoRouter( routes: [ @@ -23,7 +24,7 @@ final _router = GoRouter( void main() async { if (!kIsWeb) await prepareJar(); await preparePrefs(); - if (!kIsWeb && (Platform.isWindows || Platform.isLinux || Platform.isMacOS)) { + if (isDesktop) { WidgetsFlutterBinding.ensureInitialized(); await windowManager.ensureInitialized(); await windowManager.setTitle("E-Hentai Downloader Dashboard"); @@ -37,6 +38,16 @@ class MainApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp.router( - routerConfig: _router, title: "E-Hentai Downloader Dashboard"); + routerConfig: _router, + onGenerateTitle: (context) { + final title = AppLocalizations.of(context).title; + if (isDesktop) { + windowManager.setTitle(title); + } + return title; + }, + localizationsDelegates: AppLocalizations.localizationsDelegates, + supportedLocales: AppLocalizations.supportedLocales, + ); } } diff --git a/lib/utils.dart b/lib/utils.dart new file mode 100644 index 0000000..a4ad4ba --- /dev/null +++ b/lib/utils.dart @@ -0,0 +1,4 @@ +import 'dart:io' show Platform; +import 'package:flutter/foundation.dart'; + +bool get isDesktop => !kIsWeb && (Platform.isWindows || Platform.isLinux || Platform.isMacOS); diff --git a/pubspec.lock b/pubspec.lock index c7638c7..9cd3b79 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -5,18 +5,18 @@ packages: dependency: transitive description: name: _fe_analyzer_shared - sha256: eb376e9acf6938204f90eb3b1f00b578640d3188b4c8a8ec054f9f479af8d051 + sha256: ae92f5d747aee634b87f89d9946000c2de774be1d6ac3e58268224348cd0101a url: "https://pub.dev" source: hosted - version: "64.0.0" + version: "61.0.0" analyzer: dependency: transitive description: name: analyzer - sha256: "69f54f967773f6c26c7dcb13e93d7ccee8b17a641689da39e878d5cf13b06893" + sha256: ea3d8652bda62982addfd92fdc2d0214e5f82e43325104990d4f4c4a2a313562 url: "https://pub.dev" source: hosted - version: "6.2.0" + version: "5.13.0" args: dependency: transitive description: @@ -262,6 +262,11 @@ packages: url: "https://pub.dev" source: hosted version: "2.0.2" + flutter_localizations: + dependency: "direct main" + description: flutter + source: sdk + version: "0.0.0" flutter_test: dependency: "direct dev" description: flutter @@ -320,6 +325,14 @@ packages: url: "https://pub.dev" source: hosted version: "4.0.2" + intl: + dependency: "direct main" + description: + name: intl + sha256: a3715e3bc90294e971cb7dc063fbf3cd9ee0ebf8604ffeafabd9e6f16abbdbe6 + url: "https://pub.dev" + source: hosted + version: "0.18.0" io: dependency: transitive description: diff --git a/pubspec.yaml b/pubspec.yaml index bfc243c..2fd807b 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -14,7 +14,10 @@ dependencies: flutter: sdk: flutter flutter_hooks: ^0.20.0 + flutter_localizations: + sdk: flutter go_router: ^10.1.0 + intl: any json_annotation: ^4.8.1 path_provider: ^2.1.0 retrofit: ^4.0.1 @@ -30,4 +33,5 @@ dev_dependencies: json_serializable: ^6.7.1 flutter: + generate: true uses-material-design: true