-
Notifications
You must be signed in to change notification settings - Fork 0
/
fast_localization.dart
65 lines (60 loc) · 1.77 KB
/
fast_localization.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
// 1. import material
import 'package:flutter/material.dart';
// 2. import fast localization
import 'package:fast_localization/fast_localization.dart';
// 3. add async to main function
void main() async {
// 4. declare locales as you like, either as follows or import them
final locales = {
Locale('en'): {
"title": "Demo",
"welcome": "Hello World!",
},
Locale('ar'): {
"title": "عرض",
"welcome": "أهلاً بالعالم!",
},
};
// 5. load fast localization
await Localization.load(locales);
// 6. use LocalizationApp
runApp(LocalizationApp(
title: () => Localization.translate(
'title'), // MaterialApp(title) | required with home: only
home: () => HomeScreen(), // MaterialApp(home) | required with home: only
theme: ThemeData.light(), // MaterialApp(theme) | optional
));
}
class HomeScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(Localization.translate('title')),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text('😉', style: TextStyle(fontSize: 50)),
Text(Localization.translate('title'),
style: TextStyle(fontSize: 25)),
SizedBox(height: 60),
TextButton(
child: Text('English'),
onPressed: () {
Localization.changeLocale(Locale('en'), context);
},
),
TextButton(
child: Text('عربي'),
onPressed: () {
Localization.changeLocale(Locale('ar'), context);
},
),
],
),
),
);
}
}