forked from KomodoPlatform/komodo-wallet-mobile
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sync_arb_files.dart
132 lines (97 loc) · 3.57 KB
/
sync_arb_files.dart
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import 'dart:convert';
import 'dart:io';
void main() {
const String messagesPath = 'lib/l10n/intl_messages.arb';
print('\nLoading $messagesPath...');
final Map<String, dynamic> messagesJson = getJsonFromFile(messagesPath);
print('Done. ${messagesJson.length} keys found.');
final Map<String, String> localePaths =
getLocalePaths(Directory('lib/l10n/'));
localePaths.forEach((locale, path) {
print('\nProcessing locale: \'$locale\'...');
final Map<String, dynamic> currentLocaleJson = getJsonFromFile(path);
final Map<String, dynamic> newLocaleJson = {};
int deletionsCounter = 0;
messagesJson.forEach((key, message) {
if (key.startsWith('@')) return;
final String currentLocaleMessage = currentLocaleJson[key];
final isCurrentLocaleMessageValid =
isLocaleMessageValid(message, currentLocaleMessage);
if (!isCurrentLocaleMessageValid) {
deletionsCounter++;
return;
}
newLocaleJson[key] = currentLocaleMessage;
newLocaleJson['@$key'] = messagesJson['@$key'];
});
writeJsonToFile(newLocaleJson, path);
print('Done. $deletionsCounter keys need to be translated.');
});
}
bool isLocaleMessageValid(String message, String localeMessage) {
if (localeMessage == null) return false;
if (localeMessage.isEmpty) return false;
if (!arePlaceholdersValid(message, localeMessage)) return false;
return true;
}
bool arePlaceholdersValid(String message, String localeMessage) {
bool areValid = true;
final RegExp regExp = RegExp(r'{(.*?)}');
final List<Match> messageMatches = regExp.allMatches(message).toList();
final List<Match> localeMessageMatches =
regExp.allMatches(localeMessage).toList();
final List<String> messagePlaceholders = extractPlaceholders(messageMatches);
final List<String> localeMessagePlaceholders =
extractPlaceholders(localeMessageMatches);
for (String localePlaceholder in localeMessagePlaceholders) {
if (!messagePlaceholders.contains(localePlaceholder)) {
areValid = false;
break;
}
}
return areValid;
}
List<String> extractPlaceholders(List<Match> matches) {
List<String> placeholders = [];
for (Match match in matches) {
final String placeholder = match.group(0);
if (!placeholders.contains(placeholder)) placeholders.add(placeholder);
}
placeholders.sort((a, b) => a.compareTo(b));
return placeholders;
}
void writeJsonToFile(Map<String, dynamic> json, String filePath) {
final String spaces = ' ' * 4;
final JsonEncoder encoder = JsonEncoder.withIndent(spaces);
File(filePath).writeAsStringSync(encoder.convert(json));
}
Map<String, dynamic> getJsonFromFile(String filePath) {
Map<String, dynamic> json;
try {
final String messagesString = File(filePath).readAsStringSync();
json = jsonDecode(messagesString);
} catch (e) {
print('Unable to load json from $filePath:\n$e');
rethrow;
}
return json;
}
Map<String, String> getLocalePaths(Directory dir) {
print('\nGetting locale files list...');
final Map<String, String> paths = {};
final List<FileSystemEntity> entities = dir.listSync().toList();
for (FileSystemEntity entity in entities) {
if (entity is! File) continue;
final Match match = RegExp('.*\/intl_(.*)\.arb').firstMatch(entity.path);
if (match == null) continue;
final String locale = match.group(1);
if (locale == 'messages') continue;
paths[locale] = entity.path;
}
if (paths.isEmpty) {
print('Nothing found.');
} else {
print('Done. ${paths.length} locales found: ${paths.keys.join(', ')}.');
}
return paths;
}