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