-
Notifications
You must be signed in to change notification settings - Fork 5
/
strings_to_arb.dart
226 lines (184 loc) · 7.05 KB
/
strings_to_arb.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
import 'dart:async';
import 'dart:convert';
import 'dart:io';
import 'package:args/args.dart';
import 'package:path/path.dart' as path;
const String sourceKey = 'source';
const String sourceDefault = './lib/';
const String outputKey = 'output';
const String outputDefault = './lib/l10n/';
const String createPathsKey = 'create-paths';
const bool createPathsKeyDefault = true;
Future<void> main(List<String> arguments) async {
final ArgParser parser = ArgParser()
..addOption(
sourceKey,
abbr: 's',
help: 'Specify where to search for the arb files.',
valueHelp: sourceDefault,
defaultsTo: sourceDefault,
)
..addOption(
outputKey,
abbr: 'o',
help: 'Specify where to save the generated dart files.',
valueHelp: outputDefault,
defaultsTo: outputDefault,
)
..addFlag(
'create-paths',
abbr: 'c',
help: 'This will create the folders structure recursevly.',
defaultsTo: createPathsKeyDefault,
);
if (arguments.isNotEmpty && arguments[0] == 'help') {
stdout.writeln(parser.usage);
return;
}
final ArgResults result = parser.parse(arguments);
final String source = path.canonicalize(path.absolute(result[sourceKey]));
final String output = path.canonicalize(path.absolute(result[outputKey]));
final bool createPaths = result[createPathsKey];
final Directory sourceDir = Directory(source);
final Directory outputDir = Directory(output);
print('sada');
print(sourceDir);
print(outputDir);
if (createPaths) {
if (!sourceDir.existsSync()) {
sourceDir.createSync(recursive: true);
}
if (!outputDir.existsSync()) {
outputDir.createSync(recursive: true);
}
}
var write = false;
Map<String, String> allMessages = {};
var skipFiles = ["strings_to_arb.dart", "models", "provider", "generated"];
recursiveFolderCopySync(source, skipFiles, allMessages, write);
var locale = "en";
if (locale != null) {
allMessages["@@locale"] = locale;
}
allMessages["@@last_modified"] = new DateTime.now().toIso8601String();
String outputFilename = "intl_en.arb";
var outputFile = new File(path.join(outputDir.path, outputFilename));
var encoder = new JsonEncoder.withIndent(" ");
// List<String> sortedKeys = allMessages.keys.toList(growable: false)..sort((k1, k2) => k1.compareTo(k2));
// allMessages = Map.fromIterable(sortedKeys);
print(allMessages);
if(write) {
outputFile.writeAsStringSync(encoder.convert(allMessages));
}
}
void recursiveFolderCopySync(String source, List<String> skips, Map<dynamic, dynamic> allMessages, bool write) {
Directory directory = new Directory(source);
var skipWords = ["dart:ui", "dart:io"];
var finder = DartHardCodedStringFinder(skipWords);
directory.listSync().forEach((element) {
print(element);
var shouldSkip = skips.contains(path.basename(element.path));
if (element is File) {
if (path.extension(element.path) == ".dart" && !shouldSkip) {
var content = element.readAsStringSync();
var stringsFounded = finder.findHardCodedStrings(content);
if (stringsFounded.isNotEmpty) {
print(stringsFounded);
if (content.contains("context")) {
//only change strings if there is a context for replacing
content = "import 'package:islamic_quiz_flutter/generated/l10n.dart';\n" + content;
stringsFounded.forEach((element) {
// if (skipWords.any((word) => element.contains(word))) {
allMessages[finder.getCamelCase(element)] = element;
content = content.replaceAll("\"$element\"", "S.of(context).${finder.getCamelCase(element)}");
content = content.replaceAll("\'$element\'", "S.of(context).${finder.getCamelCase(element)}");
// }
});
// element.writeAsString(content);
}
}
}
} else if (element is Directory && !shouldSkip) {
recursiveFolderCopySync(element.path, skips, allMessages, write);
}
});
}
class DartHardCodedStringFinder {
List<String> skipWords;
DartHardCodedStringFinder(this.skipWords);
bool shouldInclude(String it) {
return it.isNotEmpty &&
!it.contains("assets") &&
!it.contains(".png") &&
!it.contains(".jpeg") &&
!it.contains(".mp3") &&
!it.contains(".mkv") &&
!it.contains("_") && //most probably api stuff
!it.contains("/") && //path
!it.contains("#") && // color stuff
it.trim().length > 1 && //most probably not needed
!it.contains(".svg");
}
var regex = RegExp("\".*?\"");
var regexDart = RegExp("\'.*?\'");
String extractHardCodedString(String it, String input) {
return it.replaceAll("\"", "").replaceAll("\'", "").trim();
}
/// Returns a string in the form "UpperCamelCase" or "lowerCamelCase".
///
/// Example:
/// print(camelize("dart_vm"));
/// => DartVm
String getCamelCase(String _words, {String separator: ''}) {
List<String> words = _words
.replaceAll("\"", "")
.replaceAll(".", "")
.replaceAll(",", "")
.replaceAll("?", "")
.replaceAll("&", "")
.replaceAll("%", "")
.replaceAll("*", "")
.replaceAll("(", "")
.replaceAll(")", "")
.replaceAll("!", "")
.replaceAll("-", "")
.replaceAll("/", "")
.replaceAll("|", "")
.split(" ")
.map((e) => _upperCaseFirstLetter(e))
.toList();
if (_words.isNotEmpty) {
words[0] = words[0].toLowerCase();
}
return words.join(separator);
}
String _upperCaseFirstLetter(String word) {
return word.length > 1 ? '${word.substring(0, 1).toUpperCase()}${word.substring(1).toLowerCase()}' : word;
}
List<String> findHardCodedStrings(String content) {
Iterable<RegExpMatch> result = regex.allMatches(content);
var strings = <String>[];
result.forEach((e) {
var string = extractHardCodedString(e.group(0), e.input);
var jsonParamAccessString = e.input.codeUnitAt(e.start-1) =='['.codeUnits.first && e.input.codeUnitAt(e.end) ==']'.codeUnits.first;
var jsonParamSetString = e.input.codeUnitAt(e.end) ==':'.codeUnits.first;
var uselessArguements = string.startsWith("\$\{") && string.endsWith("}") ;
var include = shouldInclude(string) && !jsonParamSetString && !jsonParamAccessString && !uselessArguements;
if (include) {
strings.add(string);
}
});
Iterable<RegExpMatch> result1 = regexDart.allMatches(content);
result1.forEach((e) {
var string = extractHardCodedString(e.group(0), e.input);
var jsonParamAccessString = e.input.codeUnitAt(e.start-1) =='['.codeUnits.first && e.input.codeUnitAt(e.end) ==']'.codeUnits.first;
var jsonParamSetString = e.input.codeUnitAt(e.end) ==':'.codeUnits.first;
var uselessArguements = string.startsWith("\$\{") && string.endsWith("}") ;
var include = shouldInclude(string) && !jsonParamSetString && !jsonParamAccessString && !uselessArguements;
if (include) {
strings.add(string);
}
});
return strings;
}
}