createReport method

void createReport(
  1. String? outputDir,
  2. Set<String> locales,
  3. Map<String, String>? commonKeys,
  4. Map<String, String>? langKeys,
)

Implementation

void createReport(String? outputDir, Set<String> locales,
    Map<String, String>? commonKeys, Map<String, String>? langKeys) {
  outputDir = outputDir ?? 'assets/l10n/';
  if (!outputDir.endsWith('/')) {
    outputDir += '/';
  }
  if (!File(outputDir).existsSync()) {
    Directory(outputDir).createSync(recursive: true);
  }
  Map<String, dynamic> report = {'new_keys_file': [], 'del_keys_file': []};
  final common = {};
  locales.toList()
    ..sort()
    ..forEach((code) {
      final pair = code.split('_');
      File file;
      // common keys
      if (!common.containsKey(pair[0])) {
        common[pair[0]] = true;
        file = File(outputDir! + pair[0] + '.json');
        createJson(file, commonKeys, report);
      }
      if (pair.length == 2) {
        file =
            File(outputDir! + (pair.length == 1 ? pair[0] : code) + '.json');
        createJson(file, langKeys, report);
      }
    });
  StringBuffer sb = StringBuffer();
  sb.write('''
<!doctype html>
<html lang='en'>
<head>
<meta charset='utf-8'>
<meta http-equiv='Cache-control' content='no-cache'>
<title>L10n Report</title>
<style>
html {background: lightblue}
li {padding: 2px 5px; margin: 2px 0}
h3 {padding: 3px; margin: 0; color: darkblue}
pre { white-space: pre-wrap;}
.accordion input {display: none;}
.accordion label {background: #eee; cursor: pointer; display: block; margin-bottom: .125em; padding: .25em 1em;}
.accordion label:hover {background: #ccc;}
.accordion input:checked + label {background: #ccc; color: white;}
.accordion article {background: #f7f7f7; height:0px;overflow:hidden;}
.accordion input:checked ~ article {height: auto;}
</style>
</head>
<body>
<h1>L10n Report</h1>
<h4>Release Date: ${DateTime.now().toIso8601String().replaceAll('T', '   ').split('.').first}</h4>
<h2 style="color: darkgreen; margin-top: 1em">New Keys:</h2>
''');

  int index = 0;
  report['new_keys_file'].forEach((file) => {
        sb.write('''
        <h3>${file[0]}</h3>
<section class='accordion'>
<input type='checkbox' id='check-$index'/>
<label for='check-${index++}'><b>Total:</b> ${file[2]}</label>
<article>
<pre>
${file[1].join('\n')}
</pre>
</article>
</section>
    ''')
      });
  sb.write('<h2 style="color: darkred; margin-top: 3em;">Deleted Keys:</h2>');
  report['del_keys_file'].forEach((file) => {
        sb.write('''
        <h3>${file[0]}</h3>
<section class='accordion'>
<input type='checkbox' id='check-$index'/>
<label for='check-${index++}'><b>Total:</b> ${file[2]}</label>
<article>
<pre>
${file[1].join('\n')}
</pre>
</article>
</section>
    ''')
      });
  sb.write('</body>\n</html>');
  File(outputDir + 'report.html').writeAsString(sb.toString());
  return;
}