|
@@ -0,0 +1,51 @@
|
|
1
|
+import 'dart:convert';
|
|
2
|
+
|
|
3
|
+import 'package:flutter/material.dart';
|
|
4
|
+import 'package:flutter/services.dart';
|
|
5
|
+
|
|
6
|
+class DemoLocalization {
|
|
7
|
+ DemoLocalization(this.locale);
|
|
8
|
+
|
|
9
|
+ final Locale locale;
|
|
10
|
+ static DemoLocalization of(BuildContext context) {
|
|
11
|
+ return Localizations.of<DemoLocalization>(context, DemoLocalization);
|
|
12
|
+ }
|
|
13
|
+
|
|
14
|
+ Map<String, String> _localizedValues;
|
|
15
|
+
|
|
16
|
+ Future<void> load() async {
|
|
17
|
+ String jsonStringValues =
|
|
18
|
+ await rootBundle.loadString('lib/languages/${locale.languageCode}.json');
|
|
19
|
+ Map<String, dynamic> mappedJson = json.decode(jsonStringValues);
|
|
20
|
+ _localizedValues =
|
|
21
|
+ mappedJson.map((key, value) => MapEntry(key, value.toString()));
|
|
22
|
+ }
|
|
23
|
+
|
|
24
|
+ String translate(String key) {
|
|
25
|
+ return _localizedValues[key];
|
|
26
|
+ }
|
|
27
|
+
|
|
28
|
+ // static member to have simple access to the delegate from Material App
|
|
29
|
+ static const LocalizationsDelegate<DemoLocalization> delegate =
|
|
30
|
+ _DemoLocalizationsDelegate();
|
|
31
|
+}
|
|
32
|
+
|
|
33
|
+class _DemoLocalizationsDelegate
|
|
34
|
+ extends LocalizationsDelegate<DemoLocalization> {
|
|
35
|
+ const _DemoLocalizationsDelegate();
|
|
36
|
+
|
|
37
|
+ @override
|
|
38
|
+ bool isSupported(Locale locale) {
|
|
39
|
+ return ['en', 'es', 'fa', 'hi'].contains(locale.languageCode);
|
|
40
|
+ }
|
|
41
|
+
|
|
42
|
+ @override
|
|
43
|
+ Future<DemoLocalization> load(Locale locale) async {
|
|
44
|
+ DemoLocalization localization = new DemoLocalization(locale);
|
|
45
|
+ await localization.load();
|
|
46
|
+ return localization;
|
|
47
|
+ }
|
|
48
|
+
|
|
49
|
+ @override
|
|
50
|
+ bool shouldReload(LocalizationsDelegate<DemoLocalization> old) => false;
|
|
51
|
+}
|