-
Notifications
You must be signed in to change notification settings - Fork 837
/
translations_check.dart
57 lines (50 loc) · 2.35 KB
/
translations_check.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
// This script has one task, which is to prevent the translations from
// being accidentally deleted, as we have more than 40 files for both the
// arb files (source) and the dart files (the generated)
// which make it harder to review the changes, some keys can be deleted
// without update the generated dart files which will cause a bug/build failure
// on the next time running the script after doing some changes to the translations
// which make it harder to revert the changes
// The script must run in the root project folder instead of the scripts folder
// ignore_for_file: depend_on_referenced_packages, avoid_print
import 'dart:convert';
import 'dart:io' show File, exit;
import 'package:path/path.dart' as path;
import 'package:yaml/yaml.dart';
// This must be updated once add or remove some translation keys
// if you update existing keys, no need to update it
const _expectedTranslationKeysLength = 101;
Future<void> main(List<String> args) async {
final l10nYamlText = await File('l10n.yaml').readAsString();
final l10nYaml = loadYaml(l10nYamlText);
final arbDir = l10nYaml['arb-dir'];
final templateArbFileName = l10nYaml['template-arb-file'];
final templateArbFile = File(path.join(arbDir, templateArbFileName));
print('The file path to template arb file: ${templateArbFile.path}');
final templateArb =
jsonDecode(await templateArbFile.readAsString()) as Map<String, Object?>;
print(
'The length of the current translation keys: ${templateArb.keys.length}');
final newTranslationKeysLength = templateArb.keys.length;
if (newTranslationKeysLength > _expectedTranslationKeysLength) {
print(
"You have add a new keys, please update the '_expectedTranslationKeysLength' value",
);
print('$newTranslationKeysLength > $_expectedTranslationKeysLength');
exit(1);
}
if (newTranslationKeysLength < _expectedTranslationKeysLength) {
print(
"You have removed some keys, please update the '_expectedTranslationKeysLength' value",
);
print('$newTranslationKeysLength < $_expectedTranslationKeysLength');
exit(1);
}
if (newTranslationKeysLength != _expectedTranslationKeysLength) {
print(
"It's looks like you have modified the keys length without updating the `_expectedTranslationKeysLength` value",
);
print('$newTranslationKeysLength != $_expectedTranslationKeysLength');
exit(1);
}
}