Skip to content

Commit

Permalink
[gen_l10n] Better blank lines in the header of generated files (#103414)
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexV525 authored May 12, 2022
1 parent 78885ec commit aa39fa5
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 17 deletions.
6 changes: 3 additions & 3 deletions packages/flutter_tools/lib/src/localizations/gen_l10n.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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(
Expand All @@ -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
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -789,7 +789,6 @@ void main() {
expect(fs.file('/lib/l10n/bar_en.dart').readAsStringSync(), '''
HEADER
import 'bar.dart';
/// The translations for English (`en`).
Expand Down Expand Up @@ -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', () {
Expand Down

0 comments on commit aa39fa5

Please sign in to comment.