/
applocale_example.dart
80 lines (71 loc) · 2.25 KB
/
applocale_example.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
import 'package:flutter/material.dart';
import 'package:applocale/applocale.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
// define supported Language lists
Map<String, String> get _supportedLanguages => {
"en": "English",
"en_us": "English(USA)",
"bn": "Bengali",
};
String get _defaultLanguage => "en";
List<String> get _getSupportedLanguages =>
_supportedLanguages.entries.map((l) => l.key).toList();
void main(List<String> args) => runApp(FlutterDemoApp());
class FlutterDemoApp extends StatefulWidget {
@override
_FlutterDemoApp createState() => _FlutterDemoApp();
}
class _FlutterDemoApp extends State<FlutterDemoApp> {
// initialize _localeDelegate
LocaleDelegate _localeDelegate = LocaleDelegate.init(
_getSupportedLanguages,
// * optional, if it's same as the first one in the supportedLanguages
defaultLanguage: _defaultLanguage,
);
@override
void initState() {
super.initState();
}
@override
Widget build(BuildContext context) => MaterialApp(
supportedLocales: _localeDelegate.supportedLocales, // Step I
localizationsDelegates: [
_localeDelegate, // Step II
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate
],
title: 'Flutter Demo',
home: FlutterDemo(),
);
}
class FlutterDemo extends StatelessWidget {
@override
Widget build(BuildContext context) {
// since LocaleDelegate is already initialized & ready
var appLocale = AppLocale.of(context); // Step III
// In case some additional values can be set now. This is an one time
// activity
appLocale.updateValue({'name': 'জয়ন্তী'});
return Scaffold(
appBar: AppBar(
title: Text(appLocale.localValue('title')),
),
body: ListView(
children: <Widget>[
Center(
child: Text(appLocale.localValue('subDetail.greeting')),
),
Center(
child: Text(appLocale.localValue(
'subDetail.runtimeText',
{'replacement': 'Individual'}, // runtime interpolation
)),
),
Center(
child: Text(appLocale.localValue('message')),
),
],
),
);
}
}