|
| 1 | +import 'dart:convert'; |
| 2 | +import 'dart:io'; |
| 3 | +import 'package:path/path.dart' as path; |
| 4 | + |
| 5 | +late String cwd; |
| 6 | +late Directory outDir; |
| 7 | + |
| 8 | +void main() async { |
| 9 | + cwd = Directory.current.path; |
| 10 | + final l10nSrc = Directory(path.join(cwd, 'lib', 'l10n')); |
| 11 | + outDir = Directory(path.join(cwd, 'lib', 'src')); |
| 12 | + |
| 13 | + if (!outDir.existsSync()) { |
| 14 | + outDir.createSync(recursive: true); |
| 15 | + } |
| 16 | + |
| 17 | + final readFutures = await l10nSrc.list().map((event) { |
| 18 | + final file = File(event.path); |
| 19 | + return file.openRead().transform(const Utf8Decoder()).toList(); |
| 20 | + }).toList(); |
| 21 | + |
| 22 | + final sources = await Future.wait(readFutures); |
| 23 | + |
| 24 | + final labelsByLocale = sources.fold<Map<String, dynamic>>({}, (acc, lines) { |
| 25 | + final fullSrc = lines.join(); |
| 26 | + final arbJson = jsonDecode(fullSrc); |
| 27 | + final localeString = arbJson['@@locale']; |
| 28 | + |
| 29 | + final parsed = localeString.split('_'); |
| 30 | + |
| 31 | + return { |
| 32 | + ...acc, |
| 33 | + parsed[0]: { |
| 34 | + ...(acc[parsed[0]] ?? {}), |
| 35 | + if (parsed.length > 1) parsed[1]: arbJson else 'default': arbJson, |
| 36 | + } |
| 37 | + }; |
| 38 | + }); |
| 39 | + |
| 40 | + final genOps = labelsByLocale.entries.map((entry) { |
| 41 | + if (entry.value.length == 1) { |
| 42 | + return [ |
| 43 | + generateLocalizationsClass( |
| 44 | + locale: entry.key, |
| 45 | + arb: entry.value['default'], |
| 46 | + ) |
| 47 | + ]; |
| 48 | + } |
| 49 | + |
| 50 | + return [ |
| 51 | + generateLocalizationsClass( |
| 52 | + locale: entry.key, |
| 53 | + arb: entry.value['default'], |
| 54 | + ), |
| 55 | + ...entry.value.entries |
| 56 | + .where((element) => element.key != 'default') |
| 57 | + .map((e) { |
| 58 | + return generateLocalizationsClass( |
| 59 | + locale: entry.key, |
| 60 | + countryCode: e.key, |
| 61 | + arb: e.value, |
| 62 | + ); |
| 63 | + }).toList(), |
| 64 | + ]; |
| 65 | + }).expand((element) => element); |
| 66 | + |
| 67 | + await Future.wait([...genOps.cast<Future>()]); |
| 68 | + await generateLanguagesList(labelsByLocale); |
| 69 | + Process.runSync('dart', ['format', outDir.path]); |
| 70 | +} |
| 71 | + |
| 72 | +bool isLabelEntry(MapEntry<String, dynamic> entry) { |
| 73 | + return !entry.key.startsWith('@'); |
| 74 | +} |
| 75 | + |
| 76 | +String dartFilename(String locale, [String? countryCode]) { |
| 77 | + return '$locale' |
| 78 | + '${countryCode != null ? '_${countryCode.toLowerCase()}' : ''}' |
| 79 | + '.dart'; |
| 80 | +} |
| 81 | + |
| 82 | +String dartClassName(String locale, [String? countryCode]) { |
| 83 | + return '${locale.capitalize()}' |
| 84 | + '${countryCode?.capitalize() ?? ''}Localizations'; |
| 85 | +} |
| 86 | + |
| 87 | +Future<void> generateLocalizationsClass({ |
| 88 | + required String locale, |
| 89 | + required Map<String, dynamic> arb, |
| 90 | + String? countryCode, |
| 91 | +}) async { |
| 92 | + final filename = dartFilename(locale, countryCode); |
| 93 | + final outFile = File(path.join(outDir.path, 'lang', filename)); |
| 94 | + |
| 95 | + if (!outFile.existsSync()) { |
| 96 | + outFile.createSync(recursive: true); |
| 97 | + } |
| 98 | + |
| 99 | + final out = outFile.openWrite(); |
| 100 | + |
| 101 | + out.writeln("import '../default_localizations.dart';"); |
| 102 | + |
| 103 | + final labels = arb.entries.where(isLabelEntry).map((e) { |
| 104 | + final meta = arb['@${e.key}'] ?? {}; |
| 105 | + |
| 106 | + return Label( |
| 107 | + key: e.key, |
| 108 | + translation: e.value, |
| 109 | + description: meta['description'], |
| 110 | + ); |
| 111 | + }).toList(); |
| 112 | + |
| 113 | + out.writeln(); |
| 114 | + |
| 115 | + final className = dartClassName(locale, countryCode); |
| 116 | + |
| 117 | + out.writeln('class $className extends FirebaseUILocalizationLabels {'); |
| 118 | + out.writeln(' const $className();'); |
| 119 | + |
| 120 | + for (var label in labels) { |
| 121 | + final escapedTranslation = jsonEncode(label.translation); |
| 122 | + |
| 123 | + out.writeln(); |
| 124 | + out.writeln(' @override'); |
| 125 | + out.writeln(' String get ${label.key} => $escapedTranslation;'); |
| 126 | + } |
| 127 | + |
| 128 | + out.writeln('}'); |
| 129 | + |
| 130 | + await out.flush(); |
| 131 | + await out.close(); |
| 132 | +} |
| 133 | + |
| 134 | +Future<void> generateLanguagesList(Map<String, dynamic> arb) async { |
| 135 | + final outFile = File(path.join(outDir.path, 'all_languages.dart')); |
| 136 | + |
| 137 | + if (!outFile.existsSync()) { |
| 138 | + outFile.createSync(recursive: true); |
| 139 | + } |
| 140 | + |
| 141 | + final out = outFile.openWrite(); |
| 142 | + out.writeln('import "./default_localizations.dart";'); |
| 143 | + out.writeln(); |
| 144 | + |
| 145 | + for (var entry in arb.entries) { |
| 146 | + final locale = entry.key; |
| 147 | + final countryCodes = entry.value.keys.where((e) => e != 'default').toList(); |
| 148 | + |
| 149 | + final filename = dartFilename(locale); |
| 150 | + out.writeln("import 'lang/$filename';"); |
| 151 | + |
| 152 | + for (var countryCode in countryCodes) { |
| 153 | + out.writeln("import 'lang/${dartFilename(locale, countryCode)}';"); |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + out.writeln(); |
| 158 | + out.writeln('final localizations = <String, FirebaseUILocalizationLabels>{'); |
| 159 | + |
| 160 | + for (var entry in arb.entries) { |
| 161 | + final locale = entry.key; |
| 162 | + final countryCodes = entry.value.keys.where((e) => e != 'default').toList(); |
| 163 | + |
| 164 | + out.writeln(" '$locale': const ${dartClassName(locale)}(),"); |
| 165 | + |
| 166 | + for (var countryCode in countryCodes) { |
| 167 | + final key = '${locale}_${countryCode.toLowerCase()}'; |
| 168 | + out.writeln(" '$key': const ${dartClassName(locale, countryCode)}(),"); |
| 169 | + } |
| 170 | + } |
| 171 | + |
| 172 | + out.writeln('};'); |
| 173 | + |
| 174 | + await out.flush(); |
| 175 | + await out.close(); |
| 176 | +} |
| 177 | + |
| 178 | +extension StringExtension on String { |
| 179 | + String capitalize() { |
| 180 | + return "${this[0].toUpperCase()}${substring(1)}"; |
| 181 | + } |
| 182 | +} |
| 183 | + |
| 184 | +class Label { |
| 185 | + final String key; |
| 186 | + final String translation; |
| 187 | + final String? description; |
| 188 | + |
| 189 | + Label({ |
| 190 | + required this.key, |
| 191 | + required this.translation, |
| 192 | + this.description, |
| 193 | + }); |
| 194 | +} |
0 commit comments