forked from ilteoood/flutter_i18n
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.dart
110 lines (99 loc) · 3.07 KB
/
main.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter_i18n/flutter_i18n.dart';
import 'package:flutter_i18n/flutter_i18n_delegate.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
Future main() async {
final FlutterI18nDelegate flutterI18nDelegate = FlutterI18nDelegate(
useCountryCode: false,
fallbackFile: 'en',
path: 'assets/i18n',
forcedLocale: new Locale('es'));
WidgetsFlutterBinding.ensureInitialized();
await flutterI18nDelegate.load(null);
runApp(new MyApp(flutterI18nDelegate));
}
class MyApp extends StatelessWidget {
final FlutterI18nDelegate flutterI18nDelegate;
MyApp(this.flutterI18nDelegate);
@override
Widget build(BuildContext context) {
return new MaterialApp(
title: 'Flutter Demo',
theme: new ThemeData(
primarySwatch: Colors.blue,
),
home: new MyHomePage(),
localizationsDelegates: [
flutterI18nDelegate,
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate
],
);
}
}
class MyHomePage extends StatefulWidget {
@override
MyHomeState createState() => new MyHomeState();
}
class MyHomeState extends State<MyHomePage> {
Locale currentLang;
int clicked = 0;
@override
void initState() {
super.initState();
new Future.delayed(Duration.zero, () async {
setState(() {
currentLang = FlutterI18n.currentLocale(context);
});
});
}
changeLanguage() {
setState(() {
currentLang = currentLang.languageCode == 'en'
? new Locale('it')
: new Locale('en');
});
}
incrementCounter() {
setState(() {
clicked++;
});
}
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar:
new AppBar(title: new Text(FlutterI18n.translate(context, "title"))),
body: new Builder(builder: (BuildContext context) {
return new Center(
child: new Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
new Text(FlutterI18n.translate(context, "label.main",
Map.fromIterables(["user"], ["Flutter lover"]))),
new Text(FlutterI18n.plural(context, "clicked.times", clicked)),
new FlatButton(
onPressed: () async {
incrementCounter();
},
child: new Text(
FlutterI18n.translate(context, "button.label.clickMe"))),
new FlatButton(
onPressed: () async {
changeLanguage();
await FlutterI18n.refresh(context, currentLang);
Scaffold.of(context).showSnackBar(new SnackBar(
content: new Text(FlutterI18n.translate(
context, "button.toastMessage")),
));
},
child: new Text(
FlutterI18n.translate(context, "button.label.language")))
],
),
);
}),
);
}
}