-
-
Notifications
You must be signed in to change notification settings - Fork 155
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
1. tr_unused: find unused keys 2. tr_refine: unifies all language files to follow en_US.json 3. tr (update): now accepts multiple keys to add/remove
- Loading branch information
Showing
3 changed files
with
126 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// ignore_for_file: avoid_print, depend_on_referenced_packages | ||
|
||
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 = await 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'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// ignore_for_file: avoid_print | ||
|
||
import 'dart:io'; | ||
|
||
void main(List<String> args) async { | ||
final filesPathsAndText = <String, String>{}; | ||
await for (final d in Directory('lib').list(recursive: true)) { | ||
if (d is File && d.path != _keysFilePath) { | ||
filesPathsAndText[d.path] = await File(d.path).readAsString(); | ||
} | ||
} | ||
|
||
bool findMatchInProject(String value) { | ||
for (final f in filesPathsAndText.keys) { | ||
final string = filesPathsAndText[f] ?? ''; | ||
if (string.contains(value)) return true; | ||
} | ||
return false; | ||
} | ||
|
||
final file = File(_keysFilePath); | ||
final lines = await file.readAsLines(); | ||
|
||
final notUsedKeys = <String>[]; | ||
for (final line in lines) { | ||
final regexRes = RegExp(r'(?<=String get )(.*)(?= =>)'); | ||
final match = regexRes.firstMatch(line)?[0]; | ||
if (match != null) { | ||
final hasMatch = findMatchInProject(match); | ||
if (!hasMatch) notUsedKeys.add(match); | ||
} | ||
} | ||
print("${notUsedKeys.length} not used keys were found"); | ||
print("--------------------------------"); | ||
print(notUsedKeys.toString()); | ||
} | ||
|
||
const _keysFilePath = 'lib\\core\\translations\\keys.dart'; |