[CP-beta]feat: Arbitrary format options for localizations generation (#102983) (#168035)

This pull request is created by [automatic cherry pick workflow](https://github.com/flutter/flutter/blob/main/docs/releases/Flutter-Cherrypick-Process.md#automatically-creates-a-cherry-pick-request)
Please fill in the form below, and a flutter domain expert will evaluate this cherry pick request.

### Issue Link:
What is the link to the issue this cherry-pick is addressing?

< Replace with issue link here >

### Changelog Description:
Explain this cherry pick in one line that is accessible to most Flutter developers. See [best practices](https://github.com/flutter/flutter/blob/main/docs/releases/Hotfix-Documentation-Best-Practices.md) for examples

< Replace with changelog description here >

### Impact Description:
What is the impact (ex. visual jank on Samsung phones, app crash, cannot ship an iOS app)? Does it impact development (ex. flutter doctor crashes when Android Studio is installed), or the shipping production app (the app crashes on launch)

< Replace with impact description here >

### Workaround:
Is there a workaround for this issue?

< Replace with workaround here >

### Risk:
What is the risk level of this cherry-pick?

### Test Coverage:
Are you confident that your fix is well-tested by automated tests?

### Validation Steps:
What are the steps to validate that this fix works?

< Replace with validation steps here >
This commit is contained in:
flutteractionsbot 2025-04-29 21:15:41 -07:00 committed by GitHub
parent 6423adf0dd
commit 453dd7174c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 86 additions and 12 deletions

View File

@ -356,7 +356,7 @@ class LocalizationOptions {
syntheticPackage = syntheticPackage ?? !featureFlags.isExplicitPackageDependenciesEnabled,
requiredResourceAttributes = requiredResourceAttributes ?? false,
nullableGetter = nullableGetter ?? true,
format = format ?? false,
format = format ?? true,
useEscaping = useEscaping ?? false,
suppressWarnings = suppressWarnings ?? false,
relaxSyntax = relaxSyntax ?? false,

View File

@ -202,7 +202,7 @@ flutter:
fileSystem: fileSystem,
logger: logger,
artifacts: artifacts,
processManager: processManager,
processManager: FakeProcessManager.any(),
);
await createTestCommandRunner(command).run(<String>['gen-l10n']);
@ -241,7 +241,7 @@ flutter:
fileSystem: fileSystem,
logger: logger,
artifacts: artifacts,
processManager: processManager,
processManager: FakeProcessManager.any(),
);
await createTestCommandRunner(command).run(<String>['gen-l10n']);
expect(command.usage, contains(' If this value is set to false, then '));
@ -299,6 +299,43 @@ flutter:
},
);
testUsingContext(
'dart format is not run when --no-format is passed',
() async {
final File arbFile = fileSystem.file(fileSystem.path.join('lib', 'l10n', 'app_en.arb'))
..createSync(recursive: true);
arbFile.writeAsStringSync('''
{
"helloWorld": "Hello, World!",
"@helloWorld": {
"description": "Sample description"
}
}''');
final File pubspecFile = fileSystem.file('pubspec.yaml')..createSync();
pubspecFile.writeAsStringSync(BasicProjectWithFlutterGen().pubspec);
final GenerateLocalizationsCommand command = GenerateLocalizationsCommand(
fileSystem: fileSystem,
logger: logger,
artifacts: artifacts,
processManager: processManager,
);
await createTestCommandRunner(command).run(<String>['gen-l10n', '--no-format']);
final Directory outputDirectory = fileSystem.directory(fileSystem.path.join('lib', 'l10n'));
expect(outputDirectory.existsSync(), true);
expect(outputDirectory.childFile('app_localizations_en.dart').existsSync(), true);
expect(outputDirectory.childFile('app_localizations.dart').existsSync(), true);
expect(processManager, hasNoRemainingExpectations);
},
overrides: <Type, Generator>{
FeatureFlags: enableExplicitPackageDependencies,
FileSystem: () => fileSystem,
ProcessManager: () => FakeProcessManager.any(),
},
);
testUsingContext(
'dart format is run when format: true is passed into l10n.yaml',
() async {
@ -348,6 +385,45 @@ format: true
},
);
testUsingContext(
'dart format is not running when format: false is passed into l10n.yaml',
() async {
final File arbFile = fileSystem.file(fileSystem.path.join('lib', 'l10n', 'app_en.arb'))
..createSync(recursive: true);
arbFile.writeAsStringSync('''
{
"helloWorld": "Hello, World!",
"@helloWorld": {
"description": "Sample description"
}
}''');
final File configFile = fileSystem.file('l10n.yaml')..createSync();
configFile.writeAsStringSync('''
format: false
''');
final File pubspecFile = fileSystem.file('pubspec.yaml')..createSync();
pubspecFile.writeAsStringSync(BasicProjectWithFlutterGen().pubspec);
final GenerateLocalizationsCommand command = GenerateLocalizationsCommand(
fileSystem: fileSystem,
logger: logger,
artifacts: artifacts,
processManager: processManager,
);
await createTestCommandRunner(command).run(<String>['gen-l10n']);
final Directory outputDirectory = fileSystem.directory(fileSystem.path.join('lib', 'l10n'));
expect(outputDirectory.existsSync(), true);
expect(outputDirectory.childFile('app_localizations_en.dart').existsSync(), true);
expect(outputDirectory.childFile('app_localizations.dart').existsSync(), true);
expect(processManager, hasNoRemainingExpectations);
},
overrides: <Type, Generator>{
FeatureFlags: enableExplicitPackageDependencies,
FileSystem: () => fileSystem,
ProcessManager: () => FakeProcessManager.any(),
},
);
// Regression test for https://github.com/flutter/flutter/issues/119594
testUsingContext(
'dart format is working when the untranslated messages file is produced',

View File

@ -76,7 +76,6 @@ void main() {
late MemoryFileSystem fs;
late BufferLogger logger;
late Artifacts artifacts;
late ProcessManager processManager;
late String defaultL10nPathString;
late String syntheticPackagePath;
late String syntheticL10nPackagePath;
@ -152,7 +151,6 @@ void main() {
fs = MemoryFileSystem.test();
logger = BufferLogger.test();
artifacts = Artifacts.test();
processManager = FakeProcessManager.empty();
defaultL10nPathString = fs.path.join('lib', 'l10n');
syntheticPackagePath = fs.path.join('.dart_tool', 'flutter_gen');
@ -757,7 +755,7 @@ flutter:
projectDir: projectDir,
dependenciesDir: fs.currentDirectory,
artifacts: artifacts,
processManager: processManager,
processManager: FakeProcessManager.any(),
);
});
@ -780,7 +778,7 @@ flutter:
projectDir: fs.currentDirectory,
dependenciesDir: fs.currentDirectory,
artifacts: artifacts,
processManager: processManager,
processManager: FakeProcessManager.any(),
);
});
@ -809,7 +807,7 @@ flutter:
projectDir: fs.currentDirectory,
dependenciesDir: fs.currentDirectory,
artifacts: artifacts,
processManager: processManager,
processManager: FakeProcessManager.any(),
);
expect(generator.inputDirectory.path, '/lib/l10n/');
@ -880,7 +878,7 @@ flutter:
projectDir: fs.currentDirectory,
dependenciesDir: fs.currentDirectory,
artifacts: artifacts,
processManager: processManager,
processManager: FakeProcessManager.any(),
),
throwsToolExit(
message:
@ -916,7 +914,7 @@ flutter:\r
projectDir: fs.currentDirectory,
dependenciesDir: fs.currentDirectory,
artifacts: artifacts,
processManager: processManager,
processManager: FakeProcessManager.any(),
);
final String content = getInPackageGeneratedFileContent(locale: 'en');
expect(content, contains('\r\n'));
@ -940,7 +938,7 @@ flutter:\r
projectDir: fs.currentDirectory,
dependenciesDir: fs.currentDirectory,
artifacts: artifacts,
processManager: processManager,
processManager: FakeProcessManager.any(),
);
expect(fs.file('/lib/l10n/app_localizations_en.dart').readAsStringSync(), '''
@ -973,7 +971,7 @@ class AppLocalizationsEn extends AppLocalizations {
projectDir: fs.currentDirectory,
dependenciesDir: fs.currentDirectory,
artifacts: artifacts,
processManager: processManager,
processManager: FakeProcessManager.any(),
);
expect(fs.file('/lib/l10n/app_localizations_en.dart').readAsStringSync(), '''