Stop forgetting to regenerate your Flutter translations! π
I got tired of manually running dart run intl_utils:generate every time I edited my .arb translation files. So I automated it with a simple file watcher script.
How to use it:
Just run this command in your terminal:
dart run scripts/watch_translations.dart
The script will watch your lib/l10n directory and automatically regenerate translations whenever you modify any .arb file.
Here's the implementation:
import 'dart:io';
void main() async {
final l10nDir = Directory('lib/l10n');
if (!l10nDir.existsSync()) {
print('Error: lib/l10n directory does not exist');
exit(1);
}
print('Watching for changes in lib/l10n/*.arb files...');
print('Press Ctrl+C to stop\n');
// Watch the directory
await for (final event in l10nDir.watch()) {
if (event.path.endsWith('.arb') && event.type == FileSystemEvent.modify) {
print('Detected change in: ${event.path}');
print('Generating translations...');
final result = await Process.run(
'dart',
['run', 'intl_utils:generate'],
runInShell: true,
);
if (result.exitCode == 0) {
print('β Translations generated successfully\n');
} else {
print('β Error generating translations:');
print(result.stderr);
}
}
}
}
What it does:
β
Monitors the lib/l10n directory for changes to .arb files
β
Detects when translation files are modified
β
Automatically runs the translation generation command
β
Provides clear feedback on success or failure
Now I can edit translation files and the generated code updates automatically. No more manual steps or missed regenerations.
The script uses Dart's built-in Directory.watch() API to monitor file system events. It's lightweight, runs in the background, and has been a huge time-saver.
Sometimes the best solutions are the simplest ones that remove friction from your workflow.
Top comments (0)