From 8a626ae2bfafb7d869456fbd2d9069acc82dbd55 Mon Sep 17 00:00:00 2001 From: Roland Geider Date: Wed, 3 Jun 2026 13:06:10 +0200 Subject: [PATCH] Handle (transient) case where an exercise has no translations --- lib/models/exercises/exercise.dart | 23 ++++++++++++++------ lib/providers/exercise_repository.dart | 9 +++++++- test/exercises/exercise_repository_test.dart | 18 +++++++++++---- test/exercises/model_exercise_test.dart | 14 ++++++++++++ 4 files changed, 52 insertions(+), 12 deletions(-) diff --git a/lib/models/exercises/exercise.dart b/lib/models/exercises/exercise.dart index 4aac12c0c..0beff2961 100644 --- a/lib/models/exercises/exercise.dart +++ b/lib/models/exercises/exercise.dart @@ -19,6 +19,7 @@ import 'package:collection/collection.dart'; import 'package:equatable/equatable.dart'; import 'package:logging/logging.dart'; import 'package:wger/helpers/consts.dart'; +import 'package:wger/models/core/language.dart'; import 'package:wger/models/exercises/category.dart'; import 'package:wger/models/exercises/equipment.dart'; import 'package:wger/models/exercises/image.dart'; @@ -102,18 +103,26 @@ class Exercise extends Equatable { bool get showPlateCalculator => equipment.map((e) => e.id).contains(ID_EQUIPMENT_BARBELL); - /// Returns translation for the given language + /// Returns the translation for the given language. /// - /// If no translation is found, English will be returned - /// - /// Note: we return the first translation as a fallback if we don't find a - /// translation in English. This is something that should never happen, - /// but we can't make sure that no local installation hasn't deleted - /// the entry in English. + /// Falls back to English, then to any available translation. If the exercise + /// has no translations at all, an empty placeholder in the requested language + /// is returned so callers never have to handle a missing translation. This + /// happens transiently while translations are still syncing, or for exercises + /// that genuinely ship without any translation. Translation getTranslation(String language) { // If the language is in the form en-US, take the language code only final languageCode = language.split('-')[0]; + if (translations.isEmpty) { + _logger.info('Exercise-ID $id has no translations, returning an empty placeholder.'); + return Translation( + name: '', + description: '', + language: Language(id: -1, shortName: languageCode, fullName: ''), + ); + } + return translations.firstWhere( (e) => e.language.shortName == languageCode, orElse: () => translations.firstWhere( diff --git a/lib/providers/exercise_repository.dart b/lib/providers/exercise_repository.dart index dba865e2e..519620ac8 100644 --- a/lib/providers/exercise_repository.dart +++ b/lib/providers/exercise_repository.dart @@ -266,6 +266,13 @@ class ExerciseRepository { if (category == null) { continue; } + // Skip exercises with no usable translation yet: an exercise without a + // name cannot be shown or searched, so keep it out of the catalog until + // a translation arrives on a later emission. + final translations = translationsByExercise[exerciseRow.id]; + if (translations == null || translations.isEmpty) { + continue; + } exercises.add( Exercise( id: exerciseRow.id, @@ -288,7 +295,7 @@ class ExerciseRepository { ], images: imagesByExercise[exerciseRow.id] ?? const [], videos: videosByExercise[exerciseRow.id] ?? const [], - translations: translationsByExercise[exerciseRow.id] ?? const [], + translations: translations, ), ); } diff --git a/test/exercises/exercise_repository_test.dart b/test/exercises/exercise_repository_test.dart index fbed449bf..779351187 100644 --- a/test/exercises/exercise_repository_test.dart +++ b/test/exercises/exercise_repository_test.dart @@ -252,10 +252,12 @@ void main() { }); test('separates primary and secondary muscles', () async { + await seedLanguage(testEnglish.id, 'en', 'English'); await seedCategory(testCategoryArms.id, 'Arms'); await seedMuscle(tMuscle1.id, 'Pec'); await seedMuscle(tMuscle2.id, 'Tri'); await seedExercise(1, categoryId: testCategoryArms.id); + await seedTranslation(1, exerciseId: 1, languageId: testEnglish.id, name: 'Bench Press'); await linkPrimaryMuscle(1, tMuscle1.id, linkId: 1); await linkSecondaryMuscle(1, tMuscle2.id, linkId: 1); @@ -267,10 +269,12 @@ void main() { }); test('keeps multiple equipment entries per exercise', () async { + await seedLanguage(testEnglish.id, 'en', 'English'); await seedCategory(testCategoryArms.id, 'Arms'); await seedEquipment(testEquipmentBench.id, 'Bench'); await seedEquipment(testEquipmentDumbbell.id, 'Dumbbell'); await seedExercise(1, categoryId: testCategoryArms.id); + await seedTranslation(1, exerciseId: 1, languageId: testEnglish.id, name: 'Bench Press'); await linkEquipment(1, testEquipmentBench.id, linkId: 1); await linkEquipment(1, testEquipmentDumbbell.id, linkId: 2); @@ -283,8 +287,10 @@ void main() { }); test('keeps multiple images per exercise', () async { + await seedLanguage(testEnglish.id, 'en', 'English'); await seedCategory(testCategoryArms.id, 'Arms'); await seedExercise(1, categoryId: testCategoryArms.id); + await seedTranslation(1, exerciseId: 1, languageId: testEnglish.id, name: 'Bench Press'); await seedImage(1, exerciseId: 1, path: 'one.jpg'); await seedImage(2, exerciseId: 1, path: 'two.jpg'); @@ -293,21 +299,25 @@ void main() { expect(state.exercises.single.images, hasLength(2)); }); - test('returns the exercise even if it has no translations or images', () async { + test('skips an exercise that has no translation yet', () async { + // During the initial sync the category can land before any translation. + // Such an exercise has no name to show, so it stays out of the catalog + // until a translation arrives on a later emission. await seedCategory(testCategoryArms.id, 'Arms'); await seedExercise(1, categoryId: testCategoryArms.id); final state = await repo.watchAllDrift().first; - expect(state.exercises, hasLength(1)); - expect(state.exercises.single.translations, isEmpty); - expect(state.exercises.single.images, isEmpty); + expect(state.exercises, isEmpty); }); test('keeps each exercise distinct when more than one is present', () async { + await seedLanguage(testEnglish.id, 'en', 'English'); await seedCategory(testCategoryArms.id, 'Arms'); await seedExercise(1, categoryId: testCategoryArms.id); await seedExercise(2, categoryId: testCategoryArms.id); + await seedTranslation(1, exerciseId: 1, languageId: testEnglish.id, name: 'Bench Press'); + await seedTranslation(2, exerciseId: 2, languageId: testEnglish.id, name: 'Squat'); final state = await repo.watchAllDrift().first; diff --git a/test/exercises/model_exercise_test.dart b/test/exercises/model_exercise_test.dart index e379b50ac..5279485bb 100644 --- a/test/exercises/model_exercise_test.dart +++ b/test/exercises/model_exercise_test.dart @@ -1,4 +1,5 @@ import 'package:flutter_test/flutter_test.dart'; +import 'package:wger/models/exercises/exercise.dart'; import '../../test_data/exercises.dart'; @@ -17,5 +18,18 @@ void main() { expect(base.getTranslation('fr-FR').id, 3); expect(base.getTranslation('pt').id, 5); // English again }); + + test('getTranslation returns an empty placeholder when there are no translations', () { + // arrange: an exercise can briefly have no translations during the + // initial sync, or ship without any. getTranslation must not throw. + const exercise = Exercise(id: 1, uuid: 'uuid', category: testCategoryArms); + + // act + final translation = exercise.getTranslation('de-AT'); + + // assert + expect(translation.name, ''); + expect(translation.language.shortName, 'de'); + }); }); }