Skip to content

Commit 29d814b

Browse files
[intl] speed up localization generation and regenerate symbols (#102614)
1 parent 6d8f9ed commit 29d814b

File tree

4 files changed

+2513
-2558
lines changed

4 files changed

+2513
-2558
lines changed

dev/tools/localization/bin/gen_date_localizations.dart

Lines changed: 57 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -87,16 +87,26 @@ Future<void> main(List<String> rawArgs) async {
8787
// To regenerate run (omit --overwrite to print to console instead of the file):
8888
// dart --enable-asserts dev/tools/localization/bin/gen_date_localizations.dart --overwrite
8989
90+
import 'package:intl/date_symbols.dart' as intl;
91+
9092
'''
9193
);
9294
buffer.writeln('''
9395
/// The subset of date symbols supported by the intl package which are also
9496
/// supported by flutter_localizations.''');
95-
buffer.writeln('const Map<String, dynamic> dateSymbols = <String, dynamic> {');
97+
buffer.writeln('final Map<String, intl.DateSymbols> dateSymbols = <String, intl.DateSymbols> {');
9698
symbolFiles.forEach((String locale, File data) {
9799
currentLocale = locale;
98-
if (supportedLocales.contains(locale))
99-
buffer.writeln(_jsonToMapEntry(locale, json.decode(data.readAsStringSync())));
100+
if (supportedLocales.contains(locale)) {
101+
final Map<String, Object?> objData = json.decode(data.readAsStringSync()) as Map<String, Object?>;
102+
buffer.writeln("'$locale': intl.DateSymbols(");
103+
objData.forEach((String key, Object? value) {
104+
if (value == null)
105+
return;
106+
buffer.writeln(_jsonToConstructorEntry(key, value));
107+
});
108+
buffer.writeln('),');
109+
}
100110
});
101111
currentLocale = null;
102112
buffer.writeln('};');
@@ -123,28 +133,67 @@ Future<void> main(List<String> rawArgs) async {
123133
if (writeToFile) {
124134
final File dateLocalizationsFile = File(path.join('packages', 'flutter_localizations', 'lib', 'src', 'l10n', 'generated_date_localizations.dart'));
125135
dateLocalizationsFile.writeAsStringSync(buffer.toString());
126-
Process.runSync(path.join('bin', 'cache', 'dart-sdk', 'bin', 'dartfmt'), <String>[
127-
'-w',
136+
final String extension = Platform.isWindows ? '.exe' : '';
137+
final ProcessResult result = Process.runSync(path.join('bin', 'cache', 'dart-sdk', 'bin', 'dart$extension'), <String>[
138+
'format',
128139
dateLocalizationsFile.path,
129140
]);
141+
if (result.exitCode != 0) {
142+
print(result.exitCode);
143+
print(result.stdout);
144+
print(result.stderr);
145+
}
130146
} else {
131147
print(buffer);
132148
}
133149
}
134150

151+
String _jsonToConstructorEntry(String key, dynamic value) {
152+
return '$key: ${_jsonToObject(value)},';
153+
}
154+
135155
String _jsonToMapEntry(String key, dynamic value) {
136156
return "'$key': ${_jsonToMap(value)},";
137157
}
138158

159+
String _jsonToObject(dynamic json) {
160+
if (json == null || json is num || json is bool)
161+
return '$json';
162+
163+
if (json is String)
164+
return generateEncodedString(currentLocale, json);
165+
166+
if (json is Iterable<Object?>) {
167+
final String type = json.first.runtimeType.toString();
168+
final StringBuffer buffer = StringBuffer('const <$type>[');
169+
for (final dynamic value in json) {
170+
buffer.writeln('${_jsonToMap(value)},');
171+
}
172+
buffer.write(']');
173+
return buffer.toString();
174+
}
175+
176+
if (json is Map<String, dynamic>) {
177+
final StringBuffer buffer = StringBuffer('<String, Object>{');
178+
json.forEach((String key, dynamic value) {
179+
buffer.writeln(_jsonToMapEntry(key, value));
180+
});
181+
buffer.write('}');
182+
return buffer.toString();
183+
}
184+
185+
throw 'Unsupported JSON type ${json.runtimeType} of value $json.';
186+
}
187+
139188
String _jsonToMap(dynamic json) {
140189
if (json == null || json is num || json is bool)
141190
return '$json';
142191

143192
if (json is String)
144-
return generateEncodedString(currentLocale!, json);
193+
return generateEncodedString(currentLocale, json);
145194

146195
if (json is Iterable) {
147-
final StringBuffer buffer = StringBuffer('<dynamic>[');
196+
final StringBuffer buffer = StringBuffer('<String>[');
148197
for (final dynamic value in json) {
149198
buffer.writeln('${_jsonToMap(value)},');
150199
}
@@ -153,7 +202,7 @@ String _jsonToMap(dynamic json) {
153202
}
154203

155204
if (json is Map<String, dynamic>) {
156-
final StringBuffer buffer = StringBuffer('<String, dynamic>{');
205+
final StringBuffer buffer = StringBuffer('<String, Object>{');
157206
json.forEach((String key, dynamic value) {
158207
buffer.writeln(_jsonToMapEntry(key, value));
159208
});

dev/tools/localization/localizations_utils.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,7 @@ String generateString(String value) {
427427
/// Only used to generate localization strings for the Kannada locale ('kn') because
428428
/// some of the localized strings contain characters that can crash Emacs on Linux.
429429
/// See packages/flutter_localizations/lib/src/l10n/README for more information.
430-
String generateEncodedString(String locale, String value) {
430+
String generateEncodedString(String? locale, String value) {
431431
if (locale != 'kn' || value.runes.every((int code) => code <= 0xFF))
432432
return generateString(value);
433433

0 commit comments

Comments
 (0)