setupFlutterLocalizationConfiguration function
Setup complete Flutter localization configuration including l10n.yaml and pubspec.yaml updates This ensures the project is properly configured for Flutter's localization generation
Implementation
bool setupFlutterLocalizationConfiguration(String projectRoot) {
try {
print(
'\u001b[36mℹ️ Setting up Flutter localization configuration...\u001b[0m');
// 1. Find and update pubspec.yaml to ensure 'generate: true' is set
final pubspecFile = _findPubspecFile(projectRoot);
if (pubspecFile == null) {
print('\u001b[31mError: Could not find pubspec.yaml file\u001b[0m');
return false;
}
bool pubspecUpdated = false;
try {
final content = pubspecFile.readAsStringSync();
final yamlEditor = YamlEditor(content);
// Check if flutter.generate is already set to true
final yamlDoc = loadYaml(content);
bool hasGenerate = false;
if (yamlDoc is Map && yamlDoc.containsKey('flutter')) {
final flutterSection = yamlDoc['flutter'];
if (flutterSection is Map && flutterSection.containsKey('generate')) {
hasGenerate = flutterSection['generate'] == true;
}
}
if (!hasGenerate) {
// First ensure flutter section exists
try {
if (yamlDoc is! Map || !yamlDoc.containsKey('flutter')) {
yamlEditor.update(['flutter'], {});
}
// Then add/update flutter.generate to true
yamlEditor.update(['flutter', 'generate'], true);
pubspecFile.writeAsStringSync(yamlEditor.toString());
pubspecUpdated = true;
print(
'\u001b[32m✓ Added "generate: true" to pubspec.yaml flutter section\u001b[0m');
} catch (e) {
// If YAML editing fails, try string-based approach
print(
'\u001b[33m⚠️ YAML editor failed, trying string-based approach...\u001b[0m');
final lines = content.split('\n');
int flutterIndex =
lines.indexWhere((line) => line.trim() == 'flutter:');
// Check if generate: true already exists
bool hasGenerate = false;
if (flutterIndex != -1) {
for (int i = flutterIndex + 1; i < lines.length; i++) {
final line = lines[i].trim();
if (line.isEmpty || line.startsWith('#')) continue;
if (!lines[i].startsWith(' ') && line.endsWith(':'))
break; // Hit another section
if (line == 'generate: true') {
hasGenerate = true;
break;
}
}
}
if (!hasGenerate) {
if (flutterIndex == -1) {
lines.add('flutter:');
lines.add(' generate: true');
} else {
lines.insert(flutterIndex + 1, ' generate: true');
}
pubspecFile.writeAsStringSync(lines.join('\n'));
pubspecUpdated = true;
print(
'\u001b[32m✓ Added "generate: true" to pubspec.yaml flutter section\u001b[0m');
} else {
print(
'\u001b[36mℹ️ "generate: true" already present in pubspec.yaml\u001b[0m');
}
}
} else {
print(
'\u001b[36mℹ️ "generate: true" already present in pubspec.yaml\u001b[0m');
}
} catch (e) {
print(
'\u001b[33m⚠️ Could not update pubspec.yaml flutter section: $e\u001b[0m');
print(
'\u001b[36mℹ️ Please manually add "generate: true" under the flutter section\u001b[0m');
}
// 2. Create l10n.yaml configuration file if it doesn't exist
final l10nFile = File(path.join(projectRoot, 'l10n.yaml'));
bool l10nCreated = false;
if (!l10nFile.existsSync()) {
try {
final l10nConfig = '''arb-dir: lib/l10n
template-arb-file: app_en.arb
output-localization-file: app_localizations.dart
use-escaping: true
''';
l10nFile.writeAsStringSync(l10nConfig);
l10nCreated = true;
print('\u001b[32m✓ Created l10n.yaml configuration file\u001b[0m');
} catch (e) {
print('\u001b[33m⚠️ Could not create l10n.yaml: $e\u001b[0m');
print(
'\u001b[36mℹ️ Please manually create l10n.yaml in your project root\u001b[0m');
}
} else {
print('\u001b[36mℹ️ l10n.yaml already exists\u001b[0m');
}
// 3. Ensure lib/l10n directory exists
final l10nDir = Directory(path.join(projectRoot, 'lib', 'l10n'));
bool dirCreated = false;
if (!l10nDir.existsSync()) {
try {
l10nDir.createSync(recursive: true);
dirCreated = true;
print('\u001b[32m✓ Created lib/l10n directory\u001b[0m');
} catch (e) {
print('\u001b[33m⚠️ Could not create lib/l10n directory: $e\u001b[0m');
}
} else {
print('\u001b[36mℹ️ lib/l10n directory already exists\u001b[0m');
}
// 4. Provide setup summary and next steps
if (pubspecUpdated || l10nCreated || dirCreated) {
print(
'\u001b[32m✓ Flutter localization configuration setup completed\u001b[0m');
if (pubspecUpdated) {
print(
'\u001b[36mℹ️ Run "flutter pub get" to apply pubspec.yaml changes\u001b[0m');
}
} else {
print(
'\u001b[36mℹ️ Flutter localization configuration already properly set up\u001b[0m');
}
return true;
} catch (e) {
print(
'\u001b[31mError setting up Flutter localization configuration: $e\u001b[0m');
return false;
}
}