Skip to content

Commit

Permalink
Support i18n
Browse files Browse the repository at this point in the history
  • Loading branch information
shoothzj committed Jul 31, 2021
1 parent f8cd063 commit 44d687b
Show file tree
Hide file tree
Showing 10 changed files with 121 additions and 1 deletion.
4 changes: 4 additions & 0 deletions lib/l10n/intl_en.arb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"appName": "Paas Dashboard",
"refresh": "Refresh"
}
4 changes: 4 additions & 0 deletions lib/l10n/intl_zh.arb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"appName": "Paas 仪表盘",
"refresh": "刷新"
}
15 changes: 15 additions & 0 deletions lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:paas_dashboard_flutter/generated/l10n.dart';
import 'package:paas_dashboard_flutter/module/pulsar/pulsar_topic.dart';
import 'package:paas_dashboard_flutter/route/page_route_const.dart';
import 'package:paas_dashboard_flutter/route/route_gen.dart';
import 'package:paas_dashboard_flutter/ui/bk/bk_page.dart';
import 'package:paas_dashboard_flutter/ui/general/settings_screen.dart';
import 'package:paas_dashboard_flutter/ui/home/home_page.dart';
import 'package:paas_dashboard_flutter/ui/pulsar/pulsar_page.dart';
import 'package:paas_dashboard_flutter/vm/bk/bk_instance_list_view_model.dart';
import 'package:paas_dashboard_flutter/vm/general/settings_view_model.dart';
import 'package:paas_dashboard_flutter/vm/pulsar/pulsar_instance_list_view_model.dart';
import 'package:paas_dashboard_flutter/vm/pulsar/pulsar_instance_view_model.dart';
import 'package:paas_dashboard_flutter/vm/pulsar/pulsar_namespace_view_model.dart';
Expand All @@ -22,6 +26,13 @@ class MyApp extends StatelessWidget {
Widget build(BuildContext context) {
return MaterialApp(
title: 'Paas Dashboard',
localizationsDelegates: [
S.delegate,
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
],
supportedLocales: S.delegate.supportedLocales,
theme: ThemeData(
// This is the theme of your application.
//
Expand All @@ -37,6 +48,10 @@ class MyApp extends StatelessWidget {
initialRoute: PageRouteConst.Root,
routes: {
PageRouteConst.Root: (context) => HomePage(),
PageRouteConst.Settings: (context) => ChangeNotifierProvider(
create: (context) => SettingsViewModel(),
child: SettingsScreen(),
),
PageRouteConst.Bookkeeper: (context) => ChangeNotifierProvider(
create: (context) => BkInstanceListViewModel(),
child: BkPage(),
Expand Down
1 change: 1 addition & 0 deletions lib/route/page_route_const.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
class PageRouteConst {
static const String Root = '/';
static const String Settings = '/settings';
static const String Bookkeeper = '/bookkeeper';
static const String Pulsar = '/pulsar';
static const String PulsarInstance = '/pulsar/instance';
Expand Down
63 changes: 63 additions & 0 deletions lib/ui/general/settings_screen.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import 'dart:developer';

import 'package:flutter/material.dart';
import 'package:paas_dashboard_flutter/vm/general/settings_view_model.dart';
import 'package:provider/provider.dart';

class SettingsScreen extends StatefulWidget {
SettingsScreen();

@override
State<StatefulWidget> createState() {
return new SettingsScreenState();
}
}

class SettingsScreenState extends State<SettingsScreen> {
@override
void initState() {
super.initState();
}

@override
void dispose() {
super.dispose();
}

@override
Widget build(BuildContext context) {
final vm = Provider.of<SettingsViewModel>(context);
return Scaffold(
appBar: AppBar(
title: Text('Settings'),
),
body: ExpansionTile(
title: Text('Change language'),
leading: Icon(Icons.language),
initiallyExpanded: false,
children: [
RadioListTile(
title: Text('English'),
value: 'en',
groupValue: vm.language,
onChanged: _changed),
RadioListTile(
title: Text('汉语'),
value: 'zh',
groupValue: vm.language,
onChanged: _changed),
],
),
);
}

void _changed(value) {
if (value != null) {
log('change language to $value');
final vm = Provider.of<SettingsViewModel>(context, listen: false);
if (value != null) {
vm.setLan(value);
}
}
}
}
6 changes: 6 additions & 0 deletions lib/ui/home/home_drawer.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:flutter/material.dart';
import 'package:paas_dashboard_flutter/route/page_route_const.dart';

class NavDrawer extends StatelessWidget {
@override
Expand All @@ -23,6 +24,11 @@ class NavDrawer extends StatelessWidget {
title: Text('About author'),
onTap: () => {Navigator.of(context).pop()},
),
ListTile(
title: Text('Settings'),
onTap: () =>
{Navigator.of(context).pushNamed(PageRouteConst.Settings)},
),
],
),
);
Expand Down
3 changes: 2 additions & 1 deletion lib/ui/pulsar/pulsar_page.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:flutter/material.dart';
import 'package:paas_dashboard_flutter/generated/l10n.dart';
import 'package:paas_dashboard_flutter/route/page_route_const.dart';
import 'package:paas_dashboard_flutter/ui/util/form_util.dart';
import 'package:paas_dashboard_flutter/vm/pulsar/pulsar_instance_list_view_model.dart';
Expand Down Expand Up @@ -59,7 +60,7 @@ class _PulsarPageState extends State<PulsarPage> {
onPressed: () {
vm.fetchPulsarInstances();
},
child: Text('Refresh'));
child: Text(S.of(context).refresh));
var body = ListView(
children: [
Container(
Expand Down
10 changes: 10 additions & 0 deletions lib/vm/general/settings_view_model.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import 'package:flutter/material.dart';

class SettingsViewModel extends ChangeNotifier {
String language = 'en';

void setLan(String lan) {
this.language = lan;
notifyListeners();
}
}
12 changes: 12 additions & 0 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ packages:
description: flutter
source: sdk
version: "0.0.0"
flutter_localizations:
dependency: "direct main"
description: flutter
source: sdk
version: "0.0.0"
flutter_test:
dependency: "direct dev"
description: flutter
Expand All @@ -88,6 +93,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "4.0.0"
intl:
dependency: transitive
description:
name: intl
url: "https://pub.dartlang.org"
source: hosted
version: "0.17.0"
matcher:
dependency: transitive
description:
Expand Down
4 changes: 4 additions & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ dependencies:
sqflite:
sqflite_common_ffi:
path:
flutter_localizations:
sdk: flutter

dev_dependencies:
flutter_test:
Expand Down Expand Up @@ -81,3 +83,5 @@ flutter:
#
# For details regarding fonts from package dependencies,
# see https://flutter.dev/custom-fonts/#from-packages
flutter_intl:
enabled: true

0 comments on commit 44d687b

Please sign in to comment.