CAKE-272 | moved constants into localization_constants.dart and locale_list.dart; moved generation of localized strings inside of the loop body
OleksandrSobol committed
Mar 22, 2021 at 20:08 UTC
78689d394de71c532d0b43cdb5446eef03ae744e
3 files changed
+128
-126
tool/generate_localization.dart
+13
-126
@@ -1,144 +1,31 @@
1
import 'dart:io';
2
import 'dart:convert';
3
+import 'localization/locale_list.dart';
4
+import 'localization/localization_constants.dart';
5
6
const inputPath = 'res/values/';
7
const outputPath = 'lib/generated/i18n.dart';
6
-const locales = ['en', 'de', 'es', 'hi',
7
- 'ja', 'ko', 'nl', 'pl',
8
- 'pt', 'ru', 'uk', 'zh'];
9
-const header = """
10
-import \'dart:async\';
11
-import \'package:flutter/foundation.dart\';
12
-import \'package:flutter/material.dart\';
13
-
14
-class S implements WidgetsLocalizations {
15
- const S();
16
-
17
- static S current;
18
-
19
- static const GeneratedLocalizationsDelegate delegate =
20
- GeneratedLocalizationsDelegate();
21
-
22
- static S of(BuildContext context) => Localizations.of<S>(context, S);
23
-""";
24
-
25
-const textDirectionDeclaration = """
26
-
27
- @override
28
- TextDirection get textDirection => TextDirection.ltr;
29
-
30
-""";
31
-
32
-const classDeclaration = """
33
-class GeneratedLocalizationsDelegate extends LocalizationsDelegate<S> {
34
- const GeneratedLocalizationsDelegate();
35
-
36
- List<Locale> get supportedLocales {
37
- return const <Locale>[
38
-""";
39
-
40
-const middle = """
41
- ];
42
- }
43
-
44
- LocaleListResolutionCallback listResolution({Locale fallback, bool withCountry = true}) {
45
- return (List<Locale> locales, Iterable<Locale> supported) {
46
- if (locales == null || locales.isEmpty) {
47
- return fallback ?? supported.first;
48
- } else {
49
- return _resolve(locales.first, fallback, supported, withCountry);
50
- }
51
- };
52
- }
53
-
54
- LocaleResolutionCallback resolution({Locale fallback, bool withCountry = true}) {
55
- return (Locale locale, Iterable<Locale> supported) {
56
- return _resolve(locale, fallback, supported, withCountry);
57
- };
58
- }
59
-
60
- @override
61
- Future<S> load(Locale locale) {
62
- final String lang = getLang(locale);
63
- if (lang != null) {
64
- switch (lang) {
65
-""";
66
-
67
-const end = """
68
- default:
69
- }
70
- }
71
- S.current = const S();
72
- return SynchronousFuture<S>(S.current);
73
- }
74
-
75
- @override
76
- bool isSupported(Locale locale) => _isSupported(locale, true);
77
-
78
- @override
79
- bool shouldReload(GeneratedLocalizationsDelegate old) => false;
80
-
81
- Locale _resolve(Locale locale, Locale fallback, Iterable<Locale> supported, bool withCountry) {
82
- if (locale == null || !_isSupported(locale, withCountry)) {
83
- return fallback ?? supported.first;
84
- }
85
-
86
- final Locale languageLocale = Locale(locale.languageCode, "");
87
- if (supported.contains(locale)) {
88
- return locale;
89
- } else if (supported.contains(languageLocale)) {
90
- return languageLocale;
91
- } else {
92
- final Locale fallbackLocale = fallback ?? supported.first;
93
- return fallbackLocale;
94
- }
95
- }
96
-
97
- bool _isSupported(Locale locale, bool withCountry) {
98
- if (locale != null) {
99
- for (Locale supportedLocale in supportedLocales) {
100
- if (supportedLocale.languageCode != locale.languageCode) {
101
- continue;
102
- }
103
- if (supportedLocale.countryCode == locale.countryCode) {
104
- return true;
105
- }
106
- if (true != withCountry && (supportedLocale.countryCode == null || supportedLocale.countryCode.isEmpty)) {
107
- return true;
108
- }
109
- }
110
- }
111
- return false;
112
- }
113
-}
114
-
115
-String getLang(Locale l) => l == null
116
- ? null
117
- : l.countryCode != null && l.countryCode.isEmpty
118
- ? l.languageCode
119
- : l.toString();
120
-""";
8
9
Future<void> main() async {
123
- var inputContent = File(inputPath + 'strings_en.arb').readAsStringSync();
124
- var config = json.decode(inputContent) as Map<String, dynamic>;
10
var output = '';
11
127
- output += header;
12
+ output += part1;
13
output += textDirectionDeclaration;
129
- output += localizedStrings(config: config, hasOverride: false);
130
- output += '}' + '\n\n';
14
15
for (var locale in locales) {
16
+ final inputContent = File(inputPath + 'strings_$locale.arb').readAsStringSync();
17
+ final config = json.decode(inputContent) as Map<String, dynamic>;
18
+
19
+ if (locale == locales.first) {
20
+ output += localizedStrings(config: config, hasOverride: false);
21
+ output += '}' + '\n\n';
22
+ }
23
+
24
output += 'class \$$locale extends S {' + '\n';
25
output += ' const \$$locale();' + '\n';
26
27
if (locale != locales.first) {
28
output += textDirectionDeclaration;
138
-
139
- inputContent = File(inputPath + 'strings_$locale.arb').readAsStringSync();
140
- config = json.decode(inputContent) as Map<String, dynamic>;
141
-
29
output += localizedStrings(config: config, hasOverride: true);
30
}
31
@@ -151,7 +38,7 @@ Future<void> main() async {
38
output += ' Locale("$locale", ""),' + '\n';
39
}
40
154
- output += middle;
41
+ output += part2;
42
43
for (var locale in locales) {
44
output += ' case "$locale":' + '\n';
@@ -159,7 +46,7 @@ Future<void> main() async {
46
output += ' return SynchronousFuture<S>(S.current);' + '\n';
47
}
48
162
- output += end;
49
+ output += part3;
50
51
await File(outputPath).writeAsString(output);
52
}
tool/localization/locale_list.dart
new
+3
@@ -0,0 +1,3 @@
1
+const locales = ['en', 'de', 'es', 'hi',
2
+ 'ja', 'ko', 'nl', 'pl',
3
+ 'pt', 'ru', 'uk', 'zh'];
\ No newline at end of file
tool/localization/localization_constants.dart
new
+112
@@ -0,0 +1,112 @@
1
+const textDirectionDeclaration = """
2
+
3
+ @override
4
+ TextDirection get textDirection => TextDirection.ltr;
5
+
6
+""";
7
+
8
+const classDeclaration = """
9
+class GeneratedLocalizationsDelegate extends LocalizationsDelegate<S> {
10
+ const GeneratedLocalizationsDelegate();
11
+
12
+ List<Locale> get supportedLocales {
13
+ return const <Locale>[
14
+""";
15
+
16
+const part1 = """
17
+import \'dart:async\';
18
+import \'package:flutter/foundation.dart\';
19
+import \'package:flutter/material.dart\';
20
+
21
+class S implements WidgetsLocalizations {
22
+ const S();
23
+
24
+ static S current;
25
+
26
+ static const GeneratedLocalizationsDelegate delegate =
27
+ GeneratedLocalizationsDelegate();
28
+
29
+ static S of(BuildContext context) => Localizations.of<S>(context, S);
30
+""";
31
+
32
+const part2 = """
33
+ ];
34
+ }
35
+
36
+ LocaleListResolutionCallback listResolution({Locale fallback, bool withCountry = true}) {
37
+ return (List<Locale> locales, Iterable<Locale> supported) {
38
+ if (locales == null || locales.isEmpty) {
39
+ return fallback ?? supported.first;
40
+ } else {
41
+ return _resolve(locales.first, fallback, supported, withCountry);
42
+ }
43
+ };
44
+ }
45
+
46
+ LocaleResolutionCallback resolution({Locale fallback, bool withCountry = true}) {
47
+ return (Locale locale, Iterable<Locale> supported) {
48
+ return _resolve(locale, fallback, supported, withCountry);
49
+ };
50
+ }
51
+
52
+ @override
53
+ Future<S> load(Locale locale) {
54
+ final String lang = getLang(locale);
55
+ if (lang != null) {
56
+ switch (lang) {
57
+""";
58
+
59
+const part3 = """
60
+ default:
61
+ }
62
+ }
63
+ S.current = const S();
64
+ return SynchronousFuture<S>(S.current);
65
+ }
66
+
67
+ @override
68
+ bool isSupported(Locale locale) => _isSupported(locale, true);
69
+
70
+ @override
71
+ bool shouldReload(GeneratedLocalizationsDelegate old) => false;
72
+
73
+ Locale _resolve(Locale locale, Locale fallback, Iterable<Locale> supported, bool withCountry) {
74
+ if (locale == null || !_isSupported(locale, withCountry)) {
75
+ return fallback ?? supported.first;
76
+ }
77
+
78
+ final Locale languageLocale = Locale(locale.languageCode, "");
79
+ if (supported.contains(locale)) {
80
+ return locale;
81
+ } else if (supported.contains(languageLocale)) {
82
+ return languageLocale;
83
+ } else {
84
+ final Locale fallbackLocale = fallback ?? supported.first;
85
+ return fallbackLocale;
86
+ }
87
+ }
88
+
89
+ bool _isSupported(Locale locale, bool withCountry) {
90
+ if (locale != null) {
91
+ for (Locale supportedLocale in supportedLocales) {
92
+ if (supportedLocale.languageCode != locale.languageCode) {
93
+ continue;
94
+ }
95
+ if (supportedLocale.countryCode == locale.countryCode) {
96
+ return true;
97
+ }
98
+ if (true != withCountry && (supportedLocale.countryCode == null || supportedLocale.countryCode.isEmpty)) {
99
+ return true;
100
+ }
101
+ }
102
+ }
103
+ return false;
104
+ }
105
+}
106
+
107
+String getLang(Locale l) => l == null
108
+ ? null
109
+ : l.countryCode != null && l.countryCode.isEmpty
110
+ ? l.languageCode
111
+ : l.toString();
112
+""";
\ No newline at end of file