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 {
});
return classFileTemplate
.replaceAll('@(header)', header)
.replaceAll('@(header)', header.isEmpty ? '' : '$header\n\n')
.replaceAll('@(language)', describeLocale(locale.toString()))
.replaceAll('@(baseClass)', className)
.replaceAll('@(fileName)', fileName)
.replaceAll('@(class)', '$className${locale.camelCase()}')
.replaceAll('@(localeName)', locale.toString())
.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(
......@@ -1284,7 +1284,7 @@ class LocalizationsGenerator {
);
return fileTemplate
.replaceAll('@(header)', header)
.replaceAll('@(header)', header.isEmpty ? '' : '$header\n')
.replaceAll('@(class)', className)
.replaceAll('@(methods)', _allMessages.map((Message message) => generateBaseClassMethod(message, _templateArbLocale)).join('\n'))
.replaceAll('@(importFile)', '$directory/$outputFileName')
......
......@@ -9,8 +9,7 @@ description: The Flutter application's synthetic package.
''';
const String fileTemplate = '''
@(header)
import 'dart:async';
@(header)import 'dart:async';
@(requiresFoundationImport)
import 'package:flutter/widgets.dart';
......@@ -19,14 +18,14 @@ import 'package:intl/intl.dart' as intl;
@(messageClassImports)
/// Callers can lookup localized strings with an instance of @(class) returned
/// by `@(class).of(context)`.
/// Callers can lookup localized strings with an instance of @(class)
/// returned by `@(class).of(context)`.
///
/// Applications need to include `@(class).delegate()` in their app's
/// localizationDelegates list, and the locales they support in the app's
/// supportedLocales list. For example:
/// `localizationDelegates` list, and the locales they support in the app's
/// `supportedLocales` list. For example:
///
/// ```
/// ```dart
/// import '@(importFile)';
///
/// return MaterialApp(
......@@ -41,14 +40,14 @@ import 'package:intl/intl.dart' as intl;
/// Please make sure to update your pubspec.yaml to include the following
/// packages:
///
/// ```
/// ```yaml
/// dependencies:
/// # Internationalization support.
/// flutter_localizations:
/// sdk: flutter
/// intl: any # Use the pinned version from flutter_localizations
///
/// # rest of dependencies
/// # Rest of dependencies
/// ```
///
/// ## iOS Applications
......@@ -202,10 +201,7 @@ const String selectMethodTemplateInString = '''
}''';
const String classFileTemplate = '''
@(header)
@(requiresIntlImport)
import '@(fileName)';
@(header)@(requiresIntlImport)import '@(fileName)';
/// The translations for @(language) (`@(localeName)`).
class @(class) extends @(baseClass) {
......
......@@ -789,7 +789,6 @@ void main() {
expect(fs.file('/lib/l10n/bar_en.dart').readAsStringSync(), '''
HEADER
import 'bar.dart';
/// The translations for English (`en`).
......@@ -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', () {
......
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