Skip to content

Commit aa39fa5

Browse files
authored
[gen_l10n] Better blank lines in the header of generated files (#103414)
1 parent 78885ec commit aa39fa5

File tree

3 files changed

+71
-17
lines changed

3 files changed

+71
-17
lines changed

packages/flutter_tools/lib/src/localizations/gen_l10n.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1133,14 +1133,14 @@ class LocalizationsGenerator {
11331133
});
11341134

11351135
return classFileTemplate
1136-
.replaceAll('@(header)', header)
1136+
.replaceAll('@(header)', header.isEmpty ? '' : '$header\n\n')
11371137
.replaceAll('@(language)', describeLocale(locale.toString()))
11381138
.replaceAll('@(baseClass)', className)
11391139
.replaceAll('@(fileName)', fileName)
11401140
.replaceAll('@(class)', '$className${locale.camelCase()}')
11411141
.replaceAll('@(localeName)', locale.toString())
11421142
.replaceAll('@(methods)', methods.join('\n\n'))
1143-
.replaceAll('@(requiresIntlImport)', _requiresIntlImport() ? "import 'package:intl/intl.dart' as intl;" : '');
1143+
.replaceAll('@(requiresIntlImport)', _requiresIntlImport() ? "import 'package:intl/intl.dart' as intl;\n\n" : '');
11441144
}
11451145

11461146
String _generateSubclass(
@@ -1284,7 +1284,7 @@ class LocalizationsGenerator {
12841284
);
12851285

12861286
return fileTemplate
1287-
.replaceAll('@(header)', header)
1287+
.replaceAll('@(header)', header.isEmpty ? '' : '$header\n')
12881288
.replaceAll('@(class)', className)
12891289
.replaceAll('@(methods)', _allMessages.map((Message message) => generateBaseClassMethod(message, _templateArbLocale)).join('\n'))
12901290
.replaceAll('@(importFile)', '$directory/$outputFileName')

packages/flutter_tools/lib/src/localizations/gen_l10n_templates.dart

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ description: The Flutter application's synthetic package.
99
''';
1010

1111
const String fileTemplate = '''
12-
@(header)
13-
import 'dart:async';
12+
@(header)import 'dart:async';
1413
1514
@(requiresFoundationImport)
1615
import 'package:flutter/widgets.dart';
@@ -19,14 +18,14 @@ import 'package:intl/intl.dart' as intl;
1918
2019
@(messageClassImports)
2120
22-
/// Callers can lookup localized strings with an instance of @(class) returned
23-
/// by `@(class).of(context)`.
21+
/// Callers can lookup localized strings with an instance of @(class)
22+
/// returned by `@(class).of(context)`.
2423
///
2524
/// Applications need to include `@(class).delegate()` in their app's
26-
/// localizationDelegates list, and the locales they support in the app's
27-
/// supportedLocales list. For example:
25+
/// `localizationDelegates` list, and the locales they support in the app's
26+
/// `supportedLocales` list. For example:
2827
///
29-
/// ```
28+
/// ```dart
3029
/// import '@(importFile)';
3130
///
3231
/// return MaterialApp(
@@ -41,14 +40,14 @@ import 'package:intl/intl.dart' as intl;
4140
/// Please make sure to update your pubspec.yaml to include the following
4241
/// packages:
4342
///
44-
/// ```
43+
/// ```yaml
4544
/// dependencies:
4645
/// # Internationalization support.
4746
/// flutter_localizations:
4847
/// sdk: flutter
4948
/// intl: any # Use the pinned version from flutter_localizations
5049
///
51-
/// # rest of dependencies
50+
/// # Rest of dependencies
5251
/// ```
5352
///
5453
/// ## iOS Applications
@@ -202,10 +201,7 @@ const String selectMethodTemplateInString = '''
202201
}''';
203202

204203
const String classFileTemplate = '''
205-
@(header)
206-
207-
@(requiresIntlImport)
208-
import '@(fileName)';
204+
@(header)@(requiresIntlImport)import '@(fileName)';
209205
210206
/// The translations for @(language) (`@(localeName)`).
211207
class @(class) extends @(baseClass) {

packages/flutter_tools/test/general.shard/generate_localizations_test.dart

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -789,7 +789,6 @@ void main() {
789789
expect(fs.file('/lib/l10n/bar_en.dart').readAsStringSync(), '''
790790
HEADER
791791
792-
793792
import 'bar.dart';
794793
795794
/// The translations for English (`en`).
@@ -839,6 +838,65 @@ flutter:
839838
),
840839
);
841840
});
841+
842+
testWithoutContext('blank lines generated nicely', () async {
843+
_standardFlutterDirectoryL10nSetup(fs);
844+
845+
// Test without headers.
846+
generateLocalizations(
847+
fileSystem: fs,
848+
options: LocalizationOptions(
849+
arbDirectory: Uri.directory(defaultL10nPathString),
850+
outputDirectory: Uri.directory(defaultL10nPathString, windows: false),
851+
templateArbFile: Uri.file(defaultTemplateArbFileName, windows: false),
852+
useSyntheticPackage: false,
853+
),
854+
logger: BufferLogger.test(),
855+
projectDir: fs.currentDirectory,
856+
dependenciesDir: fs.currentDirectory,
857+
);
858+
859+
expect(fs.file('/lib/l10n/app_localizations_en.dart').readAsStringSync(), '''
860+
import 'app_localizations.dart';
861+
862+
/// The translations for English (`en`).
863+
class AppLocalizationsEn extends AppLocalizations {
864+
AppLocalizationsEn([String locale = 'en']) : super(locale);
865+
866+
@override
867+
String get title => 'Title';
868+
}
869+
''');
870+
871+
// Test with headers.
872+
generateLocalizations(
873+
fileSystem: fs,
874+
options: LocalizationOptions(
875+
header: 'HEADER',
876+
arbDirectory: Uri.directory(defaultL10nPathString),
877+
outputDirectory: Uri.directory(defaultL10nPathString, windows: false),
878+
templateArbFile: Uri.file(defaultTemplateArbFileName, windows: false),
879+
useSyntheticPackage: false,
880+
),
881+
logger: BufferLogger.test(),
882+
projectDir: fs.currentDirectory,
883+
dependenciesDir: fs.currentDirectory,
884+
);
885+
886+
expect(fs.file('/lib/l10n/app_localizations_en.dart').readAsStringSync(), '''
887+
HEADER
888+
889+
import 'app_localizations.dart';
890+
891+
/// The translations for English (`en`).
892+
class AppLocalizationsEn extends AppLocalizations {
893+
AppLocalizationsEn([String locale = 'en']) : super(locale);
894+
895+
@override
896+
String get title => 'Title';
897+
}
898+
''');
899+
});
842900
});
843901

844902
group('loadResources', () {

0 commit comments

Comments
 (0)