|
| 1 | +/* |
| 2 | + * This file is part of wger Workout Manager <https://github.com/wger-project>. |
| 3 | + * Copyright (c) 2026 wger Team |
| 4 | + * |
| 5 | + * wger Workout Manager is free software: you can redistribute it and/or modify |
| 6 | + * it under the terms of the GNU Affero General Public License as published by |
| 7 | + * the Free Software Foundation, either version 3 of the License, or |
| 8 | + * (at your option) any later version. |
| 9 | + * |
| 10 | + * This program is distributed in the hope that it will be useful, |
| 11 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | + * GNU Affero General Public License for more details. |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU Affero General Public License |
| 16 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | + */ |
| 18 | + |
| 19 | +library; |
| 20 | + |
| 21 | +import 'dart:io'; |
| 22 | + |
| 23 | +import 'package:collection/collection.dart'; |
| 24 | +import 'package:wger/l10n/language_native_names.dart'; |
| 25 | +import 'package:xml/xml.dart'; |
| 26 | + |
| 27 | +const _listEq = ListEquality<String>(); |
| 28 | + |
| 29 | +const _arbDir = 'lib/l10n'; |
| 30 | +const _iosPlist = 'ios/Runner/Info.plist'; |
| 31 | +const _androidXml = 'android/app/src/main/res/xml/locales_config.xml'; |
| 32 | + |
| 33 | +/// Verifies that every place which enumerates the app's supported locales |
| 34 | +/// agrees with the canonical list derived from `lib/l10n/app_*.arb` filenames. |
| 35 | +/// |
| 36 | +/// Checks: |
| 37 | +/// - `ios/Runner/Info.plist` |
| 38 | +/// - `android/app/src/main/res/xml/locales_config.xml` |
| 39 | +/// - `languageNativeNames` in `lib/l10n/language_native_names.dart` |
| 40 | +/// |
| 41 | +/// Run `dart run tool/check_locales.dart` to check, |
| 42 | +/// or `dart run tool/check_locales.dart --fix` to rewrite the iOS/Android lists |
| 43 | +/// in place. The native-name map stays manual. |
| 44 | +void main(List<String> args) { |
| 45 | + final fix = args.contains('--fix'); |
| 46 | + |
| 47 | + final canonical = _readArbLocales(); |
| 48 | + if (canonical.isEmpty) { |
| 49 | + stderr.writeln('No app_*.arb files found in $_arbDir'); |
| 50 | + exit(2); |
| 51 | + } |
| 52 | + |
| 53 | + // Dash form for iOS/Android, underscore form for Dart. |
| 54 | + final dashForm = canonical.map((c) => c.replaceAll('_', '-')).toList()..sort(); |
| 55 | + final underscoreForm = [...canonical]..sort(); |
| 56 | + |
| 57 | + var drift = false; |
| 58 | + |
| 59 | + drift |= _checkPlist(dashForm, fix: fix); |
| 60 | + drift |= _checkAndroidXml(dashForm, fix: fix); |
| 61 | + drift |= _checkLanguageDartCoverage(underscoreForm); |
| 62 | + |
| 63 | + if (drift) { |
| 64 | + if (fix) { |
| 65 | + stdout.writeln('\nFixed drifted files. Re-run without --fix to verify.'); |
| 66 | + } else { |
| 67 | + stderr.writeln('\nLocale lists are out of sync. Re-run with --fix to update.'); |
| 68 | + exit(1); |
| 69 | + } |
| 70 | + } else { |
| 71 | + stdout.writeln('\nAll locale lists match (${canonical.length} locales).'); |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +/// Canonical locale list from `app_<tag>.arb` filenames. Returns tags in |
| 76 | +/// underscore form (e.g. `pt_BR`, `zh_Hant`). |
| 77 | +List<String> _readArbLocales() { |
| 78 | + final dir = Directory(_arbDir); |
| 79 | + if (!dir.existsSync()) { |
| 80 | + return const []; |
| 81 | + } |
| 82 | + final tags = <String>[]; |
| 83 | + for (final entity in dir.listSync()) { |
| 84 | + if (entity is! File) { |
| 85 | + continue; |
| 86 | + } |
| 87 | + final name = entity.uri.pathSegments.last; |
| 88 | + final match = RegExp(r'^app_(.+)\.arb$').firstMatch(name); |
| 89 | + if (match != null) { |
| 90 | + tags.add(match.group(1)!); |
| 91 | + } |
| 92 | + } |
| 93 | + tags.sort(); |
| 94 | + return tags; |
| 95 | +} |
| 96 | + |
| 97 | +bool _checkPlist(List<String> expectedDash, {required bool fix}) { |
| 98 | + final file = File(_iosPlist); |
| 99 | + if (!file.existsSync()) { |
| 100 | + stderr.writeln('[ios] $_iosPlist not found'); |
| 101 | + return false; |
| 102 | + } |
| 103 | + final content = file.readAsStringSync(); |
| 104 | + final doc = XmlDocument.parse(content); |
| 105 | + |
| 106 | + // Plist structure: <plist><dict><key>foo</key><value/>...</dict></plist>. |
| 107 | + // Find the <key>CFBundleLocalizations</key> and take its following sibling. |
| 108 | + final dict = doc.rootElement.findElements('dict').firstOrNull; |
| 109 | + final keyNode = dict |
| 110 | + ?.findElements('key') |
| 111 | + .firstWhereOrNull( |
| 112 | + (k) => k.innerText == 'CFBundleLocalizations', |
| 113 | + ); |
| 114 | + final array = keyNode?.nextElementSibling; |
| 115 | + if (array == null || array.name.local != 'array') { |
| 116 | + stderr.writeln('[ios] CFBundleLocalizations <array> not found in $_iosPlist'); |
| 117 | + return true; |
| 118 | + } |
| 119 | + |
| 120 | + final actual = array.findElements('string').map((e) => e.innerText).toList()..sort(); |
| 121 | + |
| 122 | + if (_listEq.equals(actual, expectedDash)) { |
| 123 | + stdout.writeln('[ios] OK (${actual.length} locales)'); |
| 124 | + return false; |
| 125 | + } |
| 126 | + |
| 127 | + _reportDiff('ios', actual, expectedDash); |
| 128 | + |
| 129 | + if (fix) { |
| 130 | + // Mutate only the <array>'s children |
| 131 | + array.children.clear(); |
| 132 | + array.children.add(XmlText('\n\t\t\t')); |
| 133 | + for (final tag in expectedDash) { |
| 134 | + array.children.add(XmlElement(XmlName('string'), [], [XmlText(tag)])); |
| 135 | + array.children.add(XmlText('\n\t\t\t')); |
| 136 | + } |
| 137 | + // Trim the trailing inter-child indent and replace with the closing one. |
| 138 | + array.children.removeLast(); |
| 139 | + array.children.add(XmlText('\n\t\t')); |
| 140 | + file.writeAsStringSync(doc.toXmlString()); |
| 141 | + stdout.writeln('[ios] rewrote $_iosPlist'); |
| 142 | + } |
| 143 | + return true; |
| 144 | +} |
| 145 | + |
| 146 | +bool _checkAndroidXml(List<String> expectedDash, {required bool fix}) { |
| 147 | + final file = File(_androidXml); |
| 148 | + if (!file.existsSync()) { |
| 149 | + stderr.writeln('[android] $_androidXml not found'); |
| 150 | + return false; |
| 151 | + } |
| 152 | + final doc = XmlDocument.parse(file.readAsStringSync()); |
| 153 | + |
| 154 | + final actual = doc.rootElement |
| 155 | + .findElements('locale') |
| 156 | + .map((e) => e.getAttribute('android:name')!) |
| 157 | + .toList(); |
| 158 | + |
| 159 | + // Convention: `en` first (matches `preferred-supported-locales: [en]` in |
| 160 | + // l10n.yaml), then alphabetical. |
| 161 | + final expectedOrdered = _enFirst(expectedDash); |
| 162 | + |
| 163 | + if (_listEq.equals(actual, expectedOrdered)) { |
| 164 | + stdout.writeln('[android] OK (${actual.length} locales)'); |
| 165 | + return false; |
| 166 | + } |
| 167 | + |
| 168 | + _reportDiff('android', actual..sort(), expectedOrdered.toList()..sort()); |
| 169 | + |
| 170 | + if (fix) { |
| 171 | + final builder = XmlBuilder(); |
| 172 | + builder.processing('xml', 'version="1.0" encoding="utf-8"'); |
| 173 | + builder.element( |
| 174 | + 'locale-config', |
| 175 | + namespaces: {'http://schemas.android.com/apk/res/android': 'android'}, |
| 176 | + nest: () { |
| 177 | + for (final tag in expectedOrdered) { |
| 178 | + builder.element('locale', attributes: {'android:name': tag}); |
| 179 | + } |
| 180 | + }, |
| 181 | + ); |
| 182 | + file.writeAsStringSync( |
| 183 | + builder.buildDocument().toXmlString(pretty: true, indent: ' '), |
| 184 | + ); |
| 185 | + stdout.writeln('[android] rewrote $_androidXml'); |
| 186 | + } |
| 187 | + return true; |
| 188 | +} |
| 189 | + |
| 190 | +/// Returns the list with `en` first (if present), the rest alphabetically. |
| 191 | +List<String> _enFirst(List<String> tags) { |
| 192 | + final rest = [...tags.where((t) => t != 'en')]..sort(); |
| 193 | + return tags.contains('en') ? ['en', ...rest] : rest; |
| 194 | +} |
| 195 | + |
| 196 | +/// The native-name map is hand-curated, since this script doesn't know how |
| 197 | +/// to spell `Українська`, we can only check coverage, not auto-fix it. |
| 198 | +bool _checkLanguageDartCoverage(List<String> expectedUnderscore) { |
| 199 | + final actual = languageNativeNames.keys.toSet(); |
| 200 | + final expected = expectedUnderscore.toSet(); |
| 201 | + |
| 202 | + final missing = expected.difference(actual).toList()..sort(); |
| 203 | + final extra = actual.difference(expected).toList()..sort(); |
| 204 | + |
| 205 | + if (missing.isEmpty && extra.isEmpty) { |
| 206 | + stdout.writeln('[dart] OK (${actual.length} entries)'); |
| 207 | + return false; |
| 208 | + } |
| 209 | + |
| 210 | + if (missing.isNotEmpty) { |
| 211 | + stderr.writeln('[dart] missing native-name entries: ${missing.join(', ')}'); |
| 212 | + stderr.writeln(' add them by hand to lib/l10n/language_native_names.dart'); |
| 213 | + } |
| 214 | + if (extra.isNotEmpty) { |
| 215 | + stderr.writeln('[dart] stale native-name entries (no matching .arb): ${extra.join(', ')}'); |
| 216 | + } |
| 217 | + return true; |
| 218 | +} |
| 219 | + |
| 220 | +void _reportDiff(String label, List<String> actual, List<String> expected) { |
| 221 | + final actualSet = actual.toSet(); |
| 222 | + final expectedSet = expected.toSet(); |
| 223 | + final missing = expectedSet.difference(actualSet).toList()..sort(); |
| 224 | + final extra = actualSet.difference(expectedSet).toList()..sort(); |
| 225 | + if (missing.isNotEmpty) { |
| 226 | + stderr.writeln('[$label] missing: ${missing.join(', ')}'); |
| 227 | + } |
| 228 | + if (extra.isNotEmpty) { |
| 229 | + stderr.writeln('[$label] extra: ${extra.join(', ')}'); |
| 230 | + } |
| 231 | +} |
0 commit comments