diff --git a/.github/workflows/analyze.yml b/.github/workflows/analyze.yml index e66916e9d..6463dfd8e 100644 --- a/.github/workflows/analyze.yml +++ b/.github/workflows/analyze.yml @@ -5,12 +5,18 @@ on: - '**.dart' - 'pubspec.yaml' - 'analysis_options.yaml' + - 'lib/l10n/**' + - 'ios/Runner/Info.plist' + - 'android/app/src/main/res/xml/locales_config.xml' pull_request: branches: [ master, ] paths: - '**/*.dart' - 'pubspec.yaml' - 'analysis_options.yaml' + - 'lib/l10n/**' + - 'ios/Runner/Info.plist' + - 'android/app/src/main/res/xml/locales_config.xml' - '.github/actions/flutter-common/action.yml' - '.github/workflows/analyze.yml' workflow_call: @@ -34,3 +40,6 @@ jobs: - name: Analyze run: flutter analyze --no-fatal-infos --no-fatal-warnings + + - name: Check locale lists are in sync + run: dart run tool/check_locales.dart diff --git a/lib/l10n/language_native_names.dart b/lib/l10n/language_native_names.dart new file mode 100644 index 000000000..63d01f0b5 --- /dev/null +++ b/lib/l10n/language_native_names.dart @@ -0,0 +1,57 @@ +/* + * This file is part of wger Workout Manager . + * Copyright (c) 2026 - 2026 wger Team + * + * wger Workout Manager is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +/// Native display names for each supported locale, keyed by the locale name +const Map languageNativeNames = { + 'am': 'አማርኛ', + 'ar': 'العربية', + 'ca': 'Català', + 'cs': 'Čeština', + 'de': 'Deutsch', + 'el': 'Ελληνικά', + 'en': 'English', + 'es': 'Español', + 'fa': 'فارسی', + 'fil': 'Filipino', + 'fr': 'Français', + 'he': 'עברית', + 'hi': 'हिन्दी', + 'hr': 'Hrvatski', + 'hu': 'Magyar', + 'id': 'Bahasa Indonesia', + 'it': 'Italiano', + 'ja': '日本語', + 'ko': '한국어', + 'mk': 'Македонски', + 'nb': 'Norsk bokmål', + 'nl': 'Nederlands', + 'pl': 'Polski', + 'pt': 'Português', + 'pt_BR': 'Português (Brasil)', + 'pt_PT': 'Português (Portugal)', + 'ro': 'Română', + 'ru': 'Русский', + 'sk': 'Slovenčina', + 'ta': 'தமிழ்', + 'th': 'ไทย', + 'tr': 'Türkçe', + 'uk': 'Українська', + 'ur': 'اردو', + 'zh': '中文', + 'zh_Hant': '繁體中文', +}; diff --git a/lib/widgets/core/settings/language.dart b/lib/widgets/core/settings/language.dart index d77ec7eb2..0f62d0e84 100644 --- a/lib/widgets/core/settings/language.dart +++ b/lib/widgets/core/settings/language.dart @@ -1,6 +1,6 @@ /* * This file is part of wger Workout Manager . - * Copyright (c) 2026 wger Team + * Copyright (c) 2026 - 2026 wger Team * * wger Workout Manager is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as published by @@ -20,52 +20,12 @@ import 'package:flutter/material.dart'; import 'package:provider/provider.dart'; import 'package:wger/helpers/locale.dart'; import 'package:wger/l10n/generated/app_localizations.dart'; +import 'package:wger/l10n/language_native_names.dart'; import 'package:wger/providers/user.dart'; -/// Native language names for each supported locale. Keys are produced by -/// [encodeLocale] (e.g. `pl`, `pt_BR`, `zh_Hant`). -const Map _languageNativeNames = { - 'am': 'አማርኛ', - 'ar': 'العربية', - 'ca': 'Català', - 'cs': 'Čeština', - 'de': 'Deutsch', - 'el': 'Ελληνικά', - 'en': 'English', - 'es': 'Español', - 'fa': 'فارسی', - 'fil': 'Filipino', - 'fr': 'Français', - 'he': 'עברית', - 'hi': 'हिन्दी', - 'hr': 'Hrvatski', - 'hu': 'Magyar', - 'id': 'Bahasa Indonesia', - 'it': 'Italiano', - 'ja': '日本語', - 'ko': '한국어', - 'mk': 'Македонски', - 'nb': 'Norsk bokmål', - 'nl': 'Nederlands', - 'pl': 'Polski', - 'pt': 'Português', - 'pt_BR': 'Português (Brasil)', - 'pt_PT': 'Português (Portugal)', - 'ro': 'Română', - 'ru': 'Русский', - 'sk': 'Slovenčina', - 'ta': 'தமிழ்', - 'th': 'ไทย', - 'tr': 'Türkçe', - 'uk': 'Українська', - 'ur': 'اردو', - 'zh': '中文', - 'zh_Hant': '繁體中文', -}; - String _displayNameFor(Locale locale) { - return _languageNativeNames[encodeLocale(locale)] ?? - _languageNativeNames[locale.languageCode] ?? + return languageNativeNames[encodeLocale(locale)] ?? + languageNativeNames[locale.languageCode] ?? encodeLocale(locale); } diff --git a/pubspec.lock b/pubspec.lock index a608a9315..ea1998954 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -1642,7 +1642,7 @@ packages: source: hosted version: "1.1.0" xml: - dependency: transitive + dependency: "direct dev" description: name: xml sha256: "971043b3a0d3da28727e40ed3e0b5d18b742fa5a68665cca88e74b7876d5e025" diff --git a/pubspec.yaml b/pubspec.yaml index 288b261de..1b103c777 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -77,6 +77,7 @@ dev_dependencies: sdk: flutter build_runner: ^2.15.0 cider: ^0.2.9 + xml: ^6.6.1 drift_dev: ^2.31.0 flutter_lints: ^6.0.0 flutter_native_splash: ^2.4.7 diff --git a/tool/check_locales.dart b/tool/check_locales.dart new file mode 100644 index 000000000..680ed713b --- /dev/null +++ b/tool/check_locales.dart @@ -0,0 +1,231 @@ +/* + * This file is part of wger Workout Manager . + * Copyright (c) 2026 wger Team + * + * wger Workout Manager is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +library; + +import 'dart:io'; + +import 'package:collection/collection.dart'; +import 'package:wger/l10n/language_native_names.dart'; +import 'package:xml/xml.dart'; + +const _listEq = ListEquality(); + +const _arbDir = 'lib/l10n'; +const _iosPlist = 'ios/Runner/Info.plist'; +const _androidXml = 'android/app/src/main/res/xml/locales_config.xml'; + +/// Verifies that every place which enumerates the app's supported locales +/// agrees with the canonical list derived from `lib/l10n/app_*.arb` filenames. +/// +/// Checks: +/// - `ios/Runner/Info.plist` +/// - `android/app/src/main/res/xml/locales_config.xml` +/// - `languageNativeNames` in `lib/l10n/language_native_names.dart` +/// +/// Run `dart run tool/check_locales.dart` to check, +/// or `dart run tool/check_locales.dart --fix` to rewrite the iOS/Android lists +/// in place. The native-name map stays manual. +void main(List args) { + final fix = args.contains('--fix'); + + final canonical = _readArbLocales(); + if (canonical.isEmpty) { + stderr.writeln('No app_*.arb files found in $_arbDir'); + exit(2); + } + + // Dash form for iOS/Android, underscore form for Dart. + final dashForm = canonical.map((c) => c.replaceAll('_', '-')).toList()..sort(); + final underscoreForm = [...canonical]..sort(); + + var drift = false; + + drift |= _checkPlist(dashForm, fix: fix); + drift |= _checkAndroidXml(dashForm, fix: fix); + drift |= _checkLanguageDartCoverage(underscoreForm); + + if (drift) { + if (fix) { + stdout.writeln('\nFixed drifted files. Re-run without --fix to verify.'); + } else { + stderr.writeln('\nLocale lists are out of sync. Re-run with --fix to update.'); + exit(1); + } + } else { + stdout.writeln('\nAll locale lists match (${canonical.length} locales).'); + } +} + +/// Canonical locale list from `app_.arb` filenames. Returns tags in +/// underscore form (e.g. `pt_BR`, `zh_Hant`). +List _readArbLocales() { + final dir = Directory(_arbDir); + if (!dir.existsSync()) { + return const []; + } + final tags = []; + for (final entity in dir.listSync()) { + if (entity is! File) { + continue; + } + final name = entity.uri.pathSegments.last; + final match = RegExp(r'^app_(.+)\.arb$').firstMatch(name); + if (match != null) { + tags.add(match.group(1)!); + } + } + tags.sort(); + return tags; +} + +bool _checkPlist(List expectedDash, {required bool fix}) { + final file = File(_iosPlist); + if (!file.existsSync()) { + stderr.writeln('[ios] $_iosPlist not found'); + return false; + } + final content = file.readAsStringSync(); + final doc = XmlDocument.parse(content); + + // Plist structure: foo.... + // Find the CFBundleLocalizations and take its following sibling. + final dict = doc.rootElement.findElements('dict').firstOrNull; + final keyNode = dict + ?.findElements('key') + .firstWhereOrNull( + (k) => k.innerText == 'CFBundleLocalizations', + ); + final array = keyNode?.nextElementSibling; + if (array == null || array.name.local != 'array') { + stderr.writeln('[ios] CFBundleLocalizations not found in $_iosPlist'); + return true; + } + + final actual = array.findElements('string').map((e) => e.innerText).toList()..sort(); + + if (_listEq.equals(actual, expectedDash)) { + stdout.writeln('[ios] OK (${actual.length} locales)'); + return false; + } + + _reportDiff('ios', actual, expectedDash); + + if (fix) { + // Mutate only the 's children + array.children.clear(); + array.children.add(XmlText('\n\t\t\t')); + for (final tag in expectedDash) { + array.children.add(XmlElement(XmlName('string'), [], [XmlText(tag)])); + array.children.add(XmlText('\n\t\t\t')); + } + // Trim the trailing inter-child indent and replace with the closing one. + array.children.removeLast(); + array.children.add(XmlText('\n\t\t')); + file.writeAsStringSync(doc.toXmlString()); + stdout.writeln('[ios] rewrote $_iosPlist'); + } + return true; +} + +bool _checkAndroidXml(List expectedDash, {required bool fix}) { + final file = File(_androidXml); + if (!file.existsSync()) { + stderr.writeln('[android] $_androidXml not found'); + return false; + } + final doc = XmlDocument.parse(file.readAsStringSync()); + + final actual = doc.rootElement + .findElements('locale') + .map((e) => e.getAttribute('android:name')!) + .toList(); + + // Convention: `en` first (matches `preferred-supported-locales: [en]` in + // l10n.yaml), then alphabetical. + final expectedOrdered = _enFirst(expectedDash); + + if (_listEq.equals(actual, expectedOrdered)) { + stdout.writeln('[android] OK (${actual.length} locales)'); + return false; + } + + _reportDiff('android', actual..sort(), expectedOrdered.toList()..sort()); + + if (fix) { + final builder = XmlBuilder(); + builder.processing('xml', 'version="1.0" encoding="utf-8"'); + builder.element( + 'locale-config', + namespaces: {'http://schemas.android.com/apk/res/android': 'android'}, + nest: () { + for (final tag in expectedOrdered) { + builder.element('locale', attributes: {'android:name': tag}); + } + }, + ); + file.writeAsStringSync( + builder.buildDocument().toXmlString(pretty: true, indent: ' '), + ); + stdout.writeln('[android] rewrote $_androidXml'); + } + return true; +} + +/// Returns the list with `en` first (if present), the rest alphabetically. +List _enFirst(List tags) { + final rest = [...tags.where((t) => t != 'en')]..sort(); + return tags.contains('en') ? ['en', ...rest] : rest; +} + +/// The native-name map is hand-curated, since this script doesn't know how +/// to spell `Українська`, we can only check coverage, not auto-fix it. +bool _checkLanguageDartCoverage(List expectedUnderscore) { + final actual = languageNativeNames.keys.toSet(); + final expected = expectedUnderscore.toSet(); + + final missing = expected.difference(actual).toList()..sort(); + final extra = actual.difference(expected).toList()..sort(); + + if (missing.isEmpty && extra.isEmpty) { + stdout.writeln('[dart] OK (${actual.length} entries)'); + return false; + } + + if (missing.isNotEmpty) { + stderr.writeln('[dart] missing native-name entries: ${missing.join(', ')}'); + stderr.writeln(' add them by hand to lib/l10n/language_native_names.dart'); + } + if (extra.isNotEmpty) { + stderr.writeln('[dart] stale native-name entries (no matching .arb): ${extra.join(', ')}'); + } + return true; +} + +void _reportDiff(String label, List actual, List expected) { + final actualSet = actual.toSet(); + final expectedSet = expected.toSet(); + final missing = expectedSet.difference(actualSet).toList()..sort(); + final extra = actualSet.difference(expectedSet).toList()..sort(); + if (missing.isNotEmpty) { + stderr.writeln('[$label] missing: ${missing.join(', ')}'); + } + if (extra.isNotEmpty) { + stderr.writeln('[$label] extra: ${extra.join(', ')}'); + } +}