Unverified Commit 9077a5c6 authored by Shi-Hao Hong's avatar Shi-Hao Hong Committed by GitHub

[gen-l10n] Remove more ignores, conditionally generate some code (#78975)

parent 04ebce92
...@@ -1169,6 +1169,7 @@ class LocalizationsGenerator { ...@@ -1169,6 +1169,7 @@ class LocalizationsGenerator {
.replaceAll('@(supportedLanguageCodes)', supportedLanguageCodes.join(', ')) .replaceAll('@(supportedLanguageCodes)', supportedLanguageCodes.join(', '))
.replaceAll('@(messageClassImports)', sortedClassImports.join('\n')) .replaceAll('@(messageClassImports)', sortedClassImports.join('\n'))
.replaceAll('@(delegateClass)', delegateClass) .replaceAll('@(delegateClass)', delegateClass)
.replaceAll('@(requiresFoundationImport)', _useDeferredLoading ? '' : "import 'package:flutter/foundation.dart';")
.replaceAll('@(requiresIntlImport)', _containsPluralMessage() ? "import 'package:intl/intl.dart' as intl;" : '') .replaceAll('@(requiresIntlImport)', _containsPluralMessage() ? "import 'package:intl/intl.dart' as intl;" : '')
.replaceAll('@(canBeNullable)', _usesNullableGetter ? '?' : '') .replaceAll('@(canBeNullable)', _usesNullableGetter ? '?' : '')
.replaceAll('@(needsNullCheck)', _usesNullableGetter ? '' : '!'); .replaceAll('@(needsNullCheck)', _usesNullableGetter ? '' : '!');
......
...@@ -12,8 +12,7 @@ const String fileTemplate = ''' ...@@ -12,8 +12,7 @@ const String fileTemplate = '''
@(header) @(header)
import 'dart:async'; import 'dart:async';
// ignore: unused_import @(requiresFoundationImport)
import 'package:flutter/foundation.dart';
import 'package:flutter/widgets.dart'; import 'package:flutter/widgets.dart';
import 'package:flutter_localizations/flutter_localizations.dart'; import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:intl/intl.dart' as intl; import 'package:intl/intl.dart' as intl;
...@@ -74,7 +73,6 @@ import 'package:intl/intl.dart' as intl; ...@@ -74,7 +73,6 @@ import 'package:intl/intl.dart' as intl;
abstract class @(class) { abstract class @(class) {
@(class)(String locale) : localeName = intl.Intl.canonicalizedLocale(locale.toString()); @(class)(String locale) : localeName = intl.Intl.canonicalizedLocale(locale.toString());
// ignore: unused_field
final String localeName; final String localeName;
static @(class)@(canBeNullable) of(BuildContext context) { static @(class)@(canBeNullable) of(BuildContext context) {
......
...@@ -57,6 +57,9 @@ const String singleZhMessageArbFileString = ''' ...@@ -57,6 +57,9 @@ const String singleZhMessageArbFileString = '''
const String intlImportDartCode = ''' const String intlImportDartCode = '''
import 'package:intl/intl.dart' as intl; import 'package:intl/intl.dart' as intl;
'''; ''';
const String foundationImportDartCode = '''
import 'package:flutter/foundation.dart';
''';
void _standardFlutterDirectoryL10nSetup(FileSystem fs) { void _standardFlutterDirectoryL10nSetup(FileSystem fs) {
final Directory l10nDirectory = fs.currentDirectory.childDirectory('lib').childDirectory('l10n') final Directory l10nDirectory = fs.currentDirectory.childDirectory('lib').childDirectory('l10n')
...@@ -1882,6 +1885,60 @@ import 'output-localization-file_en.dart' deferred as output-localization-file_e ...@@ -1882,6 +1885,60 @@ import 'output-localization-file_en.dart' deferred as output-localization-file_e
expect(localizationsFile, contains(intlImportDartCode)); expect(localizationsFile, contains(intlImportDartCode));
}); });
testUsingContext('foundation package import should be omitted from file template when deferred loading = true', () {
fs.currentDirectory.childDirectory('lib').childDirectory('l10n')..createSync(recursive: true)
..childFile(defaultTemplateArbFileName).writeAsStringSync(singleMessageArbFileString)
..childFile('app_es.arb').writeAsStringSync(singleEsMessageArbFileString);
final LocalizationsGenerator generator = LocalizationsGenerator(fs);
try {
generator.initialize(
inputPathString: defaultL10nPathString,
outputPathString: defaultL10nPathString,
templateArbFileName: defaultTemplateArbFileName,
outputFileString: defaultOutputFileString,
classNameString: defaultClassNameString,
useDeferredLoading: true,
);
generator.loadResources();
generator.writeOutputFiles(BufferLogger.test());
} on Exception catch (e) {
fail('Generating output files should not fail: $e');
}
final String localizationsFile = fs.file(
fs.path.join(syntheticL10nPackagePath, 'output-localization-file.dart'),
).readAsStringSync();
expect(localizationsFile, isNot(contains(foundationImportDartCode)));
});
testUsingContext('foundation package import should be kept in file template when deferred loading = false', () {
fs.currentDirectory.childDirectory('lib').childDirectory('l10n')..createSync(recursive: true)
..childFile(defaultTemplateArbFileName).writeAsStringSync(singleMessageArbFileString)
..childFile('app_es.arb').writeAsStringSync(singleEsMessageArbFileString);
final LocalizationsGenerator generator = LocalizationsGenerator(fs);
try {
generator.initialize(
inputPathString: defaultL10nPathString,
outputPathString: defaultL10nPathString,
templateArbFileName: defaultTemplateArbFileName,
outputFileString: defaultOutputFileString,
classNameString: defaultClassNameString,
useDeferredLoading: false,
);
generator.loadResources();
generator.writeOutputFiles(BufferLogger.test());
} on Exception catch (e) {
fail('Generating output files should not fail: $e');
}
final String localizationsFile = fs.file(
fs.path.join(syntheticL10nPackagePath, 'output-localization-file.dart'),
).readAsStringSync();
expect(localizationsFile, contains(foundationImportDartCode));
});
testUsingContext('check for string interpolation rules', () { testUsingContext('check for string interpolation rules', () {
const String enArbCheckList = ''' const String enArbCheckList = '''
{ {
......
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