From 96fd143cd7bc1e4f66bb96698d5252c3e99a16ec Mon Sep 17 00:00:00 2001 From: Shi-Hao Hong Date: Wed, 11 Mar 2020 13:55:54 -0700 Subject: [PATCH] [gen_l10n] Optional Headers (#52125) * Expose --header and --header-file arguments for gen_l10n --- .../test_apps/stocks/lib/i18n/header.txt | 3 + .../test_apps/stocks/lib/i18n/regenerate.md | 2 +- dev/tools/localization/bin/gen_l10n.dart | 21 ++++ dev/tools/localization/gen_l10n.dart | 30 ++++- .../localization/gen_l10n_templates.dart | 1 + .../test/localization/gen_l10n_test.dart | 110 +++++++++++++++++- 6 files changed, 159 insertions(+), 8 deletions(-) create mode 100644 dev/benchmarks/test_apps/stocks/lib/i18n/header.txt diff --git a/dev/benchmarks/test_apps/stocks/lib/i18n/header.txt b/dev/benchmarks/test_apps/stocks/lib/i18n/header.txt new file mode 100644 index 0000000000..854d12e7cb --- /dev/null +++ b/dev/benchmarks/test_apps/stocks/lib/i18n/header.txt @@ -0,0 +1,3 @@ +// Copyright 2014 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. diff --git a/dev/benchmarks/test_apps/stocks/lib/i18n/regenerate.md b/dev/benchmarks/test_apps/stocks/lib/i18n/regenerate.md index 236f99693a..2839babc32 100644 --- a/dev/benchmarks/test_apps/stocks/lib/i18n/regenerate.md +++ b/dev/benchmarks/test_apps/stocks/lib/i18n/regenerate.md @@ -18,7 +18,7 @@ for more info. ```dart dart ${FLUTTER_PATH}/dev/tools/localization/bin/gen_l10n.dart --arb-dir=lib/i18n \ --template-arb-file=stocks_en.arb --output-localization-file=stock_strings.dart \ - --output-class=StockStrings + --output-class=StockStrings --header-file=header.txt ``` The `StockStrings` class creates a delegate that performs message lookups diff --git a/dev/tools/localization/bin/gen_l10n.dart b/dev/tools/localization/bin/gen_l10n.dart index 9bfcd79572..0cd55e943a 100644 --- a/dev/tools/localization/bin/gen_l10n.dart +++ b/dev/tools/localization/bin/gen_l10n.dart @@ -56,6 +56,23 @@ Future main(List arguments) async { "For example, pass in ['en_US'] if you would like your app to " 'default to American English if a device supports it.', ); + parser.addOption( + 'header', + help: 'The header to prepend to the generated Dart localizations ' + 'files. This option takes in a string. \n\n' + 'For example, pass in "/// All localized files." if you would ' + 'like this string prepended to the generated Dart file. \n\n' + 'Alternatively, see the `header-file` option to pass in a text ' + 'file for longer headers.' + ); + parser.addOption( + 'header-file', + help: 'The header to prepend to the generated Dart localizations ' + 'files. The value of this option is the name of the file that ' + 'contains the header text. \n\n' + 'Alternatively, see the `header` option to pass in a string ' + 'for a simpler header.' + ); final argslib.ArgResults results = parser.parse(arguments); if (results['help'] == true) { @@ -70,6 +87,8 @@ Future main(List arguments) async { final String templateArbFileName = results['template-arb-file'] as String; final String classNameString = results['output-class'] as String; final String preferredSupportedLocaleString = results['preferred-supported-locales'] as String; + final String headerString = results['header'] as String; + final String headerFile = results['header-file'] as String; const local.LocalFileSystem fs = local.LocalFileSystem(); final LocalizationsGenerator localizationsGenerator = LocalizationsGenerator(fs); @@ -82,6 +101,8 @@ Future main(List arguments) async { outputFileString: outputFileString, classNameString: classNameString, preferredSupportedLocaleString: preferredSupportedLocaleString, + headerString: headerString, + headerFile: headerFile, ) ..loadResources() ..writeOutputFile(); diff --git a/dev/tools/localization/gen_l10n.dart b/dev/tools/localization/gen_l10n.dart index 4817b0ead8..e827383cc6 100644 --- a/dev/tools/localization/gen_l10n.dart +++ b/dev/tools/localization/gen_l10n.dart @@ -262,7 +262,6 @@ class LocalizationsGenerator { Iterable _allMessages; AppResourceBundleCollection _allBundles; - /// The reference to the project's l10n directory. /// /// It is assumed that all input files (e.g. [templateArbFile], arb files @@ -322,6 +321,9 @@ class LocalizationsGenerator { /// [l10nDirectory]. final Set supportedLocales = {}; + /// The header to be prepended to the generated Dart localization file. + String header = ''; + /// Initializes [l10nDirectory], [templateArbFile], [outputFile] and [className]. /// /// Throws an [L10nException] when a provided configuration is not allowed @@ -335,11 +337,14 @@ class LocalizationsGenerator { String outputFileString, String classNameString, String preferredSupportedLocaleString, + String headerString, + String headerFile, }) { setL10nDirectory(l10nDirectoryPath); setTemplateArbFile(templateArbFileName); setOutputFile(outputFileString); setPreferredSupportedLocales(preferredSupportedLocaleString); + _setHeader(headerString, headerFile); className = classNameString; } @@ -448,6 +453,28 @@ class LocalizationsGenerator { } } + void _setHeader(String headerString, String headerFile) { + if (headerString != null && headerFile != null) { + throw L10nException( + 'Cannot accept both header and header file arguments. \n' + 'Please make sure to define only one or the other. ' + ); + } + + if (headerString != null) { + header = headerString; + } else if (headerFile != null) { + try { + header = _fs.file(path.join(l10nDirectory.path, headerFile)).readAsStringSync(); + } on FileSystemException catch (error) { + throw L10nException ( + 'Failed to read header file: "$headerFile". \n' + 'FileSystemException: ${error.message}' + ); + } + } + } + static bool _isValidGetterAndMethodName(String name) { // Public Dart method name must not start with an underscore if (name[0] == '_') @@ -525,6 +552,7 @@ class LocalizationsGenerator { final String lookupBody = generateLookupBody(_allBundles, className); return fileTemplate + .replaceAll('@(header)', header) .replaceAll('@(class)', className) .replaceAll('@(methods)', _allMessages.map(generateBaseClassMethod).join('\n')) .replaceAll('@(importFile)', '$directory/$outputFileName') diff --git a/dev/tools/localization/gen_l10n_templates.dart b/dev/tools/localization/gen_l10n_templates.dart index ad34cade63..96f26bb4fc 100644 --- a/dev/tools/localization/gen_l10n_templates.dart +++ b/dev/tools/localization/gen_l10n_templates.dart @@ -3,6 +3,7 @@ // found in the LICENSE file. const String fileTemplate = ''' +@(header) import 'dart:async'; import 'package:flutter/foundation.dart'; diff --git a/dev/tools/test/localization/gen_l10n_test.dart b/dev/tools/test/localization/gen_l10n_test.dart index 058c0dccc5..a08999e1fd 100644 --- a/dev/tools/test/localization/gen_l10n_test.dart +++ b/dev/tools/test/localization/gen_l10n_test.dart @@ -233,6 +233,104 @@ void main() { }); }); + test('correctly adds a headerString when it is set', () { + _standardFlutterDirectoryL10nSetup(fs); + + LocalizationsGenerator generator; + try { + generator = LocalizationsGenerator(fs); + generator.initialize( + l10nDirectoryPath: defaultArbPathString, + templateArbFileName: defaultTemplateArbFileName, + outputFileString: defaultOutputFileString, + classNameString: defaultClassNameString, + headerString: '/// Sample header', + ); + } on L10nException catch (e) { + fail('Setting a header through a String should not fail: \n${e.message}'); + } + + expect(generator.header, '/// Sample header'); + }); + + test('correctly adds a headerFile when it is set', () { + fs.currentDirectory.childDirectory('lib').childDirectory('l10n') + ..createSync(recursive: true) + ..childFile(defaultTemplateArbFileName).writeAsStringSync(singleMessageArbFileString) + ..childFile(esArbFileName).writeAsStringSync(singleEsMessageArbFileString) + ..childFile('header.txt').writeAsStringSync('/// Sample header in a text file'); + + LocalizationsGenerator generator; + try { + generator = LocalizationsGenerator(fs); + generator.initialize( + l10nDirectoryPath: defaultArbPathString, + templateArbFileName: defaultTemplateArbFileName, + outputFileString: defaultOutputFileString, + classNameString: defaultClassNameString, + headerFile: 'header.txt', + ); + } on L10nException catch (e) { + fail('Setting a header through a file should not fail: \n${e.message}'); + } + + expect(generator.header, '/// Sample header in a text file'); + }); + + test('setting both a headerString and a headerFile should fail', () { + fs.currentDirectory.childDirectory('lib').childDirectory('l10n') + ..createSync(recursive: true) + ..childFile(defaultTemplateArbFileName).writeAsStringSync(singleMessageArbFileString) + ..childFile(esArbFileName).writeAsStringSync(singleEsMessageArbFileString) + ..childFile('header.txt').writeAsStringSync('/// Sample header in a text file'); + + LocalizationsGenerator generator; + try { + generator = LocalizationsGenerator(fs); + generator.initialize( + l10nDirectoryPath: defaultArbPathString, + templateArbFileName: defaultTemplateArbFileName, + outputFileString: defaultOutputFileString, + classNameString: defaultClassNameString, + headerString: '/// Sample header for localizations file.', + headerFile: 'header.txt', + ); + } on L10nException catch (e) { + expect(e.message, contains('Cannot accept both header and header file arguments')); + return; + } + + fail('Setting both headerFile and headerString should fail'); + }); + + test('setting a headerFile that does not exist should fail', () { + final Directory l10nDirectory = fs.currentDirectory.childDirectory('lib').childDirectory('l10n') + ..createSync(recursive: true); + l10nDirectory.childFile(defaultTemplateArbFileName) + .writeAsStringSync(singleMessageArbFileString); + l10nDirectory.childFile(esArbFileName) + .writeAsStringSync(singleEsMessageArbFileString); + l10nDirectory.childFile('header.txt') + .writeAsStringSync('/// Sample header in a text file'); + + LocalizationsGenerator generator; + try { + generator = LocalizationsGenerator(fs); + generator.initialize( + l10nDirectoryPath: defaultArbPathString, + templateArbFileName: defaultTemplateArbFileName, + outputFileString: defaultOutputFileString, + classNameString: defaultClassNameString, + headerFile: 'header.tx', // Intentionally spelled incorrectly + ); + } on L10nException catch (e) { + expect(e.message, contains('Failed to read header file')); + return; + } + + fail('Setting headerFile that does not exist should fail'); + }); + group('loadResources', () { test('correctly initializes supportedLocales and supportedLanguageCodes properties', () { _standardFlutterDirectoryL10nSetup(fs); @@ -248,7 +346,7 @@ void main() { ); generator.loadResources(); } on L10nException catch (e) { - fail('Setting language and locales should not fail: \n$e'); + fail('Setting language and locales should not fail: \n${e.message}'); } expect(generator.supportedLocales.contains(LocaleInfo.fromString('en')), true); @@ -277,7 +375,7 @@ void main() { ); generator.loadResources(); } on L10nException catch (e) { - fail('Setting language and locales should not fail: \n$e'); + fail('Setting language and locales should not fail: \n${e.message}'); } expect(generator.supportedLocales.first, LocaleInfo.fromString('en')); @@ -308,7 +406,7 @@ void main() { ); generator.loadResources(); } on L10nException catch (e) { - fail('Setting language and locales should not fail: \n$e'); + fail('Setting language and locales should not fail: \n${e.message}'); } expect(generator.supportedLocales.first, LocaleInfo.fromString('zh')); @@ -419,7 +517,7 @@ void main() { ); generator.loadResources(); } on L10nException catch (e) { - fail('Setting language and locales should not fail: \n$e'); + fail('Setting language and locales should not fail: \n${e.message}'); } if (Platform.isWindows) { @@ -470,7 +568,7 @@ void main() { ); generator.loadResources(); } on L10nException catch (e) { - fail('Setting language and locales should not fail: \n$e'); + fail('Setting language and locales should not fail: \n${e.message}'); } expect(generator.supportedLocales.contains(LocaleInfo.fromString('en')), true); @@ -514,7 +612,7 @@ void main() { ); generator.loadResources(); } on L10nException catch (e) { - fail('Setting language and locales should not fail: \n$e'); + fail('Setting language and locales should not fail: \n${e.message}'); } // @@locale property should hold higher priority