Skip to content

Commit

Permalink
[gen_l10n] Multiline descriptions (#116380)
Browse files Browse the repository at this point in the history
* init

* empty commit to start google testing
  • Loading branch information
thkim1011 authored Dec 6, 2022
1 parent e2fb672 commit 21f3ce8
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 3 deletions.
6 changes: 5 additions & 1 deletion packages/flutter_tools/lib/src/localizations/gen_l10n.dart
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,11 @@ Map<String, String> pluralCases = <String, String>{
};

String generateBaseClassMethod(Message message, LocaleInfo? templateArbLocale) {
final String comment = message.description ?? 'No description provided for @${message.resourceId}.';
final String comment = message
.description
?.split('\n')
.map((String line) => ' /// $line')
.join('\n') ?? ' /// No description provided for @${message.resourceId}.';
final String templateLocaleTranslationComment = '''
/// In $templateArbLocale, this message translates to:
/// **'${generateString(message.value)}'**''';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,14 +179,14 @@ class @(class) extends @(baseLanguageClassName) {
''';

const String baseClassGetterTemplate = '''
/// @(comment)
@(comment)
///
@(templateLocaleTranslationComment)
String get @(name);
''';

const String baseClassMethodTemplate = '''
/// @(comment)
@(comment)
///
@(templateLocaleTranslationComment)
String @(name)(@(parameters));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1318,6 +1318,76 @@ class AppLocalizationsEn extends AppLocalizations {
^'''));
}
});

testWithoutContext('no description generates generic comment', () {
final Directory l10nDirectory = fs.currentDirectory.childDirectory('lib').childDirectory('l10n')
..createSync(recursive: true);
l10nDirectory.childFile(defaultTemplateArbFileName)
.writeAsStringSync(r'''
{
"helloWorld": "Hello world!"
}''');
l10nDirectory.childFile(esArbFileName)
.writeAsStringSync(singleEsMessageArbFileString);
LocalizationsGenerator(
fileSystem: fs,
inputPathString: defaultL10nPathString,
outputPathString: defaultL10nPathString,
templateArbFileName: defaultTemplateArbFileName,
outputFileString: defaultOutputFileString,
classNameString: defaultClassNameString,
logger: logger,
)
..loadResources()
..writeOutputFiles();
final File baseLocalizationsFile = fs.file(
fs.path.join(syntheticL10nPackagePath, 'output-localization-file.dart')
);
expect(baseLocalizationsFile.existsSync(), isTrue);

final String baseLocalizationsFileContents = fs.file(
fs.path.join(syntheticL10nPackagePath, 'output-localization-file.dart')
).readAsStringSync();
expect(baseLocalizationsFileContents, contains('/// No description provided for @helloWorld.'));
});

testWithoutContext('multiline descriptions are correctly formatted as comments', () {
final Directory l10nDirectory = fs.currentDirectory.childDirectory('lib').childDirectory('l10n')
..createSync(recursive: true);
l10nDirectory.childFile(defaultTemplateArbFileName)
.writeAsStringSync(r'''
{
"helloWorld": "Hello world!",
"@helloWorld": {
"description": "The generic example string in every language.\nUse this for tests!"
}
}''');
l10nDirectory.childFile(esArbFileName)
.writeAsStringSync(singleEsMessageArbFileString);
LocalizationsGenerator(
fileSystem: fs,
inputPathString: defaultL10nPathString,
outputPathString: defaultL10nPathString,
templateArbFileName: defaultTemplateArbFileName,
outputFileString: defaultOutputFileString,
classNameString: defaultClassNameString,
logger: logger,
)
..loadResources()
..writeOutputFiles();
final File baseLocalizationsFile = fs.file(
fs.path.join(syntheticL10nPackagePath, 'output-localization-file.dart')
);
expect(baseLocalizationsFile.existsSync(), isTrue);

final String baseLocalizationsFileContents = fs.file(
fs.path.join(syntheticL10nPackagePath, 'output-localization-file.dart')
).readAsStringSync();
expect(baseLocalizationsFileContents, contains('''
/// The generic example string in every language.
/// Use this for tests!'''));
});

testWithoutContext('message without placeholders - should generate code comment with description and template message translation', () {
_standardFlutterDirectoryL10nSetup(fs);
LocalizationsGenerator(
Expand Down

0 comments on commit 21f3ce8

Please sign in to comment.