forked from namidaco/namida
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tr_refine.dart
53 lines (47 loc) · 1.74 KB
/
tr_refine.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
// ignore_for_file: avoid_print
import 'dart:convert';
import 'dart:io';
import 'package:path/path.dart' as p;
void main(List<String> args) async {
Future<Map<String, dynamic>?> parseJSONFile(File file) async {
try {
final str = await file.readAsString();
final langMap = jsonDecode(str) as Map<String, dynamic>;
return langMap;
} catch (e) {
print('Error parsing the map $e');
}
return null;
}
const engJsonFileName = 'en_US.json';
final mainMap = <String, dynamic>{};
final mainFile = File("$_languagesDirectoryPath\\$engJsonFileName");
final mainParsed = await parseJSONFile(mainFile);
if (mainParsed == null || mainParsed.isEmpty) {
print('Error parsing the main map, aborting...');
return;
}
mainMap.addAll(mainParsed);
const encoder = JsonEncoder.withIndent(' ');
await for (final fileSystem in Directory(_languagesDirectoryPath).list()) {
if (fileSystem is File) {
if (p.basename(fileSystem.path) != engJsonFileName && fileSystem.path.endsWith('.json')) {
final copyMap = Map<String, String>.from(mainMap);
final langMap = await parseJSONFile(fileSystem);
if (langMap == null || langMap.isEmpty) {
print('Error parsing the json of $fileSystem, skipping...');
continue;
}
for (final e in langMap.keys) {
if (copyMap[e] != null) {
// -- only assign if the key exists in the main map
// -- remaining keys (ones exists in main but not in lang) will have the same value of default map.
copyMap[e] = langMap[e];
}
}
await fileSystem.writeAsString(encoder.convert(copyMap));
}
}
}
}
const _languagesDirectoryPath = 'assets\\language\\translations';