Unverified Commit aa39fa5a authored by Alex Li's avatar Alex Li Committed by GitHub

[gen_l10n] Better blank lines in the header of generated files (#103414)

parent 78885ecb
...@@ -1133,14 +1133,14 @@ class LocalizationsGenerator { ...@@ -1133,14 +1133,14 @@ class LocalizationsGenerator {
}); });
return classFileTemplate return classFileTemplate
.replaceAll('@(header)', header) .replaceAll('@(header)', header.isEmpty ? '' : '$header\n\n')
.replaceAll('@(language)', describeLocale(locale.toString())) .replaceAll('@(language)', describeLocale(locale.toString()))
.replaceAll('@(baseClass)', className) .replaceAll('@(baseClass)', className)
.replaceAll('@(fileName)', fileName) .replaceAll('@(fileName)', fileName)
.replaceAll('@(class)', '$className${locale.camelCase()}') .replaceAll('@(class)', '$className${locale.camelCase()}')
.replaceAll('@(localeName)', locale.toString()) .replaceAll('@(localeName)', locale.toString())
.replaceAll('@(methods)', methods.join('\n\n')) .replaceAll('@(methods)', methods.join('\n\n'))
.replaceAll('@(requiresIntlImport)', _requiresIntlImport() ? "import 'package:intl/intl.dart' as intl;" : ''); .replaceAll('@(requiresIntlImport)', _requiresIntlImport() ? "import 'package:intl/intl.dart' as intl;\n\n" : '');
} }
String _generateSubclass( String _generateSubclass(
...@@ -1284,7 +1284,7 @@ class LocalizationsGenerator { ...@@ -1284,7 +1284,7 @@ class LocalizationsGenerator {
); );
return fileTemplate return fileTemplate
.replaceAll('@(header)', header) .replaceAll('@(header)', header.isEmpty ? '' : '$header\n')
.replaceAll('@(class)', className) .replaceAll('@(class)', className)
.replaceAll('@(methods)', _allMessages.map((Message message) => generateBaseClassMethod(message, _templateArbLocale)).join('\n')) .replaceAll('@(methods)', _allMessages.map((Message message) => generateBaseClassMethod(message, _templateArbLocale)).join('\n'))
.replaceAll('@(importFile)', '$directory/$outputFileName') .replaceAll('@(importFile)', '$directory/$outputFileName')
......
...@@ -9,8 +9,7 @@ description: The Flutter application's synthetic package. ...@@ -9,8 +9,7 @@ description: The Flutter application's synthetic package.
'''; ''';
const String fileTemplate = ''' const String fileTemplate = '''
@(header) @(header)import 'dart:async';
import 'dart:async';
@(requiresFoundationImport) @(requiresFoundationImport)
import 'package:flutter/widgets.dart'; import 'package:flutter/widgets.dart';
...@@ -19,14 +18,14 @@ import 'package:intl/intl.dart' as intl; ...@@ -19,14 +18,14 @@ import 'package:intl/intl.dart' as intl;
@(messageClassImports) @(messageClassImports)
/// Callers can lookup localized strings with an instance of @(class) returned /// Callers can lookup localized strings with an instance of @(class)
/// by `@(class).of(context)`. /// returned by `@(class).of(context)`.
/// ///
/// Applications need to include `@(class).delegate()` in their app's /// Applications need to include `@(class).delegate()` in their app's
/// localizationDelegates list, and the locales they support in the app's /// `localizationDelegates` list, and the locales they support in the app's
/// supportedLocales list. For example: /// `supportedLocales` list. For example:
/// ///
/// ``` /// ```dart
/// import '@(importFile)'; /// import '@(importFile)';
/// ///
/// return MaterialApp( /// return MaterialApp(
...@@ -41,14 +40,14 @@ import 'package:intl/intl.dart' as intl; ...@@ -41,14 +40,14 @@ import 'package:intl/intl.dart' as intl;
/// Please make sure to update your pubspec.yaml to include the following /// Please make sure to update your pubspec.yaml to include the following
/// packages: /// packages:
/// ///
/// ``` /// ```yaml
/// dependencies: /// dependencies:
/// # Internationalization support. /// # Internationalization support.
/// flutter_localizations: /// flutter_localizations:
/// sdk: flutter /// sdk: flutter
/// intl: any # Use the pinned version from flutter_localizations /// intl: any # Use the pinned version from flutter_localizations
/// ///
/// # rest of dependencies /// # Rest of dependencies
/// ``` /// ```
/// ///
/// ## iOS Applications /// ## iOS Applications
...@@ -202,10 +201,7 @@ const String selectMethodTemplateInString = ''' ...@@ -202,10 +201,7 @@ const String selectMethodTemplateInString = '''
}'''; }''';
const String classFileTemplate = ''' const String classFileTemplate = '''
@(header) @(header)@(requiresIntlImport)import '@(fileName)';
@(requiresIntlImport)
import '@(fileName)';
/// The translations for @(language) (`@(localeName)`). /// The translations for @(language) (`@(localeName)`).
class @(class) extends @(baseClass) { class @(class) extends @(baseClass) {
......
...@@ -789,7 +789,6 @@ void main() { ...@@ -789,7 +789,6 @@ void main() {
expect(fs.file('/lib/l10n/bar_en.dart').readAsStringSync(), ''' expect(fs.file('/lib/l10n/bar_en.dart').readAsStringSync(), '''
HEADER HEADER
import 'bar.dart'; import 'bar.dart';
/// The translations for English (`en`). /// The translations for English (`en`).
...@@ -839,6 +838,65 @@ flutter: ...@@ -839,6 +838,65 @@ flutter:
), ),
); );
}); });
testWithoutContext('blank lines generated nicely', () async {
_standardFlutterDirectoryL10nSetup(fs);
// Test without headers.
generateLocalizations(
fileSystem: fs,
options: LocalizationOptions(
arbDirectory: Uri.directory(defaultL10nPathString),
outputDirectory: Uri.directory(defaultL10nPathString, windows: false),
templateArbFile: Uri.file(defaultTemplateArbFileName, windows: false),
useSyntheticPackage: false,
),
logger: BufferLogger.test(),
projectDir: fs.currentDirectory,
dependenciesDir: fs.currentDirectory,
);
expect(fs.file('/lib/l10n/app_localizations_en.dart').readAsStringSync(), '''
import 'app_localizations.dart';
/// The translations for English (`en`).
class AppLocalizationsEn extends AppLocalizations {
AppLocalizationsEn([String locale = 'en']) : super(locale);
@override
String get title => 'Title';
}
''');
// Test with headers.
generateLocalizations(
fileSystem: fs,
options: LocalizationOptions(
header: 'HEADER',
arbDirectory: Uri.directory(defaultL10nPathString),
outputDirectory: Uri.directory(defaultL10nPathString, windows: false),
templateArbFile: Uri.file(defaultTemplateArbFileName, windows: false),
useSyntheticPackage: false,
),
logger: BufferLogger.test(),
projectDir: fs.currentDirectory,
dependenciesDir: fs.currentDirectory,
);
expect(fs.file('/lib/l10n/app_localizations_en.dart').readAsStringSync(), '''
HEADER
import 'app_localizations.dart';
/// The translations for English (`en`).
class AppLocalizationsEn extends AppLocalizations {
AppLocalizationsEn([String locale = 'en']) : super(locale);
@override
String get title => 'Title';
}
''');
});
}); });
group('loadResources', () { group('loadResources', () {
......
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