Skip to content

Instantly share code, notes, and snippets.

@JEuler
Created August 9, 2025 10:54
Show Gist options
  • Save JEuler/4441010357adee8ffd5cf4de2fcdd516 to your computer and use it in GitHub Desktop.
Save JEuler/4441010357adee8ffd5cf4de2fcdd516 to your computer and use it in GitHub Desktop.
Clearn ARB files from Metadata
#!/usr/bin/env dart
import 'dart:io';
import 'dart:convert';
void main() async {
final l10nDir = Directory('lib/l10n');
if (!await l10nDir.exists()) {
print('❌ lib/l10n directory not found');
exit(1);
}
print('🧹 Cleaning ARB files...');
final arbFiles = await l10nDir
.list()
.where((file) => file.path.endsWith('.arb'))
.cast<File>()
.toList();
if (arbFiles.isEmpty) {
print('❌ No ARB files found');
exit(1);
}
for (final file in arbFiles) {
await cleanArbFile(file);
}
print('βœ… Cleaned ${arbFiles.length} ARB files successfully!');
}
Future<void> cleanArbFile(File file) async {
final filename = file.path.split(Platform.pathSeparator).last;
print('πŸ“„ Processing $filename...');
try {
// Read the file content
final content = await file.readAsString();
// Parse JSON
final Map<String, dynamic> data = jsonDecode(content);
// Clean the data
final cleanedData = <String, dynamic>{};
// First pass: collect all translation keys (non-metadata)
final translationKeys = <String>[];
for (final key in data.keys) {
if (!key.startsWith('@') && key != '@@locale') {
translationKeys.add(key);
}
}
// Add locale if present
if (data.containsKey('@@locale')) {
cleanedData['@@locale'] = data['@@locale'];
}
// Add all translation keys with their values
for (final key in translationKeys) {
if (data[key] is String) {
cleanedData[key] = data[key];
}
}
// Convert back to JSON with proper formatting
const encoder = JsonEncoder.withIndent(' ');
final cleanedJson = encoder.convert(cleanedData);
// Write back to file
await file.writeAsString(cleanedJson + '\n');
print('βœ… Cleaned $filename (${translationKeys.length} translations)');
} catch (e) {
print('❌ Error cleaning $filename: $e');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment