Unverified Commit 550281e5 authored by J-P Nurmi's avatar J-P Nurmi Committed by GitHub

[gen_l10n] retain full output file suffix (#88362)

parent 2b33dae9
...@@ -1167,6 +1167,12 @@ class LocalizationsGenerator { ...@@ -1167,6 +1167,12 @@ class LocalizationsGenerator {
final String directory = _fs.path.basename(outputDirectory.path); final String directory = _fs.path.basename(outputDirectory.path);
final String outputFileName = _fs.path.basename(baseOutputFile.path); final String outputFileName = _fs.path.basename(baseOutputFile.path);
if (!outputFileName.endsWith('.dart')) {
throw L10nException(
"The 'output-localization-file', $outputFileName, is invalid.\n"
'The file name must have a .dart extension.'
);
}
final Iterable<String> supportedLocalesCode = supportedLocales.map((LocaleInfo locale) { final Iterable<String> supportedLocalesCode = supportedLocales.map((LocaleInfo locale) {
final String languageCode = locale.languageCode; final String languageCode = locale.languageCode;
...@@ -1189,10 +1195,18 @@ class LocalizationsGenerator { ...@@ -1189,10 +1195,18 @@ class LocalizationsGenerator {
); );
final List<LocaleInfo> allLocales = _allBundles.locales.toList()..sort(); final List<LocaleInfo> allLocales = _allBundles.locales.toList()..sort();
final String fileName = outputFileName.split('.')[0]; final int extensionIndex = outputFileName.indexOf('.');
if (extensionIndex <= 0) {
throw L10nException(
"The 'output-localization-file', $outputFileName, is invalid.\n"
'The base name cannot be empty.'
);
}
final String fileName = outputFileName.substring(0, extensionIndex);
final String fileExtension = outputFileName.substring(extensionIndex + 1);
for (final LocaleInfo locale in allLocales) { for (final LocaleInfo locale in allLocales) {
if (isBaseClassLocale(locale, locale.languageCode)) { if (isBaseClassLocale(locale, locale.languageCode)) {
final File languageMessageFile = outputDirectory.childFile('${fileName}_$locale.dart'); final File languageMessageFile = outputDirectory.childFile('${fileName}_$locale.$fileExtension');
// Generate the template for the base class file. Further string // Generate the template for the base class file. Further string
// interpolation will be done to determine if there are // interpolation will be done to determine if there are
...@@ -1229,9 +1243,9 @@ class LocalizationsGenerator { ...@@ -1229,9 +1243,9 @@ class LocalizationsGenerator {
.map((LocaleInfo locale) { .map((LocaleInfo locale) {
final String library = '${fileName}_${locale.toString()}'; final String library = '${fileName}_${locale.toString()}';
if (useDeferredLoading) { if (useDeferredLoading) {
return "import '$library.dart' deferred as $library;"; return "import '$library.$fileExtension' deferred as $library;";
} else { } else {
return "import '$library.dart';"; return "import '$library.$fileExtension';";
} }
}) })
.toList() .toList()
......
...@@ -751,7 +751,7 @@ void main() { ...@@ -751,7 +751,7 @@ void main() {
arbDirectory: Uri.directory(defaultL10nPathString), arbDirectory: Uri.directory(defaultL10nPathString),
deferredLoading: true, deferredLoading: true,
outputClass: 'Foo', outputClass: 'Foo',
outputLocalizationsFile: Uri.file('bar', windows: false), outputLocalizationsFile: Uri.file('bar.dart', windows: false),
outputDirectory: Uri.directory(defaultL10nPathString, windows: false), outputDirectory: Uri.directory(defaultL10nPathString, windows: false),
preferredSupportedLocales: <String>['es'], preferredSupportedLocales: <String>['es'],
templateArbFile: Uri.file(defaultTemplateArbFileName, windows: false), templateArbFile: Uri.file(defaultTemplateArbFileName, windows: false),
...@@ -773,7 +773,7 @@ void main() { ...@@ -773,7 +773,7 @@ void main() {
expect(generator.inputDirectory.path, '/lib/l10n/'); expect(generator.inputDirectory.path, '/lib/l10n/');
expect(generator.outputDirectory.path, '/lib/l10n/'); expect(generator.outputDirectory.path, '/lib/l10n/');
expect(generator.templateArbFile.path, '/lib/l10n/app_en.arb'); expect(generator.templateArbFile.path, '/lib/l10n/app_en.arb');
expect(generator.baseOutputFile.path, '/lib/l10n/bar'); expect(generator.baseOutputFile.path, '/lib/l10n/bar.dart');
expect(generator.className, 'Foo'); expect(generator.className, 'Foo');
expect(generator.preferredSupportedLocales.single, LocaleInfo.fromString('es')); expect(generator.preferredSupportedLocales.single, LocaleInfo.fromString('es'));
expect(generator.header, 'HEADER'); expect(generator.header, 'HEADER');
...@@ -790,7 +790,7 @@ void main() { ...@@ -790,7 +790,7 @@ void main() {
HEADER HEADER
import 'bar'; import 'bar.dart';
/// The translations for English (`en`). /// The translations for English (`en`).
class FooEn extends Foo { class FooEn extends Foo {
...@@ -1322,6 +1322,94 @@ import 'output-localization-file_zh.dart'; ...@@ -1322,6 +1322,94 @@ import 'output-localization-file_zh.dart';
''')); '''));
}); });
// Regression test for https://github.com/flutter/flutter/issues/88356
testWithoutContext('full output file suffix is retained', () {
fs.currentDirectory.childDirectory('lib').childDirectory('l10n')..createSync(recursive: true)
..childFile(defaultTemplateArbFileName).writeAsStringSync(singleMessageArbFileString);
LocalizationsGenerator(
fileSystem: fs,
inputPathString: defaultL10nPathString,
outputPathString: defaultL10nPathString,
templateArbFileName: defaultTemplateArbFileName,
outputFileString: 'output-localization-file.g.dart',
classNameString: defaultClassNameString,
)
..loadResources()
..writeOutputFiles(BufferLogger.test());
final String baseLocalizationsFile = fs.file(
fs.path.join(syntheticL10nPackagePath, 'output-localization-file.g.dart'),
).readAsStringSync();
expect(baseLocalizationsFile, contains(
'''
import 'output-localization-file_en.g.dart';
'''));
final String englishLocalizationsFile = fs.file(
fs.path.join(syntheticL10nPackagePath, 'output-localization-file_en.g.dart'),
).readAsStringSync();
expect(englishLocalizationsFile, contains(
'''
import 'output-localization-file.g.dart';
'''));
});
testWithoutContext('throws an exception when invalid output file name is passed in', () {
fs.currentDirectory.childDirectory('lib').childDirectory('l10n')..createSync(recursive: true)
..childFile(defaultTemplateArbFileName).writeAsStringSync(singleMessageArbFileString);
expect(
() {
LocalizationsGenerator(
fileSystem: fs,
inputPathString: defaultL10nPathString,
outputPathString: defaultL10nPathString,
templateArbFileName: defaultTemplateArbFileName,
outputFileString: 'asdf',
classNameString: defaultClassNameString,
)
..loadResources()
..writeOutputFiles(BufferLogger.test());
},
throwsA(isA<L10nException>().having(
(L10nException e) => e.message,
'message',
allOf(
contains('output-localization-file'),
contains('asdf'),
contains('is invalid'),
contains('The file name must have a .dart extension.'),
),
)),
);
expect(
() {
LocalizationsGenerator(
fileSystem: fs,
inputPathString: defaultL10nPathString,
outputPathString: defaultL10nPathString,
templateArbFileName: defaultTemplateArbFileName,
outputFileString: '.g.dart',
classNameString: defaultClassNameString,
)
..loadResources()
..writeOutputFiles(BufferLogger.test());
},
throwsA(isA<L10nException>().having(
(L10nException e) => e.message,
'message',
allOf(
contains('output-localization-file'),
contains('.g.dart'),
contains('is invalid'),
contains('The base name cannot be empty.'),
),
)),
);
});
testWithoutContext('imports are deferred and loaded when useDeferredImports are set', () { testWithoutContext('imports are deferred and loaded when useDeferredImports are set', () {
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