Unverified Commit 633edb64 authored by Shi-Hao Hong's avatar Shi-Hao Hong Committed by GitHub

[gen-l10n] Cleans up formatting of the generated file (#79517)

parent 53e060cc
...@@ -136,23 +136,21 @@ class _StockStringsDelegate extends LocalizationsDelegate<StockStrings> { ...@@ -136,23 +136,21 @@ class _StockStringsDelegate extends LocalizationsDelegate<StockStrings> {
StockStrings _lookupStockStrings(Locale locale) { StockStrings _lookupStockStrings(Locale locale) {
// Lookup logic when language+country codes are specified.
// Lookup logic when language+country codes are specified. switch (locale.languageCode) {
switch (locale.languageCode) { case 'en': {
case 'en': { switch (locale.countryCode) {
switch (locale.countryCode) { case 'US': return StockStringsEnUs();
case 'US': return StockStringsEnUs(); }
break;
}
} }
break;
}
}
// Lookup logic when only language code is specified. // Lookup logic when only language code is specified.
switch (locale.languageCode) { switch (locale.languageCode) {
case 'en': return StockStringsEn(); case 'en': return StockStringsEn();
case 'es': return StockStringsEs(); case 'es': return StockStringsEs();
} }
throw FlutterError( throw FlutterError(
'StockStrings.delegate failed to load unsupported locale "$locale". This is likely ' 'StockStrings.delegate failed to load unsupported locale "$locale". This is likely '
......
...@@ -346,6 +346,22 @@ String generateBaseClassMethod(Message message, LocaleInfo? templateArbLocale) { ...@@ -346,6 +346,22 @@ String generateBaseClassMethod(Message message, LocaleInfo? templateArbLocale) {
.replaceAll('@(name)', message.resourceId); .replaceAll('@(name)', message.resourceId);
} }
// Add spaces to pad the start of each line. Skips the first line
// assuming that the padding is already present.
String _addSpaces(String message, {int spaces = 0}) {
bool isFirstLine = true;
return message
.split('\n')
.map((String value) {
if (isFirstLine) {
isFirstLine = false;
return value;
}
return value.padLeft(spaces);
})
.join('\n');
}
String _generateLookupByAllCodes( String _generateLookupByAllCodes(
AppResourceBundleCollection allBundles, AppResourceBundleCollection allBundles,
String Function(LocaleInfo) generateSwitchClauseTemplate, String Function(LocaleInfo) generateSwitchClauseTemplate,
...@@ -365,7 +381,7 @@ String _generateLookupByAllCodes( ...@@ -365,7 +381,7 @@ String _generateLookupByAllCodes(
return allCodesLookupTemplate.replaceAll( return allCodesLookupTemplate.replaceAll(
'@(allCodesSwitchClauses)', '@(allCodesSwitchClauses)',
switchClauses.join('\n '), switchClauses.join('\n '),
); );
} }
...@@ -383,13 +399,20 @@ String _generateLookupByScriptCode( ...@@ -383,13 +399,20 @@ String _generateLookupByScriptCode(
return null; return null;
} }
return nestedSwitchTemplate return _addSpaces(nestedSwitchTemplate
.replaceAll('@(languageCode)', language) .replaceAll('@(languageCode)', language)
.replaceAll('@(code)', 'scriptCode') .replaceAll('@(code)', 'scriptCode')
.replaceAll('@(switchClauses)', localesWithScriptCodes.map((LocaleInfo locale) { .replaceAll('@(switchClauses)',
return generateSwitchClauseTemplate(locale) _addSpaces(
.replaceAll('@(case)', locale.scriptCode!); localesWithScriptCodes.map((LocaleInfo locale) {
}).join('\n ')); return generateSwitchClauseTemplate(locale)
.replaceAll('@(case)', locale.scriptCode!);
}).join('\n'),
spaces: 8,
),
),
spaces: 4,
);
}).whereType<String>(); }).whereType<String>();
if (switchClauses.isEmpty) { if (switchClauses.isEmpty) {
...@@ -398,7 +421,7 @@ String _generateLookupByScriptCode( ...@@ -398,7 +421,7 @@ String _generateLookupByScriptCode(
return languageCodeSwitchTemplate return languageCodeSwitchTemplate
.replaceAll('@(comment)', '// Lookup logic when language+script codes are specified.') .replaceAll('@(comment)', '// Lookup logic when language+script codes are specified.')
.replaceAll('@(switchClauses)', switchClauses.join('\n '), .replaceAll('@(switchClauses)', switchClauses.join('\n '),
); );
} }
...@@ -416,13 +439,18 @@ String _generateLookupByCountryCode( ...@@ -416,13 +439,18 @@ String _generateLookupByCountryCode(
return null; return null;
} }
return nestedSwitchTemplate return _addSpaces(
.replaceAll('@(languageCode)', language) nestedSwitchTemplate
.replaceAll('@(code)', 'countryCode') .replaceAll('@(languageCode)', language)
.replaceAll('@(switchClauses)', localesWithCountryCodes.map((LocaleInfo locale) { .replaceAll('@(code)', 'countryCode')
return generateSwitchClauseTemplate(locale) .replaceAll('@(switchClauses)', _addSpaces(
.replaceAll('@(case)', locale.countryCode!); localesWithCountryCodes.map((LocaleInfo locale) {
}).join('\n ')); return generateSwitchClauseTemplate(locale).replaceAll('@(case)', locale.countryCode!);
}).join('\n'),
spaces: 4,
)),
spaces: 4,
);
}).whereType<String>(); }).whereType<String>();
if (switchClauses.isEmpty) { if (switchClauses.isEmpty) {
...@@ -451,7 +479,7 @@ String _generateLookupByLanguageCode( ...@@ -451,7 +479,7 @@ String _generateLookupByLanguageCode(
return localesWithLanguageCode.map((LocaleInfo locale) { return localesWithLanguageCode.map((LocaleInfo locale) {
return generateSwitchClauseTemplate(locale) return generateSwitchClauseTemplate(locale)
.replaceAll('@(case)', locale.languageCode); .replaceAll('@(case)', locale.languageCode);
}).join('\n '); }).join('\n ');
}).whereType<String>(); }).whereType<String>();
if (switchClauses.isEmpty) { if (switchClauses.isEmpty) {
...@@ -1133,7 +1161,11 @@ class LocalizationsGenerator { ...@@ -1133,7 +1161,11 @@ class LocalizationsGenerator {
.replaceAll('@(requiresFoundationImport)', useDeferredLoading ? '' : "import 'package:flutter/foundation.dart';") .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 ? '' : '!')
// Removes all trailing whitespace from the generated file.
.split('\n').map((String line) => line.trimRight()).join('\n')
// Cleans out unnecessary newlines.
.replaceAll('\n\n\n', '\n\n');
} }
bool _containsPluralMessage() => _allMessages.any((Message message) => message.isPlural); bool _containsPluralMessage() => _allMessages.any((Message message) => message.isPlural);
......
...@@ -263,15 +263,15 @@ case '@(languageCode)': { ...@@ -263,15 +263,15 @@ case '@(languageCode)': {
}'''; }''';
const String languageCodeSwitchTemplate = ''' const String languageCodeSwitchTemplate = '''
@(comment) @(comment)
switch (locale.languageCode) { switch (locale.languageCode) {
@(switchClauses) @(switchClauses)
} }
'''; ''';
const String allCodesLookupTemplate = ''' const String allCodesLookupTemplate = '''
// Lookup logic when language+script+country codes are specified. // Lookup logic when language+script+country codes are specified.
switch (locale.toString()) { switch (locale.toString()) {
@(allCodesSwitchClauses) @(allCodesSwitchClauses)
} }
'''; ''';
...@@ -1809,6 +1809,42 @@ import 'output-localization-file_en.dart' deferred as output-localization-file_e ...@@ -1809,6 +1809,42 @@ import 'output-localization-file_en.dart' deferred as output-localization-file_e
expect(localizationsFile, contains(intlImportDartCode)); expect(localizationsFile, contains(intlImportDartCode));
}); });
testUsingContext('check indentation on generated files', () {
_standardFlutterDirectoryL10nSetup(fs);
try {
LocalizationsGenerator(
fileSystem: fs,
inputPathString: defaultL10nPathString,
outputPathString: defaultL10nPathString,
templateArbFileName: defaultTemplateArbFileName,
outputFileString: defaultOutputFileString,
classNameString: defaultClassNameString,
useDeferredLoading: false,
)
..loadResources()
..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();
// Tests a few of the lines in the generated code.
// Localizations lookup code
expect(localizationsFile.contains(' switch (locale.languageCode) {'), true);
expect(localizationsFile.contains(" case 'en': return AppLocalizationsEn();"), true);
expect(localizationsFile.contains(" case 'es': return AppLocalizationsEs();"), true);
expect(localizationsFile.contains(' }'), true);
// Supported locales list
expect(localizationsFile.contains(' static const List<Locale> supportedLocales = <Locale>['), true);
expect(localizationsFile.contains(" Locale('en'),"), true);
expect(localizationsFile.contains(" Locale('es')"), true);
expect(localizationsFile.contains(' ];'), true);
});
testUsingContext('foundation package import should be omitted from file template when deferred loading = true', () { testUsingContext('foundation package import should be omitted from file template when deferred loading = true', () {
fs.currentDirectory.childDirectory('lib').childDirectory('l10n')..createSync(recursive: true) fs.currentDirectory.childDirectory('lib').childDirectory('l10n')..createSync(recursive: true)
..childFile(defaultTemplateArbFileName).writeAsStringSync(singleMessageArbFileString) ..childFile(defaultTemplateArbFileName).writeAsStringSync(singleMessageArbFileString)
......
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