[gen_l10n] Fallback feature for untranslated messages (#53374)

* Generate methods using template resources if they do not exist in other locales

* Added a flag to either output of messages that have not been translated with detail into a file, or display a summary on the terminal.

* Add integration test for fallback message usage
This commit is contained in:
Shi-Hao Hong
2020-04-08 18:55:50 -07:00
committed by GitHub
parent 173c93d9a2
commit 5d63637e43
5 changed files with 223 additions and 105 deletions

View File

@@ -2,6 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'dart:convert';
import 'dart:io';
import 'package:file/file.dart';
@@ -25,6 +26,17 @@ const String singleMessageArbFileString = '''
"description": "Title for the application"
}
}''';
const String twoMessageArbFileString = '''
{
"title": "Title",
"@title": {
"description": "Title for the application"
},
"subtitle": "Subtitle",
"@subtitle": {
"description": "Subtitle for the application"
}
}''';
const String esArbFileName = 'app_es.arb';
const String singleEsMessageArbFileString = '''
{
@@ -277,6 +289,43 @@ void main() {
expect(generator.header, '/// Sample header in a text file');
});
test('correctly creates an unimplemented messages file', () {
fs.currentDirectory.childDirectory('lib').childDirectory('l10n')
..createSync(recursive: true)
..childFile(defaultTemplateArbFileName).writeAsStringSync(twoMessageArbFileString)
..childFile(esArbFileName).writeAsStringSync(singleEsMessageArbFileString);
LocalizationsGenerator generator;
try {
generator = LocalizationsGenerator(fs);
generator
..initialize(
l10nDirectoryPath: defaultArbPathString,
templateArbFileName: defaultTemplateArbFileName,
outputFileString: defaultOutputFileString,
classNameString: defaultClassNameString,
)
..loadResources()
..generateCode()
..outputUnimplementedMessages(path.join('lib', 'l10n', 'unimplemented_message_translations.json'));
} on L10nException catch (e) {
fail('Generating output should not fail: \n${e.message}');
}
final File unimplementedOutputFile = fs.file(
path.join('lib', 'l10n', 'unimplemented_message_translations.json'),
);
final String unimplementedOutputString = unimplementedOutputFile.readAsStringSync();
try {
// Since ARB file is essentially JSON, decoding it should not fail.
json.decode(unimplementedOutputString);
} on Exception {
fail('Parsing arb file should not fail');
}
expect(unimplementedOutputString, contains('es'));
expect(unimplementedOutputString, contains('subtitle'));
});
test('setting both a headerString and a headerFile should fail', () {
fs.currentDirectory.childDirectory('lib').childDirectory('l10n')
..createSync(recursive: true)