Created
August 9, 2025 10:54
-
-
Save JEuler/4441010357adee8ffd5cf4de2fcdd516 to your computer and use it in GitHub Desktop.
Clearn ARB files from Metadata
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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