forked from Anxcye/anx-reader
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnormalize_arb_files.dart
More file actions
89 lines (71 loc) · 2.36 KB
/
normalize_arb_files.dart
File metadata and controls
89 lines (71 loc) · 2.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import 'dart:io';
import 'dart:convert';
void main() async {
final l10nDir = Directory('lib/l10n');
final enFile = File('lib/l10n/app_en.arb');
if (!await enFile.exists()) {
print('Error: English ARB file not found');
return;
}
final enContent = await enFile.readAsString();
final Map<String, dynamic> enMap = json.decode(enContent);
final orderedKeys = <String>[];
for (final key in enMap.keys) {
if (!key.startsWith('@@')) {
orderedKeys.add(key);
}
}
print('Found ${orderedKeys.length} translation keys in English file');
final files = await l10nDir
.list()
.where((entity) =>
entity is File &&
entity.path.endsWith('.arb') &&
!entity.path.endsWith('app_en.arb'))
.cast<File>()
.toList();
for (final file in files) {
await normalizeArbFile(file, orderedKeys, enMap);
}
print('\nNormalization completed for ${files.length} files');
}
Future<void> normalizeArbFile(
File file, List<String> orderedKeys, Map<String, dynamic> enMap) async {
final fileName = file.path.split('/').last;
print('Processing $fileName...');
try {
final content = await file.readAsString();
final Map<String, dynamic> currentMap = json.decode(content);
final Map<String, dynamic> normalizedMap = <String, dynamic>{};
for (final key in currentMap.keys) {
if (key.startsWith('@@')) {
normalizedMap[key] = currentMap[key];
}
}
int missingKeys = 0;
int foundKeys = 0;
for (final key in orderedKeys) {
if (currentMap.containsKey(key)) {
normalizedMap[key] = currentMap[key];
foundKeys++;
} else {
print(
' Warning: Missing key "$key", using English text as placeholder');
}
}
final extraKeys = <String>[];
for (final key in currentMap.keys) {
if (!key.startsWith('@@') && !orderedKeys.contains(key)) {
extraKeys.add(key);
print(' Warning: Extra key found "$key" (will be removed)');
}
}
const encoder = JsonEncoder.withIndent(' ');
final normalizedContent = encoder.convert(normalizedMap);
await file.writeAsString('$normalizedContent\n');
print(
' ✓ Normalized $fileName: $foundKeys keys found, $missingKeys missing, ${extraKeys.length} extra removed');
} catch (e) {
print(' ✗ Error processing $fileName: $e');
}
}