Unverified Commit 29d814b7 authored by Jonah Williams's avatar Jonah Williams Committed by GitHub

[intl] speed up localization generation and regenerate symbols (#102614)

parent 6d8f9ed9
......@@ -87,16 +87,26 @@ Future<void> main(List<String> rawArgs) async {
// To regenerate run (omit --overwrite to print to console instead of the file):
// dart --enable-asserts dev/tools/localization/bin/gen_date_localizations.dart --overwrite
import 'package:intl/date_symbols.dart' as intl;
'''
);
buffer.writeln('''
/// The subset of date symbols supported by the intl package which are also
/// supported by flutter_localizations.''');
buffer.writeln('const Map<String, dynamic> dateSymbols = <String, dynamic> {');
buffer.writeln('final Map<String, intl.DateSymbols> dateSymbols = <String, intl.DateSymbols> {');
symbolFiles.forEach((String locale, File data) {
currentLocale = locale;
if (supportedLocales.contains(locale))
buffer.writeln(_jsonToMapEntry(locale, json.decode(data.readAsStringSync())));
if (supportedLocales.contains(locale)) {
final Map<String, Object?> objData = json.decode(data.readAsStringSync()) as Map<String, Object?>;
buffer.writeln("'$locale': intl.DateSymbols(");
objData.forEach((String key, Object? value) {
if (value == null)
return;
buffer.writeln(_jsonToConstructorEntry(key, value));
});
buffer.writeln('),');
}
});
currentLocale = null;
buffer.writeln('};');
......@@ -123,28 +133,67 @@ Future<void> main(List<String> rawArgs) async {
if (writeToFile) {
final File dateLocalizationsFile = File(path.join('packages', 'flutter_localizations', 'lib', 'src', 'l10n', 'generated_date_localizations.dart'));
dateLocalizationsFile.writeAsStringSync(buffer.toString());
Process.runSync(path.join('bin', 'cache', 'dart-sdk', 'bin', 'dartfmt'), <String>[
'-w',
final String extension = Platform.isWindows ? '.exe' : '';
final ProcessResult result = Process.runSync(path.join('bin', 'cache', 'dart-sdk', 'bin', 'dart$extension'), <String>[
'format',
dateLocalizationsFile.path,
]);
if (result.exitCode != 0) {
print(result.exitCode);
print(result.stdout);
print(result.stderr);
}
} else {
print(buffer);
}
}
String _jsonToConstructorEntry(String key, dynamic value) {
return '$key: ${_jsonToObject(value)},';
}
String _jsonToMapEntry(String key, dynamic value) {
return "'$key': ${_jsonToMap(value)},";
}
String _jsonToObject(dynamic json) {
if (json == null || json is num || json is bool)
return '$json';
if (json is String)
return generateEncodedString(currentLocale, json);
if (json is Iterable<Object?>) {
final String type = json.first.runtimeType.toString();
final StringBuffer buffer = StringBuffer('const <$type>[');
for (final dynamic value in json) {
buffer.writeln('${_jsonToMap(value)},');
}
buffer.write(']');
return buffer.toString();
}
if (json is Map<String, dynamic>) {
final StringBuffer buffer = StringBuffer('<String, Object>{');
json.forEach((String key, dynamic value) {
buffer.writeln(_jsonToMapEntry(key, value));
});
buffer.write('}');
return buffer.toString();
}
throw 'Unsupported JSON type ${json.runtimeType} of value $json.';
}
String _jsonToMap(dynamic json) {
if (json == null || json is num || json is bool)
return '$json';
if (json is String)
return generateEncodedString(currentLocale!, json);
return generateEncodedString(currentLocale, json);
if (json is Iterable) {
final StringBuffer buffer = StringBuffer('<dynamic>[');
final StringBuffer buffer = StringBuffer('<String>[');
for (final dynamic value in json) {
buffer.writeln('${_jsonToMap(value)},');
}
......@@ -153,7 +202,7 @@ String _jsonToMap(dynamic json) {
}
if (json is Map<String, dynamic>) {
final StringBuffer buffer = StringBuffer('<String, dynamic>{');
final StringBuffer buffer = StringBuffer('<String, Object>{');
json.forEach((String key, dynamic value) {
buffer.writeln(_jsonToMapEntry(key, value));
});
......
......@@ -427,7 +427,7 @@ String generateString(String value) {
/// Only used to generate localization strings for the Kannada locale ('kn') because
/// some of the localized strings contain characters that can crash Emacs on Linux.
/// See packages/flutter_localizations/lib/src/l10n/README for more information.
String generateEncodedString(String locale, String value) {
String generateEncodedString(String? locale, String value) {
if (locale != 'kn' || value.runes.every((int code) => code <= 0xFF))
return generateString(value);
......
......@@ -17,11 +17,9 @@ bool _dateIntlDataInitialized = false;
void loadDateIntlDataIfNotLoaded() {
if (!_dateIntlDataInitialized) {
date_localizations.dateSymbols
.cast<String, Map<String, dynamic>>()
.forEach((String locale, Map<String, dynamic> data) {
.forEach((String locale, intl.DateSymbols symbols) {
// Perform initialization.
assert(date_localizations.datePatterns.containsKey(locale));
final intl.DateSymbols symbols = intl.DateSymbols.deserializeFromMap(data);
date_symbol_data_custom.initializeDateFormattingCustom(
locale: locale,
symbols: symbols,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment